Casbay Knowledge Base

Search our articles or browse by category below

Sending email using PHP (PHPMailer)

Last modified: October 1, 2022
You are here:
Estimated reading time: 2 min

Sending email using PHP (PHPMailer)

With SMTP authentication, you can send an email via PHP. You just have to download PHPMailer class files to use the PHPMailer courses:

The following is the sample code for SMTP authentication to send e-mails using the PHP script:

$mail->Host = “mail.domain.com”; // SMTP servers
$mail->SMTPSecure = ‘ssl’; // Enable SSL encryption, then change below port to 587.

You may remove this line to use non-SSL with port 25 or 26 in below.
$mail->Port = “25”; //specify SMTP Port
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = “email@domain.com”; // SMTP username
$mail->Password = “password”; // SMTP password

$mail->From     = “email@domain.com”;
$mail->FromName = “Name”;
$mail->AddAddress(“Recipient@emailaddress.com”,”Name”);
$mail->AddReplyTo(“yourname@domain.com”,”Your Name”);

$mail->WordWrap = 50;                 // set word wrap

$mail->IsHTML(true);                               // send as HTML

$mail->Subject  =  “Here is the subject”;
$mail->Body     =  “This is the HTML body“;
$mail->AltBody  =  “This is the text-only body”;

if(!$mail->Send())
{
echo “Message was not sent

“;
echo “Mailer Error: ” . $mail->ErrorInfo;
exit;
}

echo “Message has been sent”;

?>

Please ensure that you make needed modifications to the above script.

Note: Our mail servers require your SMTP authentication script to submit an email for safety reasons.

Let's Look into PHPMailer

PHPMailer is one of the most popular open-source PHP libraries to send emails with. It was first released back in 2001, and since then it has become a PHP developer’s favorite way of sending emails programmatically, aside from others such as Swiftmailer.

Why Do We Need It?

First and foremost, there are PHP developers who need to send email from their code. The only PHP function that can make this work directly is mail(). However, it does not provide assistance for making use of popular features like HTML messages, encryption, authentication, and attachments.

Besides, formatting email correctly is surprisingly difficult. There are myriad overlapping and conflicting standards. They require tight adherence to horribly complicated formatting and encoding rules. The vast majority of code that you’ll find online that uses the mail()  function directly is just plain wrong, if not unsafe!

Furthermore, the PHP mail() function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD, and macOS platforms, however, Windows. It usually doesn’t include a local mail server. Moreover, PHPMailer’s integrated SMTP client allows email sending on all platforms without even needing a local mail server. However, please be aware, we should avoid the mail()function when possible; it’s both faster and safer to use SMTP to localhost.

Here is a suggestion:
Please don’t be tempted to do it yourself. If you don’t use PHPMailer, there are many other excellent libraries out there existed that you should look at before rolling your own. Try ZetaComponents, SwiftMailer , Laminas/Mail,  etc.

Was this article helpful?
Dislike 0
Previous: HOW TO: Block all ports in IPtables
Next: File & Folder Permission [INFO]