3
* PHPMailer - PHP email transport unit tests
4
* Before running these tests you need to install PHPUnit 3.3 or later through pear, like this:
5
* pear install "channel://pear.phpunit.de/PHPUnit"
6
* Then run the tests like this:
7
* phpunit phpmailerTest
10
* @author Marcus Bointon
11
* @copyright 2004 - 2009 Andy Prevost
12
* @version $Id: phpmailerTest.php 444 2009-05-05 11:22:26Z coolbru $
13
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
16
require 'PHPUnit/Framework.php';
20
require $INCLUDE_DIR . 'class.phpmailer-lite.php';
21
error_reporting(E_ALL);
24
* PHPMailer - PHP email transport unit test class
25
* Performs authentication tests
27
class phpmailerTest extends PHPUnit_Framework_TestCase {
29
* Holds the default phpmailer instance.
36
* Holds the SMTP mail host.
43
* Holds the change log.
47
var $ChangeLog = array();
54
var $NoteLog = array();
57
* Run before each test is started.
62
@include './testbootstrap.php'; //Overrides go in here
64
$this->Mail = new PHPMailer();
66
$this->Mail->Priority = 3;
67
$this->Mail->Encoding = "8bit";
68
$this->Mail->CharSet = "iso-8859-1";
69
if (array_key_exists('mail_from', $_REQUEST)) {
70
$this->Mail->From = $_REQUEST['mail_from'];
72
$this->Mail->From = 'unit_test@phpmailer.sf.net';
74
$this->Mail->FromName = "Unit Tester";
75
$this->Mail->Sender = "";
76
$this->Mail->Subject = "Unit Test";
77
$this->Mail->Body = "";
78
$this->Mail->AltBody = "";
79
$this->Mail->WordWrap = 0;
80
if (array_key_exists('mail_host', $_REQUEST)) {
81
$this->Mail->Host = $_REQUEST['mail_host'];
83
$this->Mail->Host = 'mail.example.com';
85
$this->Mail->Port = 25;
86
$this->Mail->Helo = "localhost.localdomain";
87
$this->Mail->SMTPAuth = false;
88
$this->Mail->Username = "";
89
$this->Mail->Password = "";
90
$this->Mail->PluginDir = $INCLUDE_DIR;
91
$this->Mail->AddReplyTo("no_reply@phpmailer.sf.net", "Reply Guy");
92
$this->Mail->Sender = "unit_test@phpmailer.sf.net";
94
if(strlen($this->Mail->Host) > 0) {
95
$this->Mail->Mailer = "smtp";
97
$this->Mail->Mailer = "mail";
98
$this->Sender = "unit_test@phpmailer.sf.net";
101
if (array_key_exists('mail_to', $_REQUEST)) {
102
$this->SetAddress($_REQUEST['mail_to'], 'Test User', 'to');
104
if (array_key_exists('mail_cc', $_REQUEST) and strlen($_REQUEST['mail_cc']) > 0) {
105
$this->SetAddress($_REQUEST['mail_cc'], 'Carbon User', 'cc');
110
* Run after each test is completed.
112
function tearDown() {
113
// Clean global variables
115
$this->ChangeLog = array();
116
$this->NoteLog = array();
121
* Build the body of the message in the appropriate format.
125
function BuildBody() {
126
$this->CheckChanges();
128
// Determine line endings for message
129
if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0)
133
$bullet_start = "<ul>";
134
$bullet_end = "</ul>";
146
$ReportBody .= "---------------------" . $eol;
147
$ReportBody .= "Unit Test Information" . $eol;
148
$ReportBody .= "---------------------" . $eol;
149
$ReportBody .= "phpmailer version: " . PHPMailer::VERSION . $eol;
150
$ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol;
152
if(strlen($this->Mail->Host) > 0)
153
$ReportBody .= "Host: " . $this->Mail->Host . $eol;
155
// If attachments then create an attachment list
156
$attachments = $this->Mail->GetAttachments();
157
if(count($attachments) > 0)
159
$ReportBody .= "Attachments:" . $eol;
160
$ReportBody .= $bullet_start;
161
foreach($attachments as $attachment) {
162
$ReportBody .= $bullet . "Name: " . $attachment[1] . ", ";
163
$ReportBody .= "Encoding: " . $attachment[3] . ", ";
164
$ReportBody .= "Type: " . $attachment[4] . $eol;
166
$ReportBody .= $bullet_end . $eol;
169
// If there are changes then list them
170
if(count($this->ChangeLog) > 0)
172
$ReportBody .= "Changes" . $eol;
173
$ReportBody .= "-------" . $eol;
175
$ReportBody .= $bullet_start;
176
for($i = 0; $i < count($this->ChangeLog); $i++)
178
$ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" .
179
$this->ChangeLog[$i][1] . "]" . $eol;
181
$ReportBody .= $bullet_end . $eol . $eol;
184
// If there are notes then list them
185
if(count($this->NoteLog) > 0)
187
$ReportBody .= "Notes" . $eol;
188
$ReportBody .= "-----" . $eol;
190
$ReportBody .= $bullet_start;
191
for($i = 0; $i < count($this->NoteLog); $i++)
193
$ReportBody .= $bullet . $this->NoteLog[$i] . $eol;
195
$ReportBody .= $bullet_end;
198
// Re-attach the original body
199
$this->Mail->Body .= $eol . $eol . $ReportBody;
203
* Check which default settings have been changed for the report.
207
function CheckChanges() {
208
if($this->Mail->Priority != 3)
209
$this->AddChange("Priority", $this->Mail->Priority);
210
if($this->Mail->Encoding != "8bit")
211
$this->AddChange("Encoding", $this->Mail->Encoding);
212
if($this->Mail->CharSet != "iso-8859-1")
213
$this->AddChange("CharSet", $this->Mail->CharSet);
214
if($this->Mail->Sender != "")
215
$this->AddChange("Sender", $this->Mail->Sender);
216
if($this->Mail->WordWrap != 0)
217
$this->AddChange("WordWrap", $this->Mail->WordWrap);
218
if($this->Mail->Mailer != "mail")
219
$this->AddChange("Mailer", $this->Mail->Mailer);
220
if($this->Mail->Port != 25)
221
$this->AddChange("Port", $this->Mail->Port);
222
if($this->Mail->Helo != "localhost.localdomain")
223
$this->AddChange("Helo", $this->Mail->Helo);
224
if($this->Mail->SMTPAuth)
225
$this->AddChange("SMTPAuth", "true");
229
* Adds a change entry.
233
function AddChange($sName, $sNewValue) {
234
$cur = count($this->ChangeLog);
235
$this->ChangeLog[$cur][0] = $sName;
236
$this->ChangeLog[$cur][1] = $sNewValue;
240
* Adds a simple note to the message.
244
function AddNote($sValue) {
245
$this->NoteLog[] = $sValue;
249
* Adds all of the addresses
253
function SetAddress($sAddress, $sName = "", $sType = "to") {
257
return $this->Mail->AddAddress($sAddress, $sName);
259
return $this->Mail->AddCC($sAddress, $sName);
261
return $this->Mail->AddBCC($sAddress, $sName);
265
/////////////////////////////////////////////////
267
/////////////////////////////////////////////////
270
* Try a plain message.
272
function test_WordWrap() {
274
$this->Mail->WordWrap = 40;
275
$my_body = "Here is the main body of this message. It should " .
276
"be quite a few lines. It should be wrapped at the " .
277
"40 characters. Make sure that it is.";
278
$nBodyLen = strlen($my_body);
279
$my_body .= "\n\nThis is the above body length: " . $nBodyLen;
281
$this->Mail->Body = $my_body;
282
$this->Mail->Subject .= ": Wordwrap";
285
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
289
* Try a plain message.
291
function test_Low_Priority() {
293
$this->Mail->Priority = 5;
294
$this->Mail->Body = "Here is the main body. There should be " .
295
"a reply to address in this message.";
296
$this->Mail->Subject .= ": Low Priority";
297
$this->Mail->AddReplyTo("nobody@nobody.com", "Nobody (Unit Test)");
300
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
304
* Simple plain file attachment test.
306
function test_Multiple_Plain_FileAttachment() {
308
$this->Mail->Body = "Here is the text body";
309
$this->Mail->Subject .= ": Plain + Multiple FileAttachments";
311
if(!$this->Mail->AddAttachment("test.png"))
313
$this->assertTrue(false, $this->Mail->ErrorInfo);
317
if(!$this->Mail->AddAttachment(__FILE__, "test.txt"))
319
$this->assertTrue(false, $this->Mail->ErrorInfo);
324
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
328
* Simple plain string attachment test.
330
function test_Plain_StringAttachment() {
332
$this->Mail->Body = "Here is the text body";
333
$this->Mail->Subject .= ": Plain + StringAttachment";
335
$sAttachment = "These characters are the content of the " .
336
"string attachment.\nThis might be taken from a ".
337
"database or some other such thing. ";
339
$this->Mail->AddStringAttachment($sAttachment, "string_attach.txt");
342
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
346
* Plain quoted-printable message.
348
function test_Quoted_Printable() {
350
$this->Mail->Body = "Here is the main body";
351
$this->Mail->Subject .= ": Plain + Quoted-printable";
352
$this->Mail->Encoding = "quoted-printable";
355
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
357
//Check that a quoted printable encode and decode results in the same as went in
358
$t = substr(file_get_contents(__FILE__), 0, 1024); //Just pick a chunk of this file as test content
359
$this->assertEquals($t, quoted_printable_decode($this->Mail->EncodeQP($t)), 'QP encoding round-trip failed');
360
//$this->assertEquals($t, quoted_printable_decode($this->Mail->EncodeQPphp($t)), 'Native PHP QP encoding round-trip failed'); //TODO the PHP qp encoder is quite broken
365
* Try a plain message.
367
function test_Html() {
369
$this->Mail->IsHTML(true);
370
$this->Mail->Subject .= ": HTML only";
372
$this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .
373
"Go to <a href=\"http://phpmailer.sourceforge.net/\">" .
374
"http://phpmailer.sourceforge.net/</a> for new versions of " .
375
"phpmailer. <p/> Thank you!";
378
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
382
* Simple HTML and attachment test
384
function test_HTML_Attachment() {
386
$this->Mail->Body = "This is the <b>HTML</b> part of the email.";
387
$this->Mail->Subject .= ": HTML + Attachment";
388
$this->Mail->IsHTML(true);
390
if(!$this->Mail->AddAttachment(__FILE__, "test_attach.txt"))
392
$this->assertTrue(false, $this->Mail->ErrorInfo);
397
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
401
* An embedded attachment test.
403
function test_Embedded_Image() {
405
$this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
406
"Here is an image!</a>";
407
$this->Mail->Subject .= ": Embedded Image";
408
$this->Mail->IsHTML(true);
410
if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",
411
"base64", "image/png"))
413
$this->assertTrue(false, $this->Mail->ErrorInfo);
418
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
420
$this->Mail->AddEmbeddedImage('thisfiledoesntexist', 'xyz'); //Non-existent file
421
$this->Mail->AddEmbeddedImage(__FILE__, '123'); //Missing name
426
* An embedded attachment test.
428
function test_Multi_Embedded_Image() {
430
$this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
431
"Here is an image!</a>";
432
$this->Mail->Subject .= ": Embedded Image + Attachment";
433
$this->Mail->IsHTML(true);
435
if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",
436
"base64", "image/png"))
438
$this->assertTrue(false, $this->Mail->ErrorInfo);
442
if(!$this->Mail->AddAttachment(__FILE__, "test.txt"))
444
$this->assertTrue(false, $this->Mail->ErrorInfo);
449
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
453
* Simple multipart/alternative test.
455
function test_AltBody() {
457
$this->Mail->Body = "This is the <b>HTML</b> part of the email.";
458
$this->Mail->AltBody = "Here is the text body of this message. " .
459
"It should be quite a few lines. It should be wrapped at the " .
460
"40 characters. Make sure that it is.";
461
$this->Mail->WordWrap = 40;
462
$this->AddNote("This is a mulipart alternative email");
463
$this->Mail->Subject .= ": AltBody + Word Wrap";
466
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
470
* Simple HTML and attachment test
472
function test_AltBody_Attachment() {
474
$this->Mail->Body = "This is the <b>HTML</b> part of the email.";
475
$this->Mail->AltBody = "This is the text part of the email.";
476
$this->Mail->Subject .= ": AltBody + Attachment";
477
$this->Mail->IsHTML(true);
479
if(!$this->Mail->AddAttachment(__FILE__, "test_attach.txt"))
481
$this->assertTrue(false, $this->Mail->ErrorInfo);
486
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
487
if (is_writable('.')) {
488
file_put_contents('message.txt', $this->Mail->CreateHeader() . $this->Mail->CreateBody());
490
$this->assertTrue(false, 'Could not write local file - check permissions');
494
function test_MultipleSend() {
495
$this->Mail->Body = "Sending two messages without keepalive";
497
$subject = $this->Mail->Subject;
499
$this->Mail->Subject = $subject . ": SMTP 1";
500
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
502
$this->Mail->Subject = $subject . ": SMTP 2";
503
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
506
function test_SendmailSend() {
507
$this->Mail->Body = "Sending via sendmail";
509
$subject = $this->Mail->Subject;
511
$this->Mail->Subject = $subject . ": sendmail";
512
$this->Mail->IsSendmail();
513
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
516
function test_MailSend() {
517
$this->Mail->Body = "Sending via mail()";
519
$subject = $this->Mail->Subject;
521
$this->Mail->Subject = $subject . ": mail()";
522
$this->Mail->IsMail();
523
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
526
function test_SmtpKeepAlive() {
527
$this->Mail->Body = "This was done using the SMTP keep-alive.";
529
$subject = $this->Mail->Subject;
531
$this->Mail->SMTPKeepAlive = true;
532
$this->Mail->Subject = $subject . ": SMTP keep-alive 1";
533
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
535
$this->Mail->Subject = $subject . ": SMTP keep-alive 2";
536
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
537
$this->Mail->SmtpClose();
541
* Tests this denial of service attack:
542
* http://www.cybsec.com/vuln/PHPMailer-DOS.pdf
544
function test_DenialOfServiceAttack() {
545
$this->Mail->Body = "This should no longer cause a denial of service.";
548
$this->Mail->Subject = str_repeat("A", 998);
549
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
552
function test_Error() {
553
$this->Mail->Subject .= ": This should be sent";
555
$this->Mail->ClearAllRecipients(); // no addresses should cause an error
556
$this->assertTrue($this->Mail->IsError() == false, "Error found");
557
$this->assertTrue($this->Mail->Send() == false, "Send succeeded");
558
$this->assertTrue($this->Mail->IsError(), "No error found");
559
$this->assertEquals('You must provide at least one recipient email address.', $this->Mail->ErrorInfo);
560
$this->Mail->AddAddress($_REQUEST['mail_to']);
561
$this->assertTrue($this->Mail->Send(), "Send failed");
564
function test_Addressing() {
565
$this->assertFalse($this->Mail->AddAddress('a@example..com'), 'Invalid address accepted');
566
$this->assertTrue($this->Mail->AddAddress('a@example.com'), 'Addressing failed');
567
$this->assertFalse($this->Mail->AddAddress('a@example.com'), 'Duplicate addressing failed');
568
$this->assertTrue($this->Mail->AddCC('b@example.com'), 'CC addressing failed');
569
$this->assertFalse($this->Mail->AddCC('b@example.com'), 'CC duplicate addressing failed');
570
$this->assertFalse($this->Mail->AddCC('a@example.com'), 'CC duplicate addressing failed (2)');
571
$this->assertTrue($this->Mail->AddBCC('c@example.com'), 'BCC addressing failed');
572
$this->assertFalse($this->Mail->AddBCC('c@example.com'), 'BCC duplicate addressing failed');
573
$this->assertFalse($this->Mail->AddBCC('a@example.com'), 'BCC duplicate addressing failed (2)');
574
$this->assertTrue($this->Mail->AddReplyTo('a@example.com'), 'Replyto Addressing failed');
575
$this->assertFalse($this->Mail->AddReplyTo('a@example..com'), 'Invalid Replyto address accepted');
576
$this->Mail->ClearAddresses();
577
$this->Mail->ClearCCs();
578
$this->Mail->ClearBCCs();
579
$this->Mail->ClearReplyTos();
583
* Test language files for missing and excess translations
584
* All languages are compared with English
586
function test_Translations() {
587
$this->Mail->SetLanguage('en');
588
$definedStrings = $this->Mail->GetTranslations();
589
foreach (new DirectoryIterator('../language') as $fileInfo) {
590
if($fileInfo->isDot()) continue;
592
//Only look at language files, ignore anything else in there
593
if (preg_match('/^phpmailer\.lang-([a-z_]{2,})\.php$/', $fileInfo->getFilename(), $matches)) {
594
$lang = $matches[1]; //Extract language code
595
$PHPMAILER_LANG = array(); //Language strings get put in here
596
include $fileInfo->getPathname(); //Get language strings
597
$missing = array_diff(array_keys($definedStrings), array_keys($PHPMAILER_LANG));
598
$extra = array_diff(array_keys($PHPMAILER_LANG), array_keys($definedStrings));
599
$this->assertTrue(empty($missing), "Missing translations in $lang: ". implode(', ', $missing));
600
$this->assertTrue(empty($extra), "Extra translations in $lang: ". implode(', ', $extra));
608
function test_Encodings() {
609
$this->Mail->Charset = 'iso-8859-1';
610
$this->assertEquals('=A1Hola!_Se=F1or!', $this->Mail->EncodeQ('�Hola! Se�or!', 'text'), 'Q Encoding (text) failed');
611
$this->assertEquals('=A1Hola!_Se=F1or!', $this->Mail->EncodeQ('�Hola! Se�or!', 'comment'), 'Q Encoding (comment) failed');
612
$this->assertEquals('=A1Hola!_Se=F1or!', $this->Mail->EncodeQ('�Hola! Se�or!', 'phrase'), 'Q Encoding (phrase) failed');
618
function test_Signing() {
619
$this->Mail->Sign('certfile.txt', 'keyfile.txt', 'password'); //TODO this is not really testing signing, but at least helps coverage
623
* Miscellaneous calls to improve test coverage and some small tests
625
function test_Miscellaneous() {
626
$this->assertEquals('application/pdf', PHPMailer::_mime_types('pdf') , 'MIME TYPE lookup failed');
627
$this->Mail->AddCustomHeader('SomeHeader: Some Value');
628
$this->Mail->ClearCustomHeaders();
629
$this->Mail->ClearAttachments();
630
$this->Mail->IsHTML(false);
631
$this->Mail->IsSMTP();
632
$this->Mail->IsMail();
633
$this->Mail->IsSendMail();
634
$this->Mail->IsQmail();
635
$this->Mail->SetLanguage('fr');
636
$this->Mail->Sender = '';
637
$this->Mail->CreateHeader();
638
$this->assertFalse($this->Mail->set('x', 'y'), 'Invalid property set succeeded');
639
$this->assertTrue($this->Mail->set('Timeout', 11), 'Valid property set failed');
640
$this->Mail->getFile(__FILE__);
645
* This is a sample form for setting appropriate test values through a browser
646
* These values can also be set using a file called testbootstrap.php (not in svn) in the same folder as this script
647
* which is probably more useful if you run these tests a lot
650
<h3>phpmailer Unit Test</h3>
651
By entering a SMTP hostname it will automatically perform tests with SMTP.
653
<form name="phpmailer_unit" action=__FILE__ method="get">
654
<input type="hidden" name="submitted" value="1"/>
655
From Address: <input type="text" size="50" name="mail_from" value="<?php echo get("mail_from"); ?>"/>
657
To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>
659
Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>
661
SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>
663
<input type="submit" value="Run Test"/>
b'\\ No newline at end of file'