Sending Email with PHP

In my previous article on how to create a mail form, I explained how to send an e-mail with the normal e-mail function. Now I will talk about sending e-mail with SMTP, which is an alternative and more effective method to that method.

SMTP (Simple Mail Transfer Protocol) is an electronic mail sending protocol. With PHP's normal mail function, we generally try to send it by defining a sender address of our choice on the server name and without any verification, whereas in SMTP, we need a real mail account to send the mail. Since the accuracy and reliability of the e-mails sent in this way are higher compared to the e-mail function, there is less chance of them falling into spam (junk) folders. With SMTP, you can send e-mails from your own server on behalf of other e-mail accounts, and I use this method especially for this purpose. Instead of using my own mail server, I direct the mail DNS (MX) settings of my own domain to Gmail and send emails via SMTP. In this way, there is no such thing as the messages I send falling into spam.

PHP SMTP Class: PHPMailer

For this case, we will learn not how to code from scratch, but how to use the existing multi-featured open source SMTP class. I'll show. Believe me, it is very simple to use and it is possible to do anything you want regarding e-mail events (sending a file, adding more than one recipient, changing the reply address, etc.).

Sending Mail

Sending SMTP e-mail with the PHPMailer class. Just take the “class.phpmailer.php” and “class.smtp.php” files and use the code block below. Enter your own server's host, port and username and password information in the relevant fields.


include 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.mysite.com';
$mail->Port = 587;
$mail->Username = 'my@address.com';
$mail->Password = 'topsecretpassword';
$mail->SetFrom($mail->Username, 'My Name');
$mail->AddAddress('recipient@adresi.com', 'Recipient's Name');
$mail->CharSet = 'UTF-8';
$mail->Subject = 'Mail Title';
$mail->MsgHTML('Content of the email!');
if($mail->Send()) {
 echo 'Mail sent!';
} else {
 echo 'An error occurred while sending the mail: ' . $mail->ErrorInfo;
}
It's that neat and simple. If you want to use this event with your existing Gmail account instead of using your own server, as I mentioned above, change the host and port section above as follows:

$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
Sending a file by e-mail We will use the AddAttachment method to add files to the e-mail you will send. For example, to send an image:

$mail->AddAttachment('img/image_to-send.jpg');
It's that simple. Send to multiple people There is no need for anything extra to add multiple recipients. We can use the AddAddress method again, where we defined the recipient address above. But if you want to add CC or BCC, just use the AddCC or AddBCC methods.

$mail->AddAddress('baska@biri.com', 'Someone else');
$mail->AddCC('baska@iki.com', 'Other');
$mail->AddBCC('baska@ucu.com', 'Other');
By the way, we should also mention the difference between CC and BCC; CC: Sends a “Carbon Copy” copy to the addresses added in this way. BCC: “Blend Carbon Copy” Same as CC, except that the addresses written here will not be visible to other sent persons.

Changing the reply address

If you want a reply to be sent to another address rather than the address you sent. All you have to do is type the e-mail address you want to be replied to using the AddReplyTo method.


$mail->AddReplyTo('cevaplar@buraya.com', 'Answer');