PHP Mail Local OSX and Web-Internet

From HaFrWiki42
Jump to navigation Jump to search

The standard features of UNIX mail (OSX) are limited, making the usage in Terminal or in PHP (which uses the sendmail) barely usable.
Therefor I decide to use PHPMailer [1] for using mail on intranet (MAMP Pro) and Internet.

Introduction

Basic Usage

Wrapper

Email Lists

The main reason to send emails is to send mailing lists, where the recipients do not see each other email addresses. [2].

PHPMailer is commonly used to send to mailing lists. Let focus on the details in the following example script. Some basic optimizations can be seen in that script: <syntaxhighlight lang="PHP" line start="1"> <?php /**

* This example shows how to send a message to a whole list of recipients efficiently.
*/

//Import the PHPMailer class into the global namespace use PHPMailer\PHPMailer\PHPMailer; error_reporting(E_STRICT | E_ALL); date_default_timezone_set('Etc/UTC'); require '../vendor/autoload.php'; $mail = new PHPMailer; $body = file_get_contents('contents.html'); $mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead $mail->Port = 25; $mail->Username = 'yourname@example.com'; $mail->Password = 'yourpassword'; $mail->setFrom('list@example.com', 'List manager'); $mail->addReplyTo('list@example.com', 'List manager'); $mail->Subject = "PHPMailer Simple database mailing list test"; //Same body for all messages, so set this before the sending loop //If you generate a different body for each recipient (e.g. you're using a templating system), //set it inside the loop $mail->msgHTML($body); //msgHTML also sets AltBody, but if you want a custom one, set it afterwards $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; //Connect to the database and select the recipients from your mailing list that have not yet been sent to //You'll need to alter this to match your database $mysql = mysqli_connect('localhost', 'username', 'password'); mysqli_select_db($mysql, 'mydb'); $result = mysqli_query($mysql, 'SELECT full_name, email, photo FROM mailinglist WHERE sent = FALSE'); foreach ($result as $row) {

   $mail->addAddress($row['email'], $row['full_name']);
   if (!empty($row['photo'])) {
       $mail->addStringAttachment($row['photo'], 'YourPhoto.jpg'); //Assumes the image data is stored in the DB
   }
   if (!$mail->send()) {
       echo "Mailer Error (" . str_replace("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo . '
'; break; //Abandon sending } else { echo "Message sent to :" . $row['full_name'] . ' (' . str_replace("@", "@", $row['email']) . ')
'; //Mark it as sent in the DB mysqli_query( $mysql, "UPDATE mailinglist SET sent = TRUE WHERE email = '" . mysqli_real_escape_string($mysql, $row['email']) . "'" ); } // Clear all addresses and attachments for next loop $mail->clearAddresses(); $mail->clearAttachments();

} </syntaxhighlight>

  • Don't create a new PHPMailer instance every time around the loop.
  • Enable 'SMTPKeepAlive' (line 15) and sort your list by domain to reduce SMTP overhead and maximise connection re-use.
  • Set all the properties that will remain the same for every recipient (e.g. From, FromName, Subject etc) before the sending loop so they only happen once.
  • Inside the loop you should have only calls to addAddress(), send() and clearAddresses() (after sending).
  • If the messages you send are absolutely identical, you can add all the addresses using addBCC(), which will mean you only need to send() a single message, though most mail servers will have a limit on the number of addresses you can send at once.

Fake Email Servers

Or how to test your email sending solutions ? [3]

When working on email sending code you'll find yourself worrying about what might happen if all these test emails got sent to your mailing list.
The solution is to use a fake mail server, one that acts just like the real thing, but just doesn't actually send anything out.
Some offer web interfaces, feedback, logging, the ability to return specific error codes, all things that are useful for testing error handling, authentication etc.
Here's a selection of mail testing tools you might like to try:

Most of these examples use the example.com and example.net domains.
These domains are reserved by IANA for illustrative purposes, as documented in RFC 2606.
Don't use made-up domains like 'mydomain.com' or 'somedomain.com' in examples as someone, somewhere, probably owns them!

See also

top

  • Help One, Can I use your SMTP server to send emails?

Reference

top

  1. GitHub, PHPMailer is The classic email sending library for PHP
  2. PHPMailer wiki, Sending to lists - Rikk edited this page on Oct 15, 2014
  3. PHPMailer Examples, Fake email Servers.