Source for file phpmailer_test.php
Documentation is available at phpmailer_test.php
require ("../class.phpmailer.php");
* Performs authentication tests
* Holds the default phpmailer instance.
* Holds the SMTP mail host.
/* must define this constructor */
* Run before each test is started.
$this->Mail->Priority = 3;
$this->Mail->Encoding = "8bit";
$this->Mail->CharSet = "iso-8859-1";
$this->Mail->From = "Frederik PHPMAILER";
$this->Mail->FromName = "Frederik PHP";
$this->Mail->Sender = "";
$this->Mail->Subject = "AMAI";
$this->Mail->AltBody = "";
$this->Mail->WordWrap = 0;
$this->Mail->Host = $global_vars["mail_host"];
$this->Mail->Helo = "localhost.localdomain";
$this->Mail->SMTPAuth = false;
$this->Mail->Username = "";
$this->Mail->Password = "";
$this->Mail->PluginDir = $INCLUDE_DIR;
$this->Mail->AddReplyTo("no_reply@derakkers.be", "NIET REPLYEN");
$this->Mail->Sender = "nobody@example.com";
$this->Mail->Mailer = "smtp";
$this->Mail->Mailer = "mail";
$this->Sender = "unit_test@phpmailer.sf.net";
$this->SetAddress($global_vars["mail_to"], "Test User");
if(strlen($global_vars["mail_cc"]) > 0)
$this->SetAddress($global_vars["mail_cc"], "Carbon User", "cc");
* Run after each test is completed.
// Clean global variables
* Build the body of the message in the appropriate format.
// Determine line endings for message
if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0)
$ReportBody .= "---------------------" . $eol;
$ReportBody .= "Unit Test Frederik " . $eol;
$ReportBody .= "---------------------" . $eol;
$ReportBody .= "phpmailer version: " . $this->Mail->Version . $eol;
$ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol;
$ReportBody .= "Host: " . $this->Mail->Host . $eol;
// If attachments then create an attachment list
$ReportBody .= "Attachments:" . $eol;
$ReportBody .= $bullet_start;
for($i = 0; $i < count($this->Mail->attachment); $i++ )
$ReportBody .= $bullet . "Name: " . $this->Mail->attachment[$i][1] . ", ";
$ReportBody .= "Encoding: " . $this->Mail->attachment[$i][3] . ", ";
$ReportBody .= "Type: " . $this->Mail->attachment[$i][4] . $eol;
$ReportBody .= $bullet_end . $eol;
// If there are changes then list them
$ReportBody .= "Changes" . $eol;
$ReportBody .= "-------" . $eol;
$ReportBody .= $bullet_start;
$ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" .
$ReportBody .= $bullet_end . $eol . $eol;
// If there are notes then list them
$ReportBody .= "Notes" . $eol;
$ReportBody .= "-----" . $eol;
$ReportBody .= $bullet_start;
$ReportBody .= $bullet . $this->NoteLog[$i] . $eol;
$ReportBody .= $bullet_end;
// Re-attach the original body
$this->Mail->Body .= $eol . $eol . $ReportBody;
* Check which default settings have been changed for the report.
if($this->Mail->Priority != 3)
if($this->Mail->Encoding != "8bit")
if($this->Mail->CharSet != "iso-8859-1")
if($this->Mail->Sender != "")
if($this->Mail->WordWrap != 0)
if($this->Mail->Mailer != "mail")
if($this->Mail->Port != 25)
if($this->Mail->Helo != "localhost.localdomain")
if($this->Mail->SMTPAuth)
* Adds a simple note to the message.
* Adds all of the addresses
function SetAddress($sAddress, $sName = "", $sType = "to") {
$this->Mail->AddAddress($sAddress, $sName);
$this->Mail->AddCC($sAddress, $sName);
$this->Mail->AddBCC($sAddress, $sName);
/////////////////////////////////////////////////
/////////////////////////////////////////////////
$this->Mail->WordWrap = 40;
$my_body = "Here is the main body of this message. It should " .
"be quite a few lines. It should be wrapped at the " .
"40 characters. Make sure that it is.";
$my_body .= "\n\nThis is the above body length: " . $nBodyLen;
$this->Mail->Body = $my_body;
$this->Mail->Subject .= ": Wordwrap";
$this->Mail->Priority = 5;
$this->Mail->Body = "Here is the main body. There should be " .
"a reply to address in this message.";
$this->Mail->Subject .= ": Low Priority";
$this->Mail->AddReplyTo("nobody@nobody.com", "Nobody (Unit Test)");
* Simple plain file attachment test.
$this->Mail->Body = "Here is the text body";
$this->Mail->Subject .= ": Plain + Multiple FileAttachments";
if(!$this->Mail->AddAttachment("rocks.png"))
if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))
* Simple plain string attachment test.
$this->Mail->Body = "Here is the text body";
$this->Mail->Subject .= ": Plain + StringAttachment";
$sAttachment = "These characters are the content of the " .
"string attachment.\nThis might be taken from a ".
"database or some other such thing. ";
$this->Mail->AddStringAttachment($sAttachment, "string_attach.txt");
* Plain quoted-printable message.
$this->Mail->Body = "Here is the main body";
$this->Mail->Subject .= ": Plain + Quoted-printable";
$this->Mail->Encoding = "quoted-printable";
$this->Mail->IsHTML(true);
$this->Mail->Subject .= ": HTML only";
$this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .
"Go to <a href=\"http://phpmailer.sourceforge.net/\">" .
"http://phpmailer.sourceforge.net/</a> for new versions of " .
"phpmailer. <p/> Thank you!";
* Simple HTML and attachment test
$this->Mail->Body = "This is the <b>HTML</b> part of the email.";
$this->Mail->Subject .= ": HTML + Attachment";
$this->Mail->IsHTML(true);
if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
* An embedded attachment test.
$this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
$this->Mail->Subject .= ": Embedded Image";
$this->Mail->IsHTML(true);
if(!$this->Mail->AddEmbeddedImage("rocks.png", "my-attach", "rocks.png",
* An embedded attachment test.
$this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
$this->Mail->Subject .= ": Embedded Image + Attachment";
$this->Mail->IsHTML(true);
if(!$this->Mail->AddEmbeddedImage("rocks.png", "my-attach", "rocks.png",
if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))
* Simple multipart/alternative test.
$this->Mail->Body = "This is the <b>HTML</b> part of the email.";
$this->Mail->AltBody = "Here is the text body of this message. " .
"It should be quite a few lines. It should be wrapped at the " .
"40 characters. Make sure that it is.";
$this->Mail->WordWrap = 40;
$this->AddNote("This is a mulipart alternative email");
$this->Mail->Subject .= ": AltBody + Word Wrap";
* Simple HTML and attachment test
$this->Mail->Body = "This is the <b>HTML</b> part of the email.";
$this->Mail->AltBody = "This is the text part of the email.";
$this->Mail->Subject .= ": AltBody + Attachment";
$this->Mail->IsHTML(true);
if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
$fp = fopen("message.txt", "w");
$this->Mail->Body = "Sending two messages without keepalive";
$subject = $this->Mail->Subject;
$this->Mail->Subject = $subject . ": SMTP 1";
$this->Mail->Subject = $subject . ": SMTP 2";
$this->Mail->Body = "This was done using the SMTP keep-alive.";
$subject = $this->Mail->Subject;
$this->Mail->SMTPKeepAlive = true;
$this->Mail->Subject = $subject . ": SMTP keep-alive 1";
$this->Mail->Subject = $subject . ": SMTP keep-alive 2";
$this->Mail->SmtpClose();
$this->Mail->Subject .= ": This should be sent";
$this->Mail->ClearAllRecipients(); // no addresses should cause an error
$this->assert($this->Mail->IsError() == false, "Error found");
$this->assert($this->Mail->Send() == false, "Send succeeded");
$this->assert($this->Mail->IsError(), "No error found");
'recipient email address.', $this->Mail->ErrorInfo);
$this->Mail->AddAddress(get("mail_to"));
$this->assert($this->Mail->Send(), "Send failed");
* Create and run test instance.
if(isset ($HTTP_GET_VARS))
$global_vars = $HTTP_GET_VARS;
$global_vars = $_REQUEST;
if(isset ($global_vars["submitted"]))
echo "Test results:<br>";
$testRunner->run($suite);
if(isset ($global_vars[$sName]))
return $global_vars[$sName];
<h3>phpmailer Unit Test</h3>
By entering a SMTP hostname it will automatically perform tests with SMTP.
<form name="phpmailer_unit" action="phpmailer_test.php" method="get">
<input type="hidden" name="submitted" value="1"/>
To Address: <input type="text" size="50" name="mail_to" value=" <?php echo get("mail_to"); ?>"/>
Cc Address: <input type="text" size="50" name="mail_cc" value=" <?php echo get("mail_cc"); ?>"/>
SMTP Hostname: <input type="text" size="50" name="mail_host" value=" <?php echo get("mail_host"); ?>"/>
<input type="submit" value="Run Test"/>
|