Skip to content

PHP mailer

MwauraKimani edited this page Feb 2, 2020 · 3 revisions

Mailing

PHP provides a mail() function that looks enticingly simple and easy. Unfortunately, like a lot of things in PHP, its simplicity is deceptive and using it at face value can lead to serious security problems.

Email is a set of protocols with an even more tortured history than PHP. Suffice it to say that there are so many gotchas in sending email that just being in the same room as PHP’s mail() function should give you the shivers.

PHPMailer is a popular and well-aged open-source library that provides an easy interface for sending mail securely. It takes care of the gotchas for you so you can concentrate on more important things.

<?php // Include the PHPMailer library require_once('phpmailer-5.2.7/PHPMailerAutoload.php');

// Passing 'true' enables exceptions. This is optional and defaults to false. $mailer = new PHPMailer(true);

// Send a mail from Bilbo Baggins to Gandalf the Grey

// Set up to, from, and the message body. The body doesn't have to be HTML; check the PHPMailer documentation for details. $mailer->Sender = '[email protected]'; $mailer->AddReplyTo('[email protected]', 'Bilbo Baggins'); $mailer->SetFrom('[email protected]', 'Bilbo Baggins'); $mailer->AddAddress('[email protected]'); $mailer->Subject = 'The finest weed in the South Farthing'; $mailer->MsgHTML('<p>You really must try it, Gandalf!</p><p>-Bilbo</p>');

// Set up our connection information. $mailer->IsSMTP(); $mailer->SMTPAuth = true; $mailer->SMTPSecure = 'ssl'; $mailer->Port = 465; $mailer->Host = 'my smtp host'; $mailer->Username = 'my smtp username'; $mailer->Password = 'my smtp password';

// All done! $mailer->Send(); ?>

Clone this wiki locally