From e8c61ada878fe11c5c5952ee282792b2b73c48ae Mon Sep 17 00:00:00 2001 From: Synchro Date: Thu, 25 Sep 2014 14:18:57 +0200 Subject: [PATCH] Add SMTP example Simplify other code examples --- examples/code_generator.phps | 7 +++--- examples/exceptions.phps | 14 ++++-------- examples/gmail.phps | 15 ++++--------- examples/index.html | 3 +++ examples/mail.phps | 14 ++++-------- examples/mailing_list.phps | 22 +++++++++++-------- examples/pop_before_smtp.phps | 14 ++++-------- examples/sendmail.phps | 16 +++++--------- examples/smtp.phps | 15 ++++--------- examples/smtp_check.phps | 40 +++++++++++++++++++++++++++++++++++ examples/smtp_no_auth.phps | 15 ++++--------- 11 files changed, 89 insertions(+), 86 deletions(-) create mode 100644 examples/smtp_check.phps diff --git a/examples/code_generator.phps b/examples/code_generator.phps index af0cfcc12..de04f0cc8 100644 --- a/examples/code_generator.phps +++ b/examples/code_generator.phps @@ -1,8 +1,9 @@ - - - - PHPMailer - Exceptions test - - getMessage(); //Boring error messages from anything else! } -?> - - diff --git a/examples/gmail.phps b/examples/gmail.phps index a6d419887..b020f3380 100644 --- a/examples/gmail.phps +++ b/examples/gmail.phps @@ -1,11 +1,7 @@ - - - - - PHPMailer - GMail SMTP test - - isSMTP(); @@ -74,6 +70,3 @@ if (!$mail->send()) { } else { echo "Message sent!"; } -?> - - diff --git a/examples/index.html b/examples/index.html index 4c55023bd..74674a2b8 100644 --- a/examples/index.html +++ b/examples/index.html @@ -40,6 +40,9 @@

pop_before_smtp.phps

mailing_list.phps

This is a somewhat naïve example of sending similar emails to a list of different addresses. It sets up a PHPMailer instance using SMTP, then connects to a MySQL database to retrieve a list of recipients. The code loops over this list, sending email to each person using their info and marks them as sent in the database. It makes use of SMTP keepalive which saves reconnecting and re-authenticating between each message.


+

smtp_check.phps

+

This is an example showing how to use the SMTP class by itself (without PHPMailer) to check an SMTP connection.

+

Most of these examples use the 'example.com' domain. This domain is 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!

diff --git a/examples/mail.phps b/examples/mail.phps index f12dbaf2d..4436cb0f9 100644 --- a/examples/mail.phps +++ b/examples/mail.phps @@ -1,11 +1,8 @@ - - - - - PHPMailer - mail() test - - send()) { } else { echo "Message sent!"; } -?> - - diff --git a/examples/mailing_list.phps b/examples/mailing_list.phps index 070e82d34..8644bb596 100644 --- a/examples/mailing_list.phps +++ b/examples/mailing_list.phps @@ -6,7 +6,7 @@ date_default_timezone_set('Etc/UTC'); require '../PHPMailerAutoload.php'; -$mail = new PHPMailer(); +$mail = new PHPMailer; $body = file_get_contents('contents.html'); @@ -26,18 +26,20 @@ $mail->Subject = "PHPMailer Simple database mailing list test"; //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, so if you want a custom one, set it afterwards +//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 = mysql_connect('localhost', 'username', 'password'); -mysql_select_db('mydb', $mysql); -$result = mysql_query("SELECT full_name, email, photo FROM mailinglist WHERE sent = false", $mysql); +$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'); -while ($row = mysql_fetch_array($result)) { +foreach ($result as $row) { //This iterator syntax only works in PHP 5.4+ $mail->addAddress($row['email'], $row['full_name']); - $mail->addStringAttachment($row['photo'], 'YourPhoto.jpg'); //Assumes the image data is stored in the DB + 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 . '
'; @@ -45,8 +47,10 @@ while ($row = mysql_fetch_array($result)) { } else { echo "Message sent to :" . $row['full_name'] . ' (' . str_replace("@", "@", $row['email']) . ')
'; //Mark it as sent in the DB - mysql_query( - "UPDATE mailinglist SET sent = true WHERE email = '" . mysql_real_escape_string($row['email'], $mysql) . "'" + 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 diff --git a/examples/pop_before_smtp.phps b/examples/pop_before_smtp.phps index 44a7d4279..164dfe8dd 100644 --- a/examples/pop_before_smtp.phps +++ b/examples/pop_before_smtp.phps @@ -1,11 +1,8 @@ - - - - - PHPMailer - POP-before-SMTP test - - getMessage(); //Boring error messages from anything else! } -?> - - diff --git a/examples/sendmail.phps b/examples/sendmail.phps index 55b90f644..a830e4952 100644 --- a/examples/sendmail.phps +++ b/examples/sendmail.phps @@ -1,15 +1,12 @@ - - - - - PHPMailer - sendmail test - - isSendmail(); //Set who the message is to be sent from @@ -34,6 +31,3 @@ if (!$mail->send()) { } else { echo "Message sent!"; } -?> - - diff --git a/examples/smtp.phps b/examples/smtp.phps index d9cc66f57..7c6b02396 100644 --- a/examples/smtp.phps +++ b/examples/smtp.phps @@ -1,11 +1,7 @@ - - - - - PHPMailer - SMTP test - - isSMTP(); //Enable SMTP debugging @@ -56,6 +52,3 @@ if (!$mail->send()) { } else { echo "Message sent!"; } -?> - - diff --git a/examples/smtp_check.phps b/examples/smtp_check.phps new file mode 100644 index 000000000..c42ed0bea --- /dev/null +++ b/examples/smtp_check.phps @@ -0,0 +1,40 @@ +do_debug = SMTP::DEBUG_CONNECTION; + +try { +//Connect to an SMTP server + if ($smtp->connect('mail.example.com', 25)) { + //Say hello + if ($smtp->hello('localhost')) { //Put your host name in here + //Authenticate + if ($smtp->authenticate('username', 'password')) { + echo "Connected ok!"; + } else { + throw new Exception('Authentication failed: ' . $smtp->getLastReply()); + } + } else { + throw new Exception('HELO failed: '. $smtp->getLastReply()); + } + } else { + throw new Exception('Connect failed'); + } +} catch (Exception $e) { + echo 'SMTP error: '. $e->getMessage(), "\n"; +} +//Whatever happened, close the connection. +$smtp->quit(true); diff --git a/examples/smtp_no_auth.phps b/examples/smtp_no_auth.phps index 84756ec03..b59029842 100644 --- a/examples/smtp_no_auth.phps +++ b/examples/smtp_no_auth.phps @@ -1,11 +1,7 @@ - - - - - PHPMailer - SMTP without auth test - - isSMTP(); //Enable SMTP debugging @@ -52,6 +48,3 @@ if (!$mail->send()) { } else { echo "Message sent!"; } -?> - -