Skip to content

Commit

Permalink
Add SMTP example
Browse files Browse the repository at this point in the history
Simplify other code examples
  • Loading branch information
Synchro committed Sep 25, 2014
1 parent e43b264 commit e8c61ad
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 86 deletions.
7 changes: 4 additions & 3 deletions examples/code_generator.phps
@@ -1,8 +1,9 @@
<?php
/*
* revised, updated and corrected 27/02/2013
* by matt.sturdy@gmail.com
*/
* A web form that both generates and uses PHPMailer code.
* revised, updated and corrected 27/02/2013
* by matt.sturdy@gmail.com
*/
require '../PHPMailerAutoload.php';

$CFG['smtp_debug'] = 2; //0 == off, 1 for client output, 2 for client and server
Expand Down
14 changes: 4 additions & 10 deletions examples/exceptions.phps
@@ -1,11 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHPMailer - Exceptions test</title>
</head>
<body>
<?php
/**
* This example shows how to make use of PHPMailer's exceptions for error handling.
*/

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
Expand Down Expand Up @@ -36,6 +33,3 @@ try {
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
</body>
</html>
15 changes: 4 additions & 11 deletions examples/gmail.phps
@@ -1,11 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
Expand All @@ -14,7 +10,7 @@ date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();
Expand Down Expand Up @@ -74,6 +70,3 @@ if (!$mail->send()) {
} else {
echo "Message sent!";
}
?>
</body>
</html>
3 changes: 3 additions & 0 deletions examples/index.html
Expand Up @@ -40,6 +40,9 @@ <h2><a href="pop_before_smtp.phps">pop_before_smtp.phps</a></h2>
<h2><a href="mailing_list.phps">mailing_list.phps</a></h2>
<p>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.</p>
<hr>
<h2><a href="smtp_check.phps">smtp_check.phps</a></h2>
<p>This is an example showing how to use the SMTP class by itself (without PHPMailer) to check an SMTP connection.</p>
<hr>
<p>Most of these examples use the 'example.com' domain. This domain is reserved by IANA for illustrative purposes, as documented in <a href="http://tools.ietf.org/html/rfc2606">RFC 2606</a>. Don't use made-up domains like 'mydomain.com' or 'somedomain.com' in examples as someone, somewhere, probably owns them!</p>
</body>
</html>
14 changes: 4 additions & 10 deletions examples/mail.phps
@@ -1,11 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHPMailer - mail() test</title>
</head>
<body>
<?php
/**
* This example shows sending a message using PHP's mail() function.
*/

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
Expand All @@ -32,6 +29,3 @@ if (!$mail->send()) {
} else {
echo "Message sent!";
}
?>
</body>
</html>
22 changes: 13 additions & 9 deletions examples/mailing_list.phps
Expand Up @@ -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');

Expand All @@ -26,27 +26,31 @@ $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("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
break; //Abandon sending
} else {
echo "Message sent to :" . $row['full_name'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';
//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
Expand Down
14 changes: 4 additions & 10 deletions examples/pop_before_smtp.phps
@@ -1,11 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHPMailer - POP-before-SMTP test</title>
</head>
<body>
<?php
/**
* This example shows how to use POP-before-SMTP for authentication.
*/

require '../PHPMailerAutoload.php';

//Authenticate via POP3.
Expand Down Expand Up @@ -55,6 +52,3 @@ try {
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
</body>
</html>
16 changes: 5 additions & 11 deletions examples/sendmail.phps
@@ -1,15 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHPMailer - sendmail test</title>
</head>
<body>
<?php
/**
* This example shows sending a message using a local sendmail binary.
*/

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail = new PHPMailer;
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
Expand All @@ -34,6 +31,3 @@ if (!$mail->send()) {
} else {
echo "Message sent!";
}
?>
</body>
</html>
15 changes: 4 additions & 11 deletions examples/smtp.phps
@@ -1,11 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHPMailer - SMTP test</title>
</head>
<body>
<?php
/**
* This example shows making an SMTP connection with authentication.
*/

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
Expand All @@ -14,7 +10,7 @@ date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
Expand Down Expand Up @@ -56,6 +52,3 @@ if (!$mail->send()) {
} else {
echo "Message sent!";
}
?>
</body>
</html>
40 changes: 40 additions & 0 deletions examples/smtp_check.phps
@@ -0,0 +1,40 @@
<?php
/**
* This uses the SMTP class alone to check that a connection can be made to an SMTP server,
* authenticate, then disconnect
*/

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require '../PHPMailerAutoload.php';

//Create a new SMTP instance
$smtp = new SMTP;

//Enable connection-level debug output
$smtp->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);
15 changes: 4 additions & 11 deletions examples/smtp_no_auth.phps
@@ -1,11 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHPMailer - SMTP without auth test</title>
</head>
<body>
<?php
/**
* This example shows making an SMTP connection without using authentication.
*/

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
Expand All @@ -14,7 +10,7 @@ date_default_timezone_set('Etc/UTC');
require_once '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
Expand Down Expand Up @@ -52,6 +48,3 @@ if (!$mail->send()) {
} else {
echo "Message sent!";
}
?>
</body>
</html>

0 comments on commit e8c61ad

Please sign in to comment.