Skip to content

Commit

Permalink
Major overhaul and cleanup of example code
Browse files Browse the repository at this point in the history
Update test_script to use some recently changed features, rename to code_generator
Generated code actually works!
Update SyntaxHighlighter
New PHPMailer graphic
  • Loading branch information
Synchro committed Apr 26, 2013
1 parent b5b2ae1 commit ff8718f
Show file tree
Hide file tree
Showing 94 changed files with 2,336 additions and 5,421 deletions.
13 changes: 8 additions & 5 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
* Add Ukranian translation from @Krezalis
* Support for do_verp
* Fix bug in CRAM-MD5 AUTH
* Propagate debug output option to SMTP class (@Reblutus)
* Propagate Debugoutput option to SMTP class (@Reblutus)
* Determine MIME type of attachments automatically
* Add cross-platform, multibyte-safe pathinfo replacement (with tests) and use it
* Add a new 'html' Debugoutput type
* Clean up SMTP debug output, remove unnecessary repeated code
* Clean up SMTP debug output, remove embedded HTML
* Some small changes in header formatting to improve IETF msglint test results
* Update test script to use some recently changed features
* Update test_script to use some recently changed features, rename to code_generator
* Generated code actually works!
* Update SyntaxHighlighter
* Major overhaul and cleanup of example code
* New PHPMailer graphic

## Version 5.2.6 (April 11th 2013)
* Reflect move to PHPMailer GitHub organisation at https://github.com/PHPMailer/PHPMailer
Expand All @@ -27,8 +31,7 @@

## Version 5.2.4 (February 19, 2013)
* Fix tag and version bug.
* un-deprecate isSMTP(), isMail(), IsSendmail() and
isQmail().
* un-deprecate isSMTP(), isMail(), IsSendmail() and isQmail().
* Numerous translation updates

## Version 5.2.3 (February 8, 2013)
Expand Down
File renamed without changes.
56 changes: 34 additions & 22 deletions test_script/index.php → examples/code_generator.phps
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
require '../class.phpmailer.php';

$CFG['smtp_debug'] = 1;
$CFG['smtp_debug'] = 2; //0 == off, 1 for client output, 2 for client and server
$CFG['smtp_debugoutput'] = 'html';
$CFG['smtp_server'] = 'localhost';
$CFG['smtp_port'] = '25';
Expand Down Expand Up @@ -33,22 +33,22 @@

// storing all status output from the script to be shown to the user later
$results_messages = array();
$errorMsg = array();

// $example_code represents the "final code" that we're using, and will
// be shown to the user at the end.
$example_code = "\nrequire_once '../class.phpmailer.php';";
$example_code .= "\n\n\$results_messages = new array();";
$example_code .= "\n\n\$results_messages = array();";

$mail = new PHPMailer(true);
$mail = new PHPMailer(true); //PHPMailer instance with exceptions enabled
$mail->CharSet = 'utf-8';
$mail->Debugoutput = $CFG['smtp_debugoutput']; //Format error messages so they are HTML-safe
$mail->Debugoutput = $CFG['smtp_debugoutput'];
$example_code .= "\n\n\$mail = new PHPMailer(true);";
$example_code .= "\n\$mail->CharSet = 'utf-8';";

class phpmailerAppException extends phpmailerException {}

$example_code .= "\n\nclass phpmailerAppException extends phpmailerException {}";
$example_code .= "\n\ntry {";

try {
if ( isset($_POST["submit"]) && $_POST['submit'] == "Submit" ) {
Expand All @@ -57,32 +57,32 @@ class phpmailerAppException extends phpmailerException {}
throw new phpmailerAppException("Email address " . $to . " is invalid -- aborting!");
}

$example_code .= "\n\$to = \$_POST[\"To_Email\"];";
$example_code .= "\nif(!PHPMailer::ValidateAddress($to)) {";
$example_code .= "\n\$to = '{$_POST['To_Email']}';";
$example_code .= "\nif(!PHPMailer::ValidateAddress(\$to)) {";
$example_code .= "\n throw new phpmailerAppException(\"Email address \" . \$to . \" is invalid -- aborting!\");";
$example_code .= "\n}";

switch ($_POST['test_type']) {
case 'smtp':
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = $_POST['smtp_debug'];
$mail->SMTPDebug = (integer)$_POST['smtp_debug'];
$mail->Host = $_POST['smtp_server']; // SMTP server
$mail->Port = $_POST['smtp_port']; // set the SMTP port
$mail->Port = (integer)$_POST['smtp_port']; // set the SMTP port
if ($_POST['smtp_secure']) {
$mail->SMTPSecure = strtolower($_POST['smtp_secure']);
}
$mail->SMTPAuth = array_key_exists('smtp_authenticate', $_POST); // enable SMTP authentication
$mail->SMTPAuth = array_key_exists('smtp_authenticate', $_POST); // enable SMTP authentication?
if (array_key_exists('smtp_authenticate', $_POST)) {
$mail->Username = $_POST['authenticate_username']; // SMTP account username
$mail->Password = $_POST['authenticate_password']; // SMTP account password
}

$example_code .= "\n\$mail->IsSMTP();";
$example_code .= "\n\$mail->SMTPDebug = \"".$_POST['smtp_debug']."\";";
$example_code .= "\n\$mail->SMTPDebug = ".$_POST['smtp_debug'].";";
$example_code .= "\n\$mail->Host = \"".$_POST['smtp_server']."\";";
$example_code .= "\n\$mail->Port = \"".$_POST['smtp_port']."\";";
$example_code .= "\n\$mail->SMTPSecure = \"".strtolower($_POST['smtp_secure'])."\";";
$example_code .= "\n\$mail->SMTPAuth = \"".(array_key_exists('smtp_authenticate', $_POST)?'true':'false')."\";";
$example_code .= "\n\$mail->SMTPAuth = ".(array_key_exists('smtp_authenticate', $_POST)?'true':'false').";";
if (array_key_exists('smtp_authenticate', $_POST)) {
$example_code .= "\n\$mail->Username = \"".$_POST['authenticate_username']."\";";
$example_code .= "\n\$mail->Password = \"".$_POST['authenticate_password']."\";";
Expand Down Expand Up @@ -111,8 +111,8 @@ class phpmailerAppException extends phpmailerException {}
$mail->FromName = $_POST['From_Name'];

$example_code .= "\n\$mail->AddReplyTo(\"".$_POST['From_Email']."\", \"".$_POST['From_Name']."\");";
$example_code .= "\n\$mail->From = \"".$_POST['From_Email']."\");";
$example_code .= "\n\$mail->FromName = \"".$_POST['From_Name']."\");";
$example_code .= "\n\$mail->From = \"".$_POST['From_Email']."\";";
$example_code .= "\n\$mail->FromName = \"".$_POST['From_Name']."\";";
}
else {
$mail->AddReplyTo($_POST['From_Email']);
Expand Down Expand Up @@ -161,18 +161,18 @@ class phpmailerAppException extends phpmailerException {}
$body = $_POST['Message'];
}

$example_code .= "\n\$body = \"".htmlentities(addslashes($body)).'";';
$example_code .= "\n\$body = <<<'EOT'\n".htmlentities($body)."\nEOT;";

$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body, dirname(__FILE__), true); //Create message bodies and embed images

$example_code .= "\n\$mail->WordWrap = 80;";
$example_code .= "\n\$mail->MsgHTML(\$body, dirname(__FILE__), true); //Create message bodies and embed images";

$mail->AddAttachment('images/aikido.gif', 'aikido.gif'); // optional name
$mail->AddAttachment('images/phpmailer.gif', 'phpmailer.gif'); // optional name
$example_code .= "\n\$mail->AddAttachment('images/aikido.gif', 'aikido.gif'); // optional name";
$example_code .= "\n\$mail->AddAttachment('images/phpmailer.gif', 'phpmailer.gif'); // optional name";
$mail->AddAttachment('images/phpmailer-mini.gif', 'phpmailer-mini.gif'); // optional name
$mail->AddAttachment('images/phpmailer.png', 'phpmailer.png'); // optional name
$example_code .= "\n\$mail->AddAttachment('images/phpmailer-mini.gif', 'phpmailer-mini.gif'); // optional name";
$example_code .= "\n\$mail->AddAttachment('images/phpmailer.png', 'phpmailer.png'); // optional name";

try {
$mail->Send();
Expand All @@ -184,7 +184,7 @@ class phpmailerAppException extends phpmailerException {}

$example_code .= "\n\ntry {";
$example_code .= "\n \$mail->Send();";
$example_code .= "\n \$results_messages[] = \"Message has been sent using \" . strtoupper(\$_POST[\"test_type\"]);";
$example_code .= "\n \$results_messages[] = \"Message has been sent using ". strtoupper($_POST['test_type'])."\";";
$example_code .= "\n}";
$example_code .= "\ncatch (phpmailerException \$e) {";
$example_code .= "\n throw new phpmailerAppException('Unable to send to: ' . \$to. ': '.\$e->getMessage());";
Expand All @@ -194,6 +194,18 @@ class phpmailerAppException extends phpmailerException {}
catch (phpmailerAppException $e) {
$results_messages[] = $e->errorMessage();
}
$example_code .= "\n}";
$example_code .= "\ncatch (phpmailerAppException \$e) {";
$example_code .= "\n \$results_messages[] = \$e->errorMessage();";
$example_code .= "\n}";
$example_code .= "\n\nif (count(\$results_messages) > 0) {";
$example_code .= "\n echo \"<h2>Run results</h2>\\n\";";
$example_code .= "\n echo \"<ul>\\n\";";
$example_code .= "\nforeach (\$results_messages as \$result) {";
$example_code .= "\n echo \"<li>\$result</li>\\n\";";
$example_code .= "\n}";
$example_code .= "\necho \"</ul>\\n\";";
$example_code .= "\n}";
?><!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -453,8 +465,8 @@ function showHideDiv(test, element_id) {
<td class="colrite">
<select size="1" id="smtp_debug" name="smtp_debug">
<option <?php echo ( $smtp_debug == '0') ? 'selected' : ''; ?> value="0">0 - Disabled</option>
<option <?php echo ( $smtp_debug == '1') ? 'selected' : ''; ?> value="1">1 - Errors and Messages</option>
<option <?php echo ( $smtp_debug == '2') ? 'selected' : ''; ?> value="2">2 - Messages only</option>
<option <?php echo ( $smtp_debug == '1') ? 'selected' : ''; ?> value="1">1 - Client messages</option>
<option <?php echo ( $smtp_debug == '2') ? 'selected' : ''; ?> value="2">2 - Client and server messages</option>
</select>
</td>
</tr>
Expand Down
35 changes: 16 additions & 19 deletions examples/contents.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<div align="center"><img src="images/phpmailer.gif" style="height: 90px; width: 340px"></div><br>
<br>
&nbsp;This is a test of PHPMailer.<br>
<br>
This particular example uses <strong>HTML</strong>, with a &lt;div&gt; tag and inline<br>
styles.<br>
<br>
Also note the use of the PHPMailer logo above with no specific code to handle
including it.<br />
Included are two attachments:<br />
phpmailer.gif is an attachment and used inline as a graphic (above)<br />
phpmailer_mini.gif is an attachment<br />
<br />
PHPMailer:<br />
Author: Andy Prevost ([email protected])<br />
Author: Marcus Bointon ([email protected])<br />
</div>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer Test</title>
</head>
<body>
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<h1>This is a test of PHPMailer.</h1>
<div align="center">
<a href="https://github.com/PHPMailer/PHPMailer/"><img src="images/phpmailer.png" height="90" width="340" alt="PHPMailer rocks"></a>
</div>
<p>This example uses <strong>HTML</strong>.</p>
<p>The PHPMailer image at the top has been embedded automatically.</p>
</div>
</body>
</html>
41 changes: 41 additions & 0 deletions examples/exceptions.phps
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer - Exceptions test</title>
</head>
<body>
<?php
require '../class.phpmailer.php';

//Create a new PHPMailer instance
//Passing true to the constructor enables the use of exceptions for error handling
$mail = new PHPMailer(true);
try {
//Set who the message is to be sent from
$mail->SetFrom('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->AddReplyTo('[email protected]','First Last');
//Set who the message is to be sent to
$mail->AddAddress('[email protected]', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer Exceptions test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//and convert the HTML into a basic plain-text alternative body
$mail->MsgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->AddAttachment('images/phpmailer-mini.gif');
//Send the message
//Note that we don't need check the response from this because it will throw an exception if it has trouble
$mail->Send();
echo "Message sent!";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
</body>
</html>
62 changes: 62 additions & 0 deletions examples/gmail.phps
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php

//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 '../class.phpmailer.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "[email protected]";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->SetFrom('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->AddReplyTo('[email protected]','First Last');
//Set who the message is to be sent to
$mail->AddAddress('[email protected]', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
$mail->MsgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->AddAttachment('images/phpmailer-mini.gif');

//Send the message, check for errors
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Binary file removed examples/images/phpmailer.gif
Binary file not shown.
Binary file added examples/images/phpmailer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ff8718f

Please sign in to comment.