2008-08-07 14:41:59 +02:00
< ? php
2009-05-05 13:22:26 +02:00
/**
2012-11-21 16:12:07 +01:00
* PHPMailer - PHP email transport unit tests
2013-12-16 14:28:57 +01:00
* Requires PHPUnit 3.3 or later .
2013-07-09 13:46:44 +02:00
*
* PHP version 5.0 . 0
*
2012-11-21 16:12:07 +01:00
* @ package PHPMailer
* @ author Andy Prevost
2013-07-09 13:46:44 +02:00
* @ author Marcus Bointon < phpmailer @ synchromedia . co . uk >
2012-11-21 16:12:07 +01:00
* @ copyright 2004 - 2009 Andy Prevost
2013-12-16 14:28:57 +01:00
* @ copyright 2010 Marcus Bointon
2012-11-21 16:12:07 +01:00
* @ license http :// www . gnu . org / copyleft / lesser . html GNU Lesser General Public License
*/
2016-03-29 15:14:19 +02:00
require_once realpath ( '../PHPMailerAutoload.php' );
2008-08-07 14:41:59 +02:00
/**
2012-11-21 16:12:07 +01:00
* PHPMailer - PHP email transport unit test class
* Performs authentication tests
*/
2013-07-09 13:46:44 +02:00
class PHPMailerTest extends PHPUnit_Framework_TestCase
2012-11-21 16:12:07 +01:00
{
2008-08-07 14:41:59 +02:00
/**
* Holds the default phpmailer instance .
* @ private
2015-09-28 11:35:42 +02:00
* @ var PHPMailer
2008-08-07 14:41:59 +02:00
*/
2012-11-21 16:12:07 +01:00
public $Mail ;
2008-08-07 14:41:59 +02:00
/**
* Holds the SMTP mail host .
* @ public
2015-09-28 11:35:42 +02:00
* @ var string
2008-08-07 14:41:59 +02:00
*/
2013-03-06 17:34:28 +01:00
public $Host = '' ;
2012-11-21 16:12:07 +01:00
2008-08-07 14:41:59 +02:00
/**
* Holds the change log .
* @ private
2015-09-28 11:35:42 +02:00
* @ var string []
2008-08-07 14:41:59 +02:00
*/
2012-11-21 16:12:07 +01:00
public $ChangeLog = array ();
/**
2008-08-07 14:41:59 +02:00
* Holds the note log .
* @ private
2015-09-28 11:35:42 +02:00
* @ var string []
2008-08-07 14:41:59 +02:00
*/
2012-11-21 16:12:07 +01:00
public $NoteLog = array ();
2008-08-07 14:41:59 +02:00
2013-08-02 16:19:01 +02:00
/**
* PIDs of any processes we need to kill
2015-09-28 11:35:42 +02:00
* @ var array
2013-08-02 16:19:01 +02:00
* @ access private
*/
private $pids = array ();
2016-03-29 15:14:19 +02:00
/**
* Default include path
* @ var string
*/
const INCLUDE_DIR = '../' ;
2012-11-21 16:12:07 +01:00
/**
* Run before each test is started .
*/
2013-07-09 13:46:44 +02:00
public function setUp ()
2012-11-21 16:12:07 +01:00
{
2016-03-29 15:14:19 +02:00
if ( file_exists ( 'testbootstrap.php' )) {
include 'testbootstrap.php' ; //Overrides go in here
2012-11-21 16:12:07 +01:00
}
$this -> Mail = new PHPMailer ;
2014-12-06 15:07:54 +01:00
$this -> Mail -> SMTPDebug = 3 ; //Full debug output
2008-08-07 14:41:59 +02:00
$this -> Mail -> Priority = 3 ;
2013-03-06 17:34:28 +01:00
$this -> Mail -> Encoding = '8bit' ;
$this -> Mail -> CharSet = 'iso-8859-1' ;
2009-05-05 13:22:26 +02:00
if ( array_key_exists ( 'mail_from' , $_REQUEST )) {
2012-11-21 16:12:07 +01:00
$this -> Mail -> From = $_REQUEST [ 'mail_from' ];
} else {
$this -> Mail -> From = 'unit_test@phpmailer.example.com' ;
}
2013-03-06 17:34:28 +01:00
$this -> Mail -> FromName = 'Unit Tester' ;
$this -> Mail -> Sender = '' ;
$this -> Mail -> Subject = 'Unit Test' ;
$this -> Mail -> Body = '' ;
$this -> Mail -> AltBody = '' ;
2008-08-07 14:41:59 +02:00
$this -> Mail -> WordWrap = 0 ;
2009-05-05 13:22:26 +02:00
if ( array_key_exists ( 'mail_host' , $_REQUEST )) {
2012-11-21 16:12:07 +01:00
$this -> Mail -> Host = $_REQUEST [ 'mail_host' ];
} else {
$this -> Mail -> Host = 'mail.example.com' ;
}
if ( array_key_exists ( 'mail_port' , $_REQUEST )) {
$this -> Mail -> Port = $_REQUEST [ 'mail_port' ];
} else {
$this -> Mail -> Port = 25 ;
}
2013-03-06 17:34:28 +01:00
$this -> Mail -> Helo = 'localhost.localdomain' ;
2008-08-07 14:41:59 +02:00
$this -> Mail -> SMTPAuth = false ;
2013-03-06 17:34:28 +01:00
$this -> Mail -> Username = '' ;
$this -> Mail -> Password = '' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> addReplyTo ( 'no_reply@phpmailer.example.com' , 'Reply Guy' );
2013-03-06 17:34:28 +01:00
$this -> Mail -> Sender = 'unit_test@phpmailer.example.com' ;
2012-11-21 16:12:07 +01:00
if ( strlen ( $this -> Mail -> Host ) > 0 ) {
2013-03-06 17:34:28 +01:00
$this -> Mail -> Mailer = 'smtp' ;
2009-05-05 13:22:26 +02:00
} else {
2013-03-06 17:34:28 +01:00
$this -> Mail -> Mailer = 'mail' ;
2008-08-07 14:41:59 +02:00
}
2009-05-05 13:22:26 +02:00
if ( array_key_exists ( 'mail_to' , $_REQUEST )) {
2013-07-09 13:46:44 +02:00
$this -> setAddress ( $_REQUEST [ 'mail_to' ], 'Test User' , 'to' );
2012-11-21 16:12:07 +01:00
}
2009-05-05 13:22:26 +02:00
if ( array_key_exists ( 'mail_cc' , $_REQUEST ) and strlen ( $_REQUEST [ 'mail_cc' ]) > 0 ) {
2013-07-09 13:46:44 +02:00
$this -> setAddress ( $_REQUEST [ 'mail_cc' ], 'Carbon User' , 'cc' );
2012-11-21 16:12:07 +01:00
}
}
2008-08-07 14:41:59 +02:00
/**
* Run after each test is completed .
*/
2013-07-09 13:46:44 +02:00
public function tearDown ()
2012-11-21 16:12:07 +01:00
{
2008-08-07 14:41:59 +02:00
// Clean global variables
2012-11-21 16:12:07 +01:00
$this -> Mail = null ;
2008-08-07 14:41:59 +02:00
$this -> ChangeLog = array ();
$this -> NoteLog = array ();
2013-08-02 16:19:01 +02:00
foreach ( $this -> pids as $pid ) {
$p = escapeshellarg ( $pid );
shell_exec ( " ps $p && kill -TERM $p " );
}
2008-08-07 14:41:59 +02:00
}
/**
* Build the body of the message in the appropriate format .
2013-06-06 17:31:11 +02:00
*
2008-08-07 14:41:59 +02:00
* @ private
2013-06-06 17:31:11 +02:00
* @ return void
2008-08-07 14:41:59 +02:00
*/
2013-07-09 13:46:44 +02:00
public function buildBody ()
2012-11-21 16:12:07 +01:00
{
2013-07-09 13:46:44 +02:00
$this -> checkChanges ();
2012-11-21 16:12:07 +01:00
// Determine line endings for message
2013-03-06 17:34:28 +01:00
if ( $this -> Mail -> ContentType == 'text/html' || strlen ( $this -> Mail -> AltBody ) > 0 ) {
2015-03-18 08:49:42 +01:00
$eol = " <br> \r \n " ;
2015-03-17 18:55:11 +01:00
$bullet_start = '<li>' ;
$bullet_end = " </li> \r \n " ;
2015-03-25 19:22:55 +01:00
$list_start = " <ul> \r \n " ;
2015-03-17 18:55:11 +01:00
$list_end = " </ul> \r \n " ;
2012-11-21 16:12:07 +01:00
} else {
2015-03-17 18:55:11 +01:00
$eol = " \r \n " ;
$bullet_start = ' - ' ;
2015-03-20 12:03:16 +01:00
$bullet_end = " \r \n " ;
2015-03-17 18:55:11 +01:00
$list_start = '' ;
$list_end = '' ;
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
2013-03-06 17:34:28 +01:00
$ReportBody = '' ;
2012-11-21 16:12:07 +01:00
2013-03-06 17:34:28 +01:00
$ReportBody .= '---------------------' . $eol ;
$ReportBody .= 'Unit Test Information' . $eol ;
$ReportBody .= '---------------------' . $eol ;
$ReportBody .= 'phpmailer version: ' . $this -> Mail -> Version . $eol ;
$ReportBody .= 'Content Type: ' . $this -> Mail -> ContentType . $eol ;
2015-03-25 19:22:55 +01:00
$ReportBody .= 'CharSet: ' . $this -> Mail -> CharSet . $eol ;
2012-11-21 16:12:07 +01:00
if ( strlen ( $this -> Mail -> Host ) > 0 ) {
2013-03-06 17:34:28 +01:00
$ReportBody .= 'Host: ' . $this -> Mail -> Host . $eol ;
2012-11-21 16:12:07 +01:00
}
2008-08-07 14:41:59 +02:00
// If attachments then create an attachment list
2013-07-09 13:46:44 +02:00
$attachments = $this -> Mail -> getAttachments ();
2012-11-21 16:12:07 +01:00
if ( count ( $attachments ) > 0 ) {
2013-03-06 17:34:28 +01:00
$ReportBody .= 'Attachments:' . $eol ;
2015-03-17 18:55:11 +01:00
$ReportBody .= $list_start ;
2012-11-21 16:12:07 +01:00
foreach ( $attachments as $attachment ) {
2015-03-17 18:55:11 +01:00
$ReportBody .= $bullet_start . 'Name: ' . $attachment [ 1 ] . ', ' ;
2013-03-06 17:34:28 +01:00
$ReportBody .= 'Encoding: ' . $attachment [ 3 ] . ', ' ;
2015-03-17 18:55:11 +01:00
$ReportBody .= 'Type: ' . $attachment [ 4 ] . $bullet_end ;
2008-08-07 14:41:59 +02:00
}
2015-03-17 18:55:11 +01:00
$ReportBody .= $list_end . $eol ;
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
2008-08-07 14:41:59 +02:00
// If there are changes then list them
2012-11-21 16:12:07 +01:00
if ( count ( $this -> ChangeLog ) > 0 ) {
2013-03-06 17:34:28 +01:00
$ReportBody .= 'Changes' . $eol ;
$ReportBody .= '-------' . $eol ;
2008-08-07 14:41:59 +02:00
2015-03-17 18:55:11 +01:00
$ReportBody .= $list_start ;
2012-11-21 16:12:07 +01:00
for ( $i = 0 ; $i < count ( $this -> ChangeLog ); $i ++ ) {
2015-03-17 18:55:11 +01:00
$ReportBody .= $bullet_start . $this -> ChangeLog [ $i ][ 0 ] . ' was changed to [' .
2015-03-17 19:00:07 +01:00
$this -> ChangeLog [ $i ][ 1 ] . ']' . $bullet_end ;
2008-08-07 14:41:59 +02:00
}
2015-03-17 18:55:11 +01:00
$ReportBody .= $list_end . $eol . $eol ;
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
2008-08-07 14:41:59 +02:00
// If there are notes then list them
2012-11-21 16:12:07 +01:00
if ( count ( $this -> NoteLog ) > 0 ) {
2013-03-06 17:34:28 +01:00
$ReportBody .= 'Notes' . $eol ;
$ReportBody .= '-----' . $eol ;
2008-08-07 14:41:59 +02:00
2015-03-17 18:55:11 +01:00
$ReportBody .= $list_start ;
2012-11-21 16:12:07 +01:00
for ( $i = 0 ; $i < count ( $this -> NoteLog ); $i ++ ) {
2015-03-17 19:00:07 +01:00
$ReportBody .= $bullet_start . $this -> NoteLog [ $i ] . $bullet_end ;
2008-08-07 14:41:59 +02:00
}
2015-03-17 18:55:11 +01:00
$ReportBody .= $list_end ;
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
2008-08-07 14:41:59 +02:00
// Re-attach the original body
2015-03-25 19:22:55 +01:00
$this -> Mail -> Body .= $eol . $ReportBody ;
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
2008-08-07 14:41:59 +02:00
/**
* Check which default settings have been changed for the report .
* @ private
2013-06-06 17:31:11 +02:00
* @ return void
2008-08-07 14:41:59 +02:00
*/
2013-07-09 13:46:44 +02:00
public function checkChanges ()
2012-11-21 16:12:07 +01:00
{
if ( $this -> Mail -> Priority != 3 ) {
2013-07-09 13:46:44 +02:00
$this -> addChange ( 'Priority' , $this -> Mail -> Priority );
2012-11-21 16:12:07 +01:00
}
2013-03-06 17:34:28 +01:00
if ( $this -> Mail -> Encoding != '8bit' ) {
2013-07-09 13:46:44 +02:00
$this -> addChange ( 'Encoding' , $this -> Mail -> Encoding );
2012-11-21 16:12:07 +01:00
}
2013-03-06 17:34:28 +01:00
if ( $this -> Mail -> CharSet != 'iso-8859-1' ) {
2013-07-09 13:46:44 +02:00
$this -> addChange ( 'CharSet' , $this -> Mail -> CharSet );
2012-11-21 16:12:07 +01:00
}
2013-03-06 17:34:28 +01:00
if ( $this -> Mail -> Sender != '' ) {
2013-07-09 13:46:44 +02:00
$this -> addChange ( 'Sender' , $this -> Mail -> Sender );
2012-11-21 16:12:07 +01:00
}
if ( $this -> Mail -> WordWrap != 0 ) {
2013-07-09 13:46:44 +02:00
$this -> addChange ( 'WordWrap' , $this -> Mail -> WordWrap );
2012-11-21 16:12:07 +01:00
}
2013-03-06 17:34:28 +01:00
if ( $this -> Mail -> Mailer != 'mail' ) {
2013-07-09 13:46:44 +02:00
$this -> addChange ( 'Mailer' , $this -> Mail -> Mailer );
2012-11-21 16:12:07 +01:00
}
if ( $this -> Mail -> Port != 25 ) {
2013-07-09 13:46:44 +02:00
$this -> addChange ( 'Port' , $this -> Mail -> Port );
2012-11-21 16:12:07 +01:00
}
2013-03-06 17:34:28 +01:00
if ( $this -> Mail -> Helo != 'localhost.localdomain' ) {
2013-07-09 13:46:44 +02:00
$this -> addChange ( 'Helo' , $this -> Mail -> Helo );
2012-11-21 16:12:07 +01:00
}
if ( $this -> Mail -> SMTPAuth ) {
2013-07-09 13:46:44 +02:00
$this -> addChange ( 'SMTPAuth' , 'true' );
2012-11-21 16:12:07 +01:00
}
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
2008-08-07 14:41:59 +02:00
/**
2012-11-21 16:12:07 +01:00
* Add a changelog entry .
* @ access private
* @ param string $sName
* @ param string $sNewValue
* @ return void
2008-08-07 14:41:59 +02:00
*/
2013-07-09 13:46:44 +02:00
public function addChange ( $sName , $sNewValue )
2012-11-21 16:12:07 +01:00
{
$this -> ChangeLog [] = array ( $sName , $sNewValue );
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
2008-08-07 14:41:59 +02:00
/**
* Adds a simple note to the message .
* @ public
2012-11-21 16:12:07 +01:00
* @ param string $sValue
* @ return void
2008-08-07 14:41:59 +02:00
*/
2013-07-09 13:46:44 +02:00
public function addNote ( $sValue )
2012-11-21 16:12:07 +01:00
{
2008-08-07 14:41:59 +02:00
$this -> NoteLog [] = $sValue ;
}
/**
* Adds all of the addresses
2012-11-21 16:12:07 +01:00
* @ access public
* @ param string $sAddress
* @ param string $sName
* @ param string $sType
* @ return boolean
2008-08-07 14:41:59 +02:00
*/
2013-07-09 13:46:44 +02:00
public function setAddress ( $sAddress , $sName = '' , $sType = 'to' )
2012-11-21 16:12:07 +01:00
{
switch ( $sType ) {
case 'to' :
2013-07-09 13:46:44 +02:00
return $this -> Mail -> addAddress ( $sAddress , $sName );
2012-11-21 16:12:07 +01:00
case 'cc' :
2013-07-09 13:46:44 +02:00
return $this -> Mail -> addCC ( $sAddress , $sName );
2013-03-06 17:34:28 +01:00
case 'bcc' :
2013-07-09 13:46:44 +02:00
return $this -> Mail -> addBCC ( $sAddress , $sName );
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
return false ;
2008-08-07 14:41:59 +02:00
}
2014-06-05 16:58:17 +02:00
/**
* Check that we have loaded default test params .
* Pretty much everything will fail due to unset recipient if this is not done .
*/
public function testBootstrap ()
{
$this -> assertTrue (
2016-03-29 15:14:19 +02:00
file_exists ( 'testbootstrap.php' ),
2014-06-05 16:58:17 +02:00
'Test config params missing - copy testbootstrap.php to testbootstrap-dist.php and change as appropriate'
);
}
2012-12-04 12:15:39 +01:00
/**
2015-03-04 10:29:46 +01:00
* Test CRAM - MD5 authentication .
2012-12-04 12:15:39 +01:00
* Needs a connection to a server that supports this auth mechanism , so commented out by default
*/
2013-07-09 13:46:44 +02:00
public function testAuthCRAMMD5 ()
2012-12-04 12:15:39 +01:00
{
$this -> Mail -> Host = 'hostname' ;
$this -> Mail -> Port = 587 ;
$this -> Mail -> SMTPAuth = true ;
$this -> Mail -> SMTPSecure = 'tls' ;
$this -> Mail -> AuthType = 'CRAM-MD5' ;
$this -> Mail -> Username = 'username' ;
$this -> Mail -> Password = 'password' ;
$this -> Mail -> Body = 'Test body' ;
2013-03-06 17:34:28 +01:00
$this -> Mail -> Subject .= ': Auth CRAM-MD5' ;
2012-12-04 12:15:39 +01:00
$this -> Mail -> From = 'from@example.com' ;
$this -> Mail -> Sender = 'from@example.com' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> clearAllRecipients ();
$this -> Mail -> addAddress ( 'user@example.com' );
//$this->assertTrue($this->mail->send(), $this->mail->ErrorInfo);
2012-12-04 12:15:39 +01:00
}
/**
2015-03-04 10:29:46 +01:00
* Test email address validation .
2012-12-04 12:15:39 +01:00
* Test addresses obtained from http :// isemail . info
2012-12-04 17:35:58 +01:00
* Some failing cases commented out that are apparently up for debate !
2012-12-04 12:15:39 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testValidate ()
2012-12-04 12:15:39 +01:00
{
$validaddresses = array (
'first@iana.org' ,
'first.last@iana.org' ,
'1234567890123456789012345678901234567890123456789012345678901234@iana.org' ,
'"first\"last"@iana.org' ,
'"first@last"@iana.org' ,
'"first\last"@iana.org' ,
'first.last@[12.34.56.78]' ,
'first.last@[IPv6:::12.34.56.78]' ,
2012-12-04 17:35:58 +01:00
'first.last@[IPv6:1111:2222:3333::4444:12.34.56.78]' ,
2012-12-04 12:15:39 +01:00
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.56.78]' ,
'first.last@[IPv6:::1111:2222:3333:4444:5555:6666]' ,
'first.last@[IPv6:1111:2222:3333::4444:5555:6666]' ,
'first.last@[IPv6:1111:2222:3333:4444:5555:6666::]' ,
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888]' ,
'first.last@x23456789012345678901234567890123456789012345678901234567890123.iana.org' ,
'first.last@3com.com' ,
'first.last@123.iana.org' ,
'"first\last"@iana.org' ,
2012-12-04 17:35:58 +01:00
'first.last@[IPv6:1111:2222:3333::4444:5555:12.34.56.78]' ,
'first.last@[IPv6:1111:2222:3333::4444:5555:6666:7777]' ,
2012-12-04 12:15:39 +01:00
'first.last@example.123' ,
'first.last@com' ,
'"Abc\@def"@iana.org' ,
'"Fred\ Bloggs"@iana.org' ,
'"Joe.\Blow"@iana.org' ,
'"Abc@def"@iana.org' ,
'user+mailbox@iana.org' ,
'customer/department=shipping@iana.org' ,
'$A12345@iana.org' ,
'!def!xyz%abc@iana.org' ,
'_somename@iana.org' ,
'dclo@us.ibm.com' ,
'peter.piper@iana.org' ,
'"Doug \"Ace\" L."@iana.org' ,
'test@iana.org' ,
'TEST@iana.org' ,
'1234567890@iana.org' ,
'test+test@iana.org' ,
'test-test@iana.org' ,
't*est@iana.org' ,
'+1~1+@iana.org' ,
'{_test_}@iana.org' ,
'"[[ test ]]"@iana.org' ,
'test.test@iana.org' ,
'"test.test"@iana.org' ,
'test."test"@iana.org' ,
'"test@test"@iana.org' ,
'test@123.123.123.x123' ,
'test@123.123.123.123' ,
'test@[123.123.123.123]' ,
'test@example.iana.org' ,
'test@example.example.iana.org' ,
'"test\test"@iana.org' ,
'test@example' ,
'"test\blah"@iana.org' ,
'"test\blah"@iana.org' ,
'"test\"blah"@iana.org' ,
'customer/department@iana.org' ,
'_Yosemite.Sam@iana.org' ,
'~@iana.org' ,
'"Austin@Powers"@iana.org' ,
'Ima.Fool@iana.org' ,
'"Ima.Fool"@iana.org' ,
'"Ima Fool"@iana.org' ,
'"first"."last"@iana.org' ,
'"first".middle."last"@iana.org' ,
'"first".last@iana.org' ,
'first."last"@iana.org' ,
'"first"."middle"."last"@iana.org' ,
'"first.middle"."last"@iana.org' ,
'"first.middle.last"@iana.org' ,
'"first..last"@iana.org' ,
2012-12-04 17:35:58 +01:00
'"first\"last"@iana.org' ,
2012-12-04 12:15:39 +01:00
'first."mid\dle"."last"@iana.org' ,
'"test blah"@iana.org' ,
2012-12-04 17:35:58 +01:00
'(foo)cal(bar)@(baz)iamcal.com(quux)' ,
'cal@iamcal(woo).(yay)com' ,
'cal(woo(yay)hoopla)@iamcal.com' ,
'cal(foo\@bar)@iamcal.com' ,
'cal(foo\)bar)@iamcal.com' ,
'first().last@iana.org' ,
'pete(his account)@silly.test(his host)' ,
'c@(Chris\'s host.)public.example' ,
'jdoe@machine(comment). example' ,
'1234 @ local(blah) .machine .example' ,
2012-12-04 12:15:39 +01:00
'first(abc.def).last@iana.org' ,
'first(a"bc.def).last@iana.org' ,
'first.(")middle.last(")@iana.org' ,
'first(abc\(def)@iana.org' ,
'first.last@x(1234567890123456789012345678901234567890123456789012345678901234567890).com' ,
'a(a(b(c)d(e(f))g)h(i)j)@iana.org' ,
'name.lastname@domain.com' ,
'a@b' ,
'a@bar.com' ,
'aaa@[123.123.123.123]' ,
'a@bar' ,
'a-b@bar.com' ,
'+@b.c' ,
'+@b.com' ,
'a@b.co-foo.uk' ,
'"hello my name is"@stutter.com' ,
'"Test \"Fail\" Ing"@iana.org' ,
'valid@about.museum' ,
'shaitan@my-domain.thisisminekthx' ,
'foobar@192.168.0.1' ,
'"Joe\Blow"@iana.org' ,
'HM2Kinsists@(that comments are allowed)this.is.ok' ,
'user%uucp!path@berkeley.edu' ,
'first.last @iana.org' ,
'cdburgess+!#$%&\'*-/=?+_{}|~test@gmail.com' ,
2012-12-04 17:35:58 +01:00
'first.last@[IPv6:::a2:a3:a4:b1:b2:b3:b4]' ,
'first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3::]' ,
2012-12-04 12:15:39 +01:00
'first.last@[IPv6:::]' ,
'first.last@[IPv6:::b4]' ,
'first.last@[IPv6:::b3:b4]' ,
'first.last@[IPv6:a1::b4]' ,
'first.last@[IPv6:a1::]' ,
'first.last@[IPv6:a1:a2::]' ,
'first.last@[IPv6:0123:4567:89ab:cdef::]' ,
'first.last@[IPv6:0123:4567:89ab:CDEF::]' ,
'first.last@[IPv6:::a3:a4:b1:ffff:11.22.33.44]' ,
2012-12-04 17:35:58 +01:00
'first.last@[IPv6:::a2:a3:a4:b1:ffff:11.22.33.44]' ,
2012-12-04 12:15:39 +01:00
'first.last@[IPv6:a1:a2:a3:a4::11.22.33.44]' ,
2012-12-04 17:35:58 +01:00
'first.last@[IPv6:a1:a2:a3:a4:b1::11.22.33.44]' ,
2012-12-04 12:15:39 +01:00
'first.last@[IPv6:a1::11.22.33.44]' ,
'first.last@[IPv6:a1:a2::11.22.33.44]' ,
'first.last@[IPv6:0123:4567:89ab:cdef::11.22.33.44]' ,
'first.last@[IPv6:0123:4567:89ab:CDEF::11.22.33.44]' ,
'first.last@[IPv6:a1::b2:11.22.33.44]' ,
'test@test.com' ,
'test@xn--example.com' ,
2015-11-01 11:12:04 +01:00
'test@example.com'
2012-12-04 12:15:39 +01:00
);
$invalidaddresses = array (
'first.last@sub.do,com' ,
'first\@last@iana.org' ,
2013-07-09 13:46:44 +02:00
'123456789012345678901234567890123456789012345678901234567890' .
'@12345678901234567890123456789012345678901234 [...]' ,
2012-12-04 12:15:39 +01:00
'first.last' ,
'12345678901234567890123456789012345678901234567890123456789012345@iana.org' ,
'.first.last@iana.org' ,
'first.last.@iana.org' ,
'first..last@iana.org' ,
'"first"last"@iana.org' ,
2012-12-04 17:35:58 +01:00
'"""@iana.org' ,
2012-12-04 12:15:39 +01:00
'"\"@iana.org' ,
2013-07-09 13:46:44 +02:00
//'""@iana.org',
2012-12-04 12:15:39 +01:00
'first\@last@iana.org' ,
'first.last@' ,
2013-07-09 13:46:44 +02:00
'x@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.' .
'x23456789.x23456789.x23456789.x23 [...]' ,
2012-12-04 12:15:39 +01:00
'first.last@[.12.34.56.78]' ,
'first.last@[12.34.56.789]' ,
'first.last@[::12.34.56.78]' ,
'first.last@[IPv5:::12.34.56.78]' ,
'first.last@[IPv6:1111:2222:3333:4444:5555:12.34.56.78]' ,
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:12.34.56.78]' ,
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777]' ,
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888:9999]' ,
'first.last@[IPv6:1111:2222::3333::4444:5555:6666]' ,
'first.last@[IPv6:1111:2222:333x::4444:5555]' ,
'first.last@[IPv6:1111:2222:33333::4444:5555]' ,
'first.last@-xample.com' ,
'first.last@exampl-.com' ,
'first.last@x234567890123456789012345678901234567890123456789012345678901234.iana.org' ,
'abc\@def@iana.org' ,
'abc\@iana.org' ,
'Doug\ \"Ace\"\ Lovell@iana.org' ,
'abc@def@iana.org' ,
'abc\@def@iana.org' ,
'abc\@iana.org' ,
'@iana.org' ,
'doug@' ,
'"qu@iana.org' ,
'ote"@iana.org' ,
'.dot@iana.org' ,
'dot.@iana.org' ,
'two..dot@iana.org' ,
'"Doug "Ace" L."@iana.org' ,
'Doug\ \"Ace\"\ L\.@iana.org' ,
'hello world@iana.org' ,
2013-07-09 13:46:44 +02:00
//'helloworld@iana .org',
2012-12-04 12:15:39 +01:00
'gatsby@f.sc.ot.t.f.i.tzg.era.l.d.' ,
'test.iana.org' ,
'test.@iana.org' ,
'test..test@iana.org' ,
'.test@iana.org' ,
'test@test@iana.org' ,
'test@@iana.org' ,
'-- test --@iana.org' ,
'[test]@iana.org' ,
'"test"test"@iana.org' ,
'()[]\;:,><@iana.org' ,
'test@.' ,
'test@example.' ,
'test@.org' ,
2013-07-09 13:46:44 +02:00
'test@12345678901234567890123456789012345678901234567890123456789012345678901234567890' .
'12345678901234567890 [...]' ,
2012-12-04 12:15:39 +01:00
'test@[123.123.123.123' ,
'test@123.123.123.123]' ,
'NotAnEmail' ,
'@NotAnEmail' ,
'"test"blah"@iana.org' ,
'.wooly@iana.org' ,
'wo..oly@iana.org' ,
'pootietang.@iana.org' ,
'.@iana.org' ,
'Ima Fool@iana.org' ,
'phil.h\@\@ck@haacked.com' ,
'foo@[\1.2.3.4]' ,
2013-07-09 13:46:44 +02:00
//'first."".last@iana.org',
2012-12-04 12:15:39 +01:00
'first\last@iana.org' ,
'Abc\@def@iana.org' ,
'Fred\ Bloggs@iana.org' ,
'Joe.\Blow@iana.org' ,
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.567.89]' ,
'{^c\@**Dog^}@cartoon.com' ,
2013-07-09 13:46:44 +02:00
//'"foo"(yay)@(hoopla)[1.2.3.4]',
2012-12-04 12:15:39 +01:00
'cal(foo(bar)@iamcal.com' ,
'cal(foo)bar)@iamcal.com' ,
'cal(foo\)@iamcal.com' ,
2013-07-09 13:46:44 +02:00
'first(12345678901234567890123456789012345678901234567890)last@(1234567890123456789' .
'01234567890123456789012 [...]' ,
2012-12-04 12:15:39 +01:00
'first(middle)last@iana.org' ,
2013-07-09 13:46:44 +02:00
'first(abc("def".ghi).mno)middle(abc("def".ghi).mno).last@(abc("def".ghi).mno)example' .
'(abc("def".ghi).mno). [...]' ,
2012-12-04 12:15:39 +01:00
'a(a(b(c)d(e(f))g)(h(i)j)@iana.org' ,
'.@' ,
'@bar.com' ,
'@@bar.com' ,
'aaa.com' ,
'aaa@.com' ,
'aaa@.123' ,
'aaa@[123.123.123.123]a' ,
'aaa@[123.123.123.333]' ,
'a@bar.com.' ,
'a@-b.com' ,
'a@b-.com' ,
'-@..com' ,
'-@a..com' ,
'invalid@about.museum-' ,
'test@...........com' ,
2012-12-04 17:35:58 +01:00
'"Unicode NULL' . chr ( 0 ) . '"@char.com' ,
'Unicode NULL' . chr ( 0 ) . '@char.com' ,
2012-12-04 12:15:39 +01:00
'first.last@[IPv6::]' ,
'first.last@[IPv6::::]' ,
'first.last@[IPv6::b4]' ,
'first.last@[IPv6::::b4]' ,
'first.last@[IPv6::b3:b4]' ,
'first.last@[IPv6::::b3:b4]' ,
'first.last@[IPv6:a1:::b4]' ,
'first.last@[IPv6:a1:]' ,
'first.last@[IPv6:a1:::]' ,
'first.last@[IPv6:a1:a2:]' ,
'first.last@[IPv6:a1:a2:::]' ,
'first.last@[IPv6::11.22.33.44]' ,
'first.last@[IPv6::::11.22.33.44]' ,
'first.last@[IPv6:a1:11.22.33.44]' ,
'first.last@[IPv6:a1:::11.22.33.44]' ,
'first.last@[IPv6:a1:a2:::11.22.33.44]' ,
'first.last@[IPv6:0123:4567:89ab:cdef::11.22.33.xx]' ,
'first.last@[IPv6:0123:4567:89ab:CDEFF::11.22.33.44]' ,
'first.last@[IPv6:a1::a4:b1::b4:11.22.33.44]' ,
'first.last@[IPv6:a1::11.22.33]' ,
'first.last@[IPv6:a1::11.22.33.44.55]' ,
'first.last@[IPv6:a1::b211.22.33.44]' ,
'first.last@[IPv6:a1::b2::11.22.33.44]' ,
'first.last@[IPv6:a1::b3:]' ,
'first.last@[IPv6::a2::b4]' ,
'first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3:]' ,
'first.last@[IPv6::a2:a3:a4:b1:b2:b3:b4]' ,
2015-10-14 21:39:29 +02:00
'first.last@[IPv6:a1:a2:a3:a4::b1:b2:b3:b4]' ,
2015-12-22 09:23:03 +01:00
" ( \r \n RCPT TO:user@example.com \r \n DATA \\ \n Subject: spam10 \\ \n \r \n Hello, \r \n this is a spam mail. \\ \n . \r \n QUIT \r \n ) a@example.net " //This is valid RCC5322, but we don't want to allow it
2015-10-14 21:39:29 +02:00
);
// IDNs in Unicode and ASCII forms.
$unicodeaddresses = array (
'first.last@bücher.ch' ,
'first.last@кто.рф' ,
'first.last@phplíst.com' ,
);
$asciiaddresses = array (
'first.last@xn--bcher-kva.ch' ,
'first.last@xn--j1ail.xn--p1ai' ,
'first.last@xn--phplst-6va.com' ,
2012-12-04 12:15:39 +01:00
);
$goodfails = array ();
2015-10-14 21:39:29 +02:00
foreach ( array_merge ( $validaddresses , $asciiaddresses ) as $address ) {
2013-07-09 13:46:44 +02:00
if ( ! PHPMailer :: validateAddress ( $address )) {
2012-12-04 12:15:39 +01:00
$goodfails [] = $address ;
}
}
$badpasses = array ();
2015-10-14 21:39:29 +02:00
foreach ( array_merge ( $invalidaddresses , $unicodeaddresses ) as $address ) {
2013-07-09 13:46:44 +02:00
if ( PHPMailer :: validateAddress ( $address )) {
2012-12-04 12:15:39 +01:00
$badpasses [] = $address ;
}
}
$err = '' ;
if ( count ( $goodfails ) > 0 ) {
2013-07-09 13:46:44 +02:00
$err .= " Good addresses that failed validation: \n " ;
2012-12-04 12:15:39 +01:00
$err .= implode ( " \n " , $goodfails );
}
if ( count ( $badpasses ) > 0 ) {
if ( ! empty ( $err )) {
$err .= " \n \n " ;
}
2013-07-09 13:46:44 +02:00
$err .= " Bad addresses that passed validation: \n " ;
2012-12-04 12:15:39 +01:00
$err .= implode ( " \n " , $badpasses );
}
$this -> assertEmpty ( $err , $err );
2013-07-09 13:46:44 +02:00
//For coverage
$this -> assertTrue ( PHPMailer :: validateAddress ( 'test@example.com' , 'auto' ));
$this -> assertFalse ( PHPMailer :: validateAddress ( 'test@example.com.' , 'auto' ));
$this -> assertTrue ( PHPMailer :: validateAddress ( 'test@example.com' , 'pcre' ));
$this -> assertFalse ( PHPMailer :: validateAddress ( 'test@example.com.' , 'pcre' ));
$this -> assertTrue ( PHPMailer :: validateAddress ( 'test@example.com' , 'pcre8' ));
$this -> assertFalse ( PHPMailer :: validateAddress ( 'test@example.com.' , 'pcre8' ));
$this -> assertTrue ( PHPMailer :: validateAddress ( 'test@example.com' , 'php' ));
$this -> assertFalse ( PHPMailer :: validateAddress ( 'test@example.com.' , 'php' ));
$this -> assertTrue ( PHPMailer :: validateAddress ( 'test@example.com' , 'noregex' ));
$this -> assertFalse ( PHPMailer :: validateAddress ( 'bad' , 'noregex' ));
2012-12-04 12:15:39 +01:00
}
/**
2015-03-04 10:29:46 +01:00
* Word - wrap an ASCII message .
2008-08-07 14:41:59 +02:00
*/
2013-07-09 13:46:44 +02:00
public function testWordWrap ()
2012-11-21 16:12:07 +01:00
{
2008-08-07 14:41:59 +02:00
$this -> Mail -> WordWrap = 40 ;
2014-06-05 16:58:17 +02:00
$my_body = str_repeat (
'Here is the main body of this message. It should ' .
'be quite a few lines. It should be wrapped at ' .
'40 characters. Make sure that it is. ' ,
10
);
2008-08-07 14:41:59 +02:00
$nBodyLen = strlen ( $my_body );
$my_body .= " \n \n This is the above body length: " . $nBodyLen ;
$this -> Mail -> Body = $my_body ;
2013-03-06 17:34:28 +01:00
$this -> Mail -> Subject .= ': Wordwrap' ;
2008-08-07 14:41:59 +02:00
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
}
/**
2015-03-04 10:29:46 +01:00
* Word - wrap a multibyte message .
*/
public function testWordWrapMultibyte ()
{
$this -> Mail -> WordWrap = 40 ;
$my_body = str_repeat (
'飛兒樂 團光茫 飛兒樂 團光茫 飛兒樂 團光茫 飛兒樂 團光茫 ' .
'飛飛兒樂 團光茫兒樂 團光茫飛兒樂 團光飛兒樂 團光茫飛兒樂 團光茫兒樂 團光茫 ' .
'飛兒樂 團光茫飛兒樂 團飛兒樂 團光茫光茫飛兒樂 團光茫. ' ,
10
);
$nBodyLen = strlen ( $my_body );
$my_body .= " \n \n This is the above body length: " . $nBodyLen ;
$this -> Mail -> Body = $my_body ;
$this -> Mail -> Subject .= ': Wordwrap multibyte' ;
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
}
/**
* Test low priority .
2008-08-07 14:41:59 +02:00
*/
2013-07-09 13:46:44 +02:00
public function testLowPriority ()
2012-11-21 16:12:07 +01:00
{
2008-08-07 14:41:59 +02:00
$this -> Mail -> Priority = 5 ;
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'Here is the main body. There should be ' .
'a reply to address in this message.' ;
$this -> Mail -> Subject .= ': Low Priority' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> addReplyTo ( 'nobody@nobody.com' , 'Nobody (Unit Test)' );
2008-08-07 14:41:59 +02:00
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
}
/**
* Simple plain file attachment test .
*/
2013-07-09 13:46:44 +02:00
public function testMultiplePlainFileAttachment ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'Here is the text body' ;
$this -> Mail -> Subject .= ': Plain + Multiple FileAttachments' ;
2008-08-07 14:41:59 +02:00
2016-03-29 15:14:19 +02:00
if ( ! $this -> Mail -> addAttachment ( realpath ( self :: INCLUDE_DIR . 'examples/images/phpmailer.png' ))) {
2009-05-05 13:22:26 +02:00
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
return ;
}
2013-07-09 13:46:44 +02:00
if ( ! $this -> Mail -> addAttachment ( __FILE__ , 'test.txt' )) {
2009-05-05 13:22:26 +02:00
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
return ;
}
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
}
/**
* Simple plain string attachment test .
*/
2013-07-09 13:46:44 +02:00
public function testPlainStringAttachment ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'Here is the text body' ;
$this -> Mail -> Subject .= ': Plain + StringAttachment' ;
2012-11-21 16:12:07 +01:00
2013-03-06 17:34:28 +01:00
$sAttachment = 'These characters are the content of the ' .
2012-12-04 12:15:39 +01:00
" string attachment. \n This might be taken from a " .
2013-03-06 17:34:28 +01:00
'database or some other such thing. ' ;
2012-11-21 16:12:07 +01:00
2013-07-09 13:46:44 +02:00
$this -> Mail -> addStringAttachment ( $sAttachment , 'string_attach.txt' );
2008-08-07 14:41:59 +02:00
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
}
/**
* Plain quoted - printable message .
*/
2013-07-09 13:46:44 +02:00
public function testQuotedPrintable ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'Here is the main body' ;
$this -> Mail -> Subject .= ': Plain + Quoted-printable' ;
$this -> Mail -> Encoding = 'quoted-printable' ;
2008-08-07 14:41:59 +02:00
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2009-05-05 13:22:26 +02:00
2012-11-21 16:12:07 +01:00
//Check that a quoted printable encode and decode results in the same as went in
2012-12-04 12:15:39 +01:00
$t = file_get_contents ( __FILE__ ); //Use this file as test content
2015-08-01 11:32:18 +02:00
//Force line breaks to UNIX-style
$t = str_replace ( array ( " \r \n " , " \r " ), " \n " , $t );
2012-12-04 12:15:39 +01:00
$this -> assertEquals (
$t ,
2013-07-09 13:46:44 +02:00
quoted_printable_decode ( $this -> Mail -> encodeQP ( $t )),
2012-12-04 12:15:39 +01:00
'Quoted-Printable encoding round-trip failed'
);
2013-07-09 13:46:44 +02:00
$this -> assertEquals (
$this -> Mail -> encodeQP ( $t ),
$this -> Mail -> encodeQPphp ( $t ),
'Quoted-Printable BC wrapper failed'
);
2015-08-01 11:32:18 +02:00
//Force line breaks to Windows-style
$t = str_replace ( " \n " , " \r \n " , $t );
$this -> assertEquals (
$t ,
quoted_printable_decode ( $this -> Mail -> encodeQP ( $t )),
'Quoted-Printable encoding round-trip failed (Windows line breaks)'
);
2008-08-07 14:41:59 +02:00
}
/**
2015-03-04 10:29:46 +01:00
* Send an HTML message .
2008-08-07 14:41:59 +02:00
*/
2013-07-09 13:46:44 +02:00
public function testHtml ()
2012-11-21 16:12:07 +01:00
{
2013-07-09 13:46:44 +02:00
$this -> Mail -> isHTML ( true );
2008-08-07 14:41:59 +02:00
$this -> Mail -> Subject .= " : HTML only " ;
2012-11-21 16:12:07 +01:00
2013-11-09 13:53:45 +01:00
$this -> Mail -> Body = <<< EOT
2013-02-28 14:21:00 +01:00
< html >
< head >
< title > HTML email test </ title >
</ head >
< body >
< h1 > PHPMailer does HTML !</ h1 >
< p > This is a < strong > test message </ strong > written in HTML .< br >
2013-04-11 18:45:39 +02:00
Go to < a href = " https://github.com/PHPMailer/PHPMailer/ " > https :// github . com / PHPMailer / PHPMailer /</ a >
2013-02-28 14:21:00 +01:00
for new versions of PHPMailer .</ p >
< p > Thank you !</ p >
</ body >
</ html >
EOT ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2015-03-16 14:10:14 +01:00
$msg = $this -> Mail -> getSentMIMEMessage ();
$this -> assertNotContains ( " \r \n \r \n MIME-Version: " , $msg , 'Incorrect MIME headers' );
2008-08-07 14:41:59 +02:00
}
2015-03-25 19:22:55 +01:00
/**
* Send a message containing ISO - 8859 - 1 text .
*/
public function testHtmlIso8859 ()
{
2015-04-29 20:46:10 +02:00
$this -> Mail -> isHTML ( true );
2015-03-25 19:22:55 +01:00
$this -> Mail -> Subject .= " : ISO-8859-1 HTML " ;
$this -> Mail -> CharSet = 'iso-8859-1' ;
//This file is in ISO-8859-1 charset
//Needs to be external because this file is in UTF-8
2016-03-29 15:14:19 +02:00
$content = file_get_contents ( realpath ( '../examples/contents.html' ));
2015-04-29 20:46:10 +02:00
// This is the string 'éèîüçÅñæß' in ISO-8859-1, base-64 encoded
$check = base64_decode ( '6eju/OfF8ebf' );
2015-03-25 19:22:55 +01:00
//Make sure it really is in ISO-8859-1!
2015-04-29 20:46:10 +02:00
$this -> Mail -> msgHTML (
2015-03-25 19:22:55 +01:00
mb_convert_encoding (
$content ,
" ISO-8859-1 " ,
mb_detect_encoding ( $content , " UTF-8, ISO-8859-1, ISO-8859-15 " , true )
2015-04-29 20:46:10 +02:00
),
2016-03-29 15:14:19 +02:00
realpath ( self :: INCLUDE_DIR . 'examples' )
2015-04-29 20:46:10 +02:00
);
2015-03-25 19:22:55 +01:00
$this -> buildBody ();
2015-04-29 20:46:10 +02:00
$this -> assertTrue (
strpos ( $this -> Mail -> Body , $check ) !== false ,
'ISO message body does not contain expected text'
);
2015-03-25 19:22:55 +01:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
}
2013-05-01 10:20:10 +02:00
/**
2015-03-20 18:11:15 +01:00
* Send a message containing multilingual UTF - 8 text .
*/
public function testHtmlUtf8 ()
{
$this -> Mail -> isHTML ( true );
$this -> Mail -> Subject .= " : UTF-8 HTML " ;
$this -> Mail -> CharSet = 'UTF-8' ;
$this -> Mail -> Body = <<< EOT
< html >
< head >
2015-03-25 19:22:55 +01:00
< meta http - equiv = " Content-Type " content = " text/html; charset=utf-8 " >
2015-03-20 18:11:15 +01:00
< title > HTML email test </ title >
</ head >
< body >
< p > Chinese text : 郵件內容為空 </ p >
< p > Russian text : Пустое тело сообщения </ p >
< p > Armenian text : Հաղորդագրությունը դատարկ է </ p >
< p > Czech text : Prázdné tělo zprávy </ p >
</ body >
</ html >
EOT ;
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
$msg = $this -> Mail -> getSentMIMEMessage ();
$this -> assertNotContains ( " \r \n \r \n MIME-Version: " , $msg , 'Incorrect MIME headers' );
}
2015-03-25 19:22:55 +01:00
/**
* Send a message containing multilingual UTF - 8 text with an embedded image .
*/
public function testUtf8WithEmbeddedImage ()
{
$this -> Mail -> isHTML ( true );
$this -> Mail -> Subject .= " : UTF-8 with embedded image " ;
$this -> Mail -> CharSet = 'UTF-8' ;
$this -> Mail -> Body = <<< EOT
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=utf-8 " >
< title > HTML email test </ title >
</ head >
< body >
< p > Chinese text : 郵件內容為空 </ p >
< p > Russian text : Пустое тело сообщения </ p >
< p > Armenian text : Հաղորդագրությունը դատարկ է </ p >
< p > Czech text : Prázdné tělo zprávy </ p >
Embedded Image : < img alt = " phpmailer " src = " cid:my-attach " >
</ body >
</ html >
EOT ;
$this -> Mail -> addEmbeddedImage (
2016-03-29 15:14:19 +02:00
realpath ( self :: INCLUDE_DIR . 'examples/images/phpmailer.png' ),
2015-03-25 19:22:55 +01:00
'my-attach' ,
'phpmailer.png' ,
'base64' ,
'image/png'
);
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
}
2015-03-20 18:11:15 +01:00
/**
* Send a message containing multilingual UTF - 8 text .
*/
public function testPlainUtf8 ()
{
$this -> Mail -> isHTML ( false );
$this -> Mail -> Subject .= " : UTF-8 plain text " ;
$this -> Mail -> CharSet = 'UTF-8' ;
$this -> Mail -> Body = <<< EOT
Chinese text : 郵件內容為空
Russian text : Пустое тело сообщения
Armenian text : Հաղորդագրությունը դատարկ է
Czech text : Prázdné tělo zprávy
EOT ;
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
$msg = $this -> Mail -> getSentMIMEMessage ();
$this -> assertNotContains ( " \r \n \r \n MIME-Version: " , $msg , 'Incorrect MIME headers' );
}
/**
2013-05-01 10:20:10 +02:00
* Test simple message builder and html2text converters
*/
2013-07-09 13:46:44 +02:00
public function testMsgHTML ()
{
2016-03-29 15:14:19 +02:00
$message = file_get_contents ( realpath ( self :: INCLUDE_DIR . 'examples/contentsutf8.html' ));
2013-05-01 10:20:10 +02:00
$this -> Mail -> CharSet = 'utf-8' ;
$this -> Mail -> Body = '' ;
$this -> Mail -> AltBody = '' ;
2014-12-17 16:07:02 +01:00
//Uses internal HTML to text conversion
2016-03-29 15:14:19 +02:00
$this -> Mail -> msgHTML ( $message , realpath ( self :: INCLUDE_DIR . 'examples' ));
2013-07-09 13:46:44 +02:00
$this -> Mail -> Subject .= ': msgHTML' ;
2013-02-28 14:21:00 +01:00
2013-07-09 13:46:44 +02:00
$this -> assertNotEmpty ( $this -> Mail -> Body , 'Body not set by msgHTML' );
$this -> assertNotEmpty ( $this -> Mail -> AltBody , 'AltBody not set by msgHTML' );
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2013-03-21 15:08:29 +01:00
2014-12-17 16:07:02 +01:00
//Again, using a custom HTML to text converter
2013-03-21 15:08:29 +01:00
$this -> Mail -> AltBody = '' ;
2016-03-29 15:14:19 +02:00
$this -> Mail -> msgHTML ( $message , realpath ( self :: INCLUDE_DIR . 'examples' ), function ( $html ) {
2014-12-17 16:07:02 +01:00
return strtoupper ( strip_tags ( $html ));
});
$this -> Mail -> Subject .= ' + custom html2text' ;
$this -> assertNotEmpty ( $this -> Mail -> AltBody , 'Custom AltBody not set by msgHTML' );
2013-03-21 15:08:29 +01:00
2013-07-09 13:46:44 +02:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2013-02-28 14:21:00 +01:00
}
2013-05-01 10:20:10 +02:00
2008-08-07 14:41:59 +02:00
/**
* Simple HTML and attachment test
*/
2013-07-09 13:46:44 +02:00
public function testHTMLAttachment ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'This is the <strong>HTML</strong> part of the email.' ;
$this -> Mail -> Subject .= ': HTML + Attachment' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> isHTML ( true );
2012-11-21 16:12:07 +01:00
2016-03-29 15:14:19 +02:00
if ( ! $this -> Mail -> addAttachment (
realpath ( self :: INCLUDE_DIR . 'examples/images/phpmailer_mini.png' ), 'phpmailer_mini.png' )
) {
2009-05-05 13:22:26 +02:00
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
return ;
}
2013-06-06 17:31:11 +02:00
//Make sure that trying to attach a nonexistent file fails
2013-07-09 13:46:44 +02:00
$this -> assertFalse ( $this -> Mail -> addAttachment ( __FILE__ . md5 ( microtime ()), 'nonexistent_file.txt' ));
2013-06-06 17:31:11 +02:00
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
2015-08-25 12:01:39 +02:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
}
/**
* Test embedded image without a name
*/
public function testHTMLStringEmbedNoName ()
{
$this -> Mail -> Body = 'This is the <strong>HTML</strong> part of the email.' ;
$this -> Mail -> Subject .= ': HTML + unnamed embedded image' ;
$this -> Mail -> isHTML ( true );
if ( ! $this -> Mail -> addStringEmbeddedImage (
2016-03-29 15:14:19 +02:00
file_get_contents ( realpath ( self :: INCLUDE_DIR . 'examples/images/phpmailer_mini.png' )),
2015-08-25 12:01:39 +02:00
md5 ( 'phpmailer_mini.png' ) . '@phpmailer.0' ,
'' , //intentionally empty name
'base64' ,
'image/png' ,
'inline' )
) {
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
return ;
}
$this -> buildBody ();
2013-07-09 13:46:44 +02:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
}
2015-05-04 10:59:56 +02:00
/**
* Simple HTML and multiple attachment test
*/
public function testHTMLMultiAttachment ()
{
$this -> Mail -> Body = 'This is the <strong>HTML</strong> part of the email.' ;
$this -> Mail -> Subject .= ': HTML + multiple Attachment' ;
$this -> Mail -> isHTML ( true );
2016-03-29 15:14:19 +02:00
if ( ! $this -> Mail -> addAttachment ( realpath ( self :: INCLUDE_DIR . 'examples/images/phpmailer_mini.png' ), 'phpmailer_mini.png' )) {
2015-05-04 10:59:56 +02:00
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
return ;
}
2016-03-29 15:14:19 +02:00
if ( ! $this -> Mail -> addAttachment ( realpath ( self :: INCLUDE_DIR . 'examples/images/phpmailer.png' ), 'phpmailer.png' )) {
2015-05-04 10:59:56 +02:00
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
return ;
}
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
}
2008-08-07 14:41:59 +02:00
/**
* An embedded attachment test .
*/
2013-07-09 13:46:44 +02:00
public function testEmbeddedImage ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
2015-03-25 19:22:55 +01:00
'Here is an image!' ;
2013-03-06 17:34:28 +01:00
$this -> Mail -> Subject .= ': Embedded Image' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> isHTML ( true );
2012-11-21 16:12:07 +01:00
2013-07-09 13:46:44 +02:00
if ( ! $this -> Mail -> addEmbeddedImage (
2016-03-29 15:14:19 +02:00
realpath ( self :: INCLUDE_DIR . 'examples/images/phpmailer.png' ),
2013-03-06 17:34:28 +01:00
'my-attach' ,
2013-05-01 10:20:10 +02:00
'phpmailer.png' ,
2013-03-06 17:34:28 +01:00
'base64' ,
'image/png'
2012-11-21 16:12:07 +01:00
)
) {
2009-05-05 13:22:26 +02:00
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
return ;
}
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2012-11-21 16:12:07 +01:00
//For code coverage
2013-07-09 13:46:44 +02:00
$this -> Mail -> addEmbeddedImage ( 'thisfiledoesntexist' , 'xyz' ); //Non-existent file
$this -> Mail -> addEmbeddedImage ( __FILE__ , '123' ); //Missing name
2008-08-07 14:41:59 +02:00
}
/**
* An embedded attachment test .
*/
2013-07-09 13:46:44 +02:00
public function testMultiEmbeddedImage ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
'Here is an image!</a>' ;
$this -> Mail -> Subject .= ': Embedded Image + Attachment' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> isHTML ( true );
2012-11-21 16:12:07 +01:00
2013-07-09 13:46:44 +02:00
if ( ! $this -> Mail -> addEmbeddedImage (
2016-03-29 15:14:19 +02:00
realpath ( self :: INCLUDE_DIR . 'examples/images/phpmailer.png' ),
2013-03-06 17:34:28 +01:00
'my-attach' ,
2013-05-01 10:20:10 +02:00
'phpmailer.png' ,
2013-03-06 17:34:28 +01:00
'base64' ,
'image/png'
2012-11-21 16:12:07 +01:00
)
) {
2009-05-05 13:22:26 +02:00
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
return ;
}
2013-07-09 13:46:44 +02:00
if ( ! $this -> Mail -> addAttachment ( __FILE__ , 'test.txt' )) {
2009-05-05 13:22:26 +02:00
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
return ;
}
2012-11-21 16:12:07 +01:00
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
}
/**
* Simple multipart / alternative test .
*/
2013-07-09 13:46:44 +02:00
public function testAltBody ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'This is the <strong>HTML</strong> 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.' ;
2008-08-07 14:41:59 +02:00
$this -> Mail -> WordWrap = 40 ;
2013-07-09 13:46:44 +02:00
$this -> addNote ( 'This is a mulipart alternative email' );
2013-03-06 17:34:28 +01:00
$this -> Mail -> Subject .= ': AltBody + Word Wrap' ;
2008-08-07 14:41:59 +02:00
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
}
/**
* Simple HTML and attachment test
*/
2013-07-09 13:46:44 +02:00
public function testAltBodyAttachment ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'This is the <strong>HTML</strong> part of the email.' ;
$this -> Mail -> AltBody = 'This is the text part of the email.' ;
$this -> Mail -> Subject .= ': AltBody + Attachment' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> isHTML ( true );
2012-11-21 16:12:07 +01:00
2013-07-09 13:46:44 +02:00
if ( ! $this -> Mail -> addAttachment ( __FILE__ , 'test_attach.txt' )) {
2009-05-05 13:22:26 +02:00
$this -> assertTrue ( false , $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
return ;
}
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2012-11-21 16:12:07 +01:00
}
2008-08-07 14:41:59 +02:00
2013-07-09 13:46:44 +02:00
/**
2015-03-04 10:29:46 +01:00
* iCal event test .
2013-07-09 13:46:44 +02:00
*/
public function testIcal ()
{
2015-01-09 14:13:47 +01:00
//Standalone ICS tests
2016-03-29 15:14:19 +02:00
require_once realpath ( self :: INCLUDE_DIR . 'extras/EasyPeasyICS.php' );
2015-01-09 14:13:47 +01:00
$ICS = new EasyPeasyICS ( " PHPMailer test calendar " );
$this -> assertNotEmpty (
$ICS -> addEvent (
strtotime ( 'tomorrow 10:00 Europe/Paris' ),
strtotime ( 'tomorrow 11:00 Europe/Paris' ),
'PHPMailer iCal test' ,
'A test of PHPMailer iCal support' ,
'https://github.com/PHPMailer/PHPMailer'
),
'Generated event string is empty'
);
$ICS -> addEvent (
strtotime ( 'tomorrow 10:00 Europe/Paris' ),
strtotime ( 'tomorrow 11:00 Europe/Paris' ),
'PHPMailer iCal test' ,
'A test of PHPMailer iCal support' ,
'https://github.com/PHPMailer/PHPMailer'
);
$events = $ICS -> getEvents ();
$this -> assertEquals ( 2 , count ( $events ), 'Event count mismatch' );
$ICS -> clearEvents ();
$events = $ICS -> getEvents ();
$this -> assertEquals ( 0 , count ( $events ), 'Event clearing failed' );
$ICS -> setName ( 'test' );
$this -> assertEquals ( 'test' , $ICS -> getName (), 'Setting ICS name failed' );
$this -> assertNotEmpty ( $ICS -> render ( false ), 'Empty calendar' );
//Need to test generated output but PHPUnit isn't playing nice
//$rendered = $ICS->render();
2015-04-29 20:46:10 +02:00
//Test sending an ICS
2013-07-09 13:46:44 +02:00
$this -> Mail -> Body = 'This is the <strong>HTML</strong> part of the email.' ;
$this -> Mail -> AltBody = 'This is the text part of the email.' ;
$this -> Mail -> Subject .= ': iCal' ;
$this -> Mail -> isHTML ( true );
$this -> buildBody ();
$this -> Mail -> Ical = $ICS -> render ( false );
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
}
/**
2015-03-04 10:29:46 +01:00
* Test sending multiple messages with separate connections .
2012-11-21 16:12:07 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testMultipleSend ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'Sending two messages without keepalive' ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
2008-08-07 14:41:59 +02:00
$subject = $this -> Mail -> Subject ;
2013-03-06 17:34:28 +01:00
$this -> Mail -> Subject = $subject . ': SMTP 1' ;
2013-07-09 13:46:44 +02:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2012-11-21 16:12:07 +01:00
2013-03-06 17:34:28 +01:00
$this -> Mail -> Subject = $subject . ': SMTP 2' ;
2012-11-21 16:12:07 +01:00
$this -> Mail -> Sender = 'blah@example.com' ;
2013-07-09 13:46:44 +02:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2009-05-05 13:22:26 +02:00
}
2012-11-21 16:12:07 +01:00
/**
2015-03-04 10:29:46 +01:00
* Test sending using SendMail .
2012-11-21 16:12:07 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testSendmailSend ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'Sending via sendmail' ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
2009-05-05 13:22:26 +02:00
$subject = $this -> Mail -> Subject ;
2013-03-06 17:34:28 +01:00
$this -> Mail -> Subject = $subject . ': sendmail' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> isSendmail ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2009-05-05 13:22:26 +02:00
}
2013-10-29 11:17:57 +01:00
/**
2015-03-04 10:29:46 +01:00
* Test sending using Qmail .
2013-10-29 11:17:57 +01:00
*/
public function testQmailSend ()
{
//Only run if we have qmail installed
if ( file_exists ( '/var/qmail/bin/qmail-inject' )) {
$this -> Mail -> Body = 'Sending via qmail' ;
$this -> BuildBody ();
$subject = $this -> Mail -> Subject ;
$this -> Mail -> Subject = $subject . ': qmail' ;
$this -> Mail -> IsQmail ();
$this -> assertTrue ( $this -> Mail -> Send (), $this -> Mail -> ErrorInfo );
2013-12-16 14:28:57 +01:00
} else {
$this -> markTestSkipped ( 'Qmail is not installed' );
2013-10-29 11:17:57 +01:00
}
}
2012-11-21 16:12:07 +01:00
/**
2015-03-04 10:29:46 +01:00
* Test sending using PHP mail () function .
2012-11-21 16:12:07 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testMailSend ()
2012-11-21 16:12:07 +01:00
{
2013-02-28 19:25:31 +01:00
$sendmail = ini_get ( 'sendmail_path' );
2015-03-16 14:10:14 +01:00
//No path in sendmail_path
if ( strpos ( $sendmail , '/' ) === false ) {
2013-02-28 19:25:31 +01:00
ini_set ( 'sendmail_path' , '/usr/sbin/sendmail -t -i ' );
}
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'Sending via mail()' ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
2009-05-05 13:22:26 +02:00
2013-03-21 11:56:26 +01:00
$this -> Mail -> Subject = $this -> Mail -> Subject . ': mail()' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> isMail ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2015-03-16 14:10:14 +01:00
$msg = $this -> Mail -> getSentMIMEMessage ();
$this -> assertNotContains ( " \r \n \r \n MIME-Version: " , $msg , 'Incorrect MIME headers' );
2013-03-21 11:56:26 +01:00
}
/**
2015-03-04 10:29:46 +01:00
* Test sending an empty body .
2013-03-21 11:56:26 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testEmptyBody ()
2013-03-21 11:56:26 +01:00
{
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
2013-03-21 11:56:26 +01:00
$this -> Mail -> Body = '' ;
$this -> Mail -> Subject = $this -> Mail -> Subject . ': Empty Body' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> isMail ();
2013-03-21 11:56:26 +01:00
$this -> Mail -> AllowEmpty = true ;
2013-07-09 13:46:44 +02:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2013-03-21 11:56:26 +01:00
$this -> Mail -> AllowEmpty = false ;
2013-07-09 13:46:44 +02:00
$this -> assertFalse ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
}
2014-11-14 10:15:45 +01:00
/**
* Test constructing a message that contains lines that are too long for RFC compliance .
*/
public function testLongBody ()
{
$oklen = str_repeat ( str_repeat ( '0' , PHPMailer :: MAX_LINE_LENGTH ) . PHPMailer :: CRLF , 10 );
$badlen = str_repeat ( str_repeat ( '1' , PHPMailer :: MAX_LINE_LENGTH + 1 ) . PHPMailer :: CRLF , 2 );
$this -> Mail -> Body = " This message contains lines that are too long. " .
PHPMailer :: CRLF . PHPMailer :: CRLF . $oklen . $badlen . $oklen ;
$this -> assertTrue (
PHPMailer :: hasLineLongerThanMax ( $this -> Mail -> Body ),
'Test content does not contain long lines!'
);
$this -> buildBody ();
$this -> Mail -> Encoding = '8bit' ;
$this -> Mail -> preSend ();
$message = $this -> Mail -> getSentMIMEMessage ();
$this -> assertFalse ( PHPMailer :: hasLineLongerThanMax ( $message ), 'Long line not corrected.' );
$this -> assertContains (
'Content-Transfer-Encoding: quoted-printable' ,
$message ,
'Long line did not cause transfer encoding switch.'
);
}
/**
* Test constructing a message that does NOT contain lines that are too long for RFC compliance .
*/
public function testShortBody ()
{
$oklen = str_repeat ( str_repeat ( '0' , PHPMailer :: MAX_LINE_LENGTH ) . PHPMailer :: CRLF , 10 );
$this -> Mail -> Body = " This message does not contain lines that are too long. " .
PHPMailer :: CRLF . PHPMailer :: CRLF . $oklen ;
$this -> assertFalse (
PHPMailer :: hasLineLongerThanMax ( $this -> Mail -> Body ),
'Test content contains long lines!'
);
$this -> buildBody ();
$this -> Mail -> Encoding = '8bit' ;
$this -> Mail -> preSend ();
$message = $this -> Mail -> getSentMIMEMessage ();
$this -> assertFalse ( PHPMailer :: hasLineLongerThanMax ( $message ), 'Long line not corrected.' );
$this -> assertNotContains (
'Content-Transfer-Encoding: quoted-printable' ,
$message ,
'Short line caused transfer encoding switch.'
);
}
2012-11-21 16:12:07 +01:00
/**
2015-03-04 10:29:46 +01:00
* Test keepalive ( sending multiple messages in a single connection ) .
2012-11-21 16:12:07 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testSmtpKeepAlive ()
2012-11-21 16:12:07 +01:00
{
2015-03-16 14:10:14 +01:00
$this -> Mail -> Body = 'SMTP keep-alive test.' ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
2008-08-07 14:41:59 +02:00
$subject = $this -> Mail -> Subject ;
$this -> Mail -> SMTPKeepAlive = true ;
2013-03-06 17:34:28 +01:00
$this -> Mail -> Subject = $subject . ': SMTP keep-alive 1' ;
2013-07-09 13:46:44 +02:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2012-11-21 16:12:07 +01:00
2013-03-06 17:34:28 +01:00
$this -> Mail -> Subject = $subject . ': SMTP keep-alive 2' ;
2013-07-09 13:46:44 +02:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
$this -> Mail -> smtpClose ();
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
2013-05-16 17:08:50 +02:00
/**
2012-11-21 16:12:07 +01:00
* Tests this denial of service attack :
2015-03-16 14:10:14 +01:00
* @ link http :// www . cybsec . com / vuln / PHPMailer - DOS . pdf
2008-08-07 14:41:59 +02:00
*/
2013-07-09 13:46:44 +02:00
public function testDenialOfServiceAttack ()
2012-11-21 16:12:07 +01:00
{
2013-03-06 17:34:28 +01:00
$this -> Mail -> Body = 'This should no longer cause a denial of service.' ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
2012-11-21 16:12:07 +01:00
2013-03-06 23:55:58 +01:00
$this -> Mail -> Subject = substr ( str_repeat ( '0123456789' , 100 ), 0 , 998 );
2013-07-09 13:46:44 +02:00
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
2008-08-07 14:41:59 +02:00
}
2012-11-21 16:12:07 +01:00
2013-03-06 17:34:28 +01:00
/**
* Tests this denial of service attack :
2015-03-16 14:10:14 +01:00
* @ link https :// sourceforge . net / p / phpmailer / bugs / 383 /
2013-03-06 17:34:28 +01:00
* According to the ticket , this should get stuck in a loop , though I can ' t make it happen .
2015-03-16 14:10:14 +01:00
* @ TODO No assertions in here - how can you assert for this ?
2013-03-06 17:34:28 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testDenialOfServiceAttack2 ()
2013-03-06 17:34:28 +01:00
{
//Encoding name longer than 68 chars
$this -> Mail -> Encoding = '1234567890123456789012345678901234567890123456789012345678901234567890' ;
2013-07-09 13:46:44 +02:00
//Call wrapText with a zero length value
$this -> Mail -> wrapText ( str_repeat ( 'This should no longer cause a denial of service. ' , 30 ), 0 );
2013-03-06 17:34:28 +01:00
}
2012-11-21 16:12:07 +01:00
/**
2015-03-04 10:29:46 +01:00
* Test error handling .
2012-11-21 16:12:07 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testError ()
2012-11-21 16:12:07 +01:00
{
2015-03-16 10:24:39 +01:00
$this -> Mail -> Subject .= ': Error handling test - this should be sent ok' ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> Mail -> clearAllRecipients (); // no addresses should cause an error
$this -> assertTrue ( $this -> Mail -> isError () == false , 'Error found' );
$this -> assertTrue ( $this -> Mail -> send () == false , 'send succeeded' );
$this -> assertTrue ( $this -> Mail -> isError (), 'No error found' );
2012-11-21 16:12:07 +01:00
$this -> assertEquals ( 'You must provide at least one recipient email address.' , $this -> Mail -> ErrorInfo );
2013-07-09 13:46:44 +02:00
$this -> Mail -> addAddress ( $_REQUEST [ 'mail_to' ]);
$this -> assertTrue ( $this -> Mail -> send (), 'send failed' );
2012-11-21 16:12:07 +01:00
}
/**
2015-03-04 10:29:46 +01:00
* Test addressing .
2012-11-21 16:12:07 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testAddressing ()
2012-11-21 16:12:07 +01:00
{
2013-07-09 13:46:44 +02:00
$this -> assertFalse ( $this -> Mail -> addAddress ( '' ), 'Empty address accepted' );
$this -> assertFalse ( $this -> Mail -> addAddress ( '' , 'Nobody' ), 'Empty address with name accepted' );
$this -> assertFalse ( $this -> Mail -> addAddress ( 'a@example..com' ), 'Invalid address accepted' );
$this -> assertTrue ( $this -> Mail -> addAddress ( 'a@example.com' ), 'Addressing failed' );
$this -> assertFalse ( $this -> Mail -> addAddress ( 'a@example.com' ), 'Duplicate addressing failed' );
$this -> assertTrue ( $this -> Mail -> addCC ( 'b@example.com' ), 'CC addressing failed' );
$this -> assertFalse ( $this -> Mail -> addCC ( 'b@example.com' ), 'CC duplicate addressing failed' );
$this -> assertFalse ( $this -> Mail -> addCC ( 'a@example.com' ), 'CC duplicate addressing failed (2)' );
$this -> assertTrue ( $this -> Mail -> addBCC ( 'c@example.com' ), 'BCC addressing failed' );
$this -> assertFalse ( $this -> Mail -> addBCC ( 'c@example.com' ), 'BCC duplicate addressing failed' );
$this -> assertFalse ( $this -> Mail -> addBCC ( 'a@example.com' ), 'BCC duplicate addressing failed (2)' );
$this -> assertTrue ( $this -> Mail -> addReplyTo ( 'a@example.com' ), 'Replyto Addressing failed' );
$this -> assertFalse ( $this -> Mail -> addReplyTo ( 'a@example..com' ), 'Invalid Replyto address accepted' );
$this -> assertTrue ( $this -> Mail -> setFrom ( 'a@example.com' , 'some name' ), 'setFrom failed' );
$this -> assertFalse ( $this -> Mail -> setFrom ( 'a@example.com.' , 'some name' ), 'setFrom accepted invalid address' );
$this -> Mail -> Sender = '' ;
$this -> Mail -> setFrom ( 'a@example.com' , 'some name' , true );
$this -> assertEquals ( $this -> Mail -> Sender , 'a@example.com' , 'setFrom failed to set sender' );
$this -> Mail -> Sender = '' ;
$this -> Mail -> setFrom ( 'a@example.com' , 'some name' , false );
$this -> assertEquals ( $this -> Mail -> Sender , '' , 'setFrom should not have set sender' );
$this -> Mail -> clearCCs ();
$this -> Mail -> clearBCCs ();
$this -> Mail -> clearReplyTos ();
2012-11-21 16:12:07 +01:00
}
2015-05-21 17:05:57 +02:00
/**
* Test RFC822 address splitting .
*/
public function testAddressSplitting ()
{
//Test built-in address parser
$this -> assertCount (
2 ,
$this -> Mail -> parseAddresses (
'Joe User <joe@example.com>, Jill User <jill@example.net>'
),
'Failed to recognise address list (IMAP parser)'
);
2016-04-17 09:49:12 +02:00
$this -> assertEquals (
array (
array ( " name " => 'Joe User' , 'address' => 'joe@example.com' ),
array ( " name " => 'Jill User' , 'address' => 'jill@example.net' ),
array ( " name " => '' , 'address' => 'frank@example.com' ),
),
$this -> Mail -> parseAddresses (
'Joe User <joe@example.com>,'
. 'Jill User <jill@example.net>,'
. 'frank@example.com,'
),
'Parsed addresses'
);
2015-05-21 17:05:57 +02:00
//Test simple address parser
$this -> assertCount (
2 ,
$this -> Mail -> parseAddresses (
'Joe User <joe@example.com>, Jill User <jill@example.net>' ,
false
),
'Failed to recognise address list'
);
//Test single address
$this -> assertNotEmpty (
$this -> Mail -> parseAddresses (
'Joe User <joe@example.com>' ,
false
),
'Failed to recognise single address'
);
//Test quoted name IMAP
$this -> assertNotEmpty (
$this -> Mail -> parseAddresses (
'Tim "The Book" O\'Reilly <foo@example.com>'
),
'Failed to recognise quoted name (IMAP)'
);
//Test quoted name
$this -> assertNotEmpty (
$this -> Mail -> parseAddresses (
'Tim "The Book" O\'Reilly <foo@example.com>' ,
false
),
'Failed to recognise quoted name'
);
//Test single address IMAP
$this -> assertNotEmpty (
$this -> Mail -> parseAddresses (
'Joe User <joe@example.com>'
),
'Failed to recognise single address (IMAP)'
);
//Test unnamed address
$this -> assertNotEmpty (
$this -> Mail -> parseAddresses (
'joe@example.com' ,
false
),
'Failed to recognise unnamed address'
);
//Test unnamed address IMAP
$this -> assertNotEmpty (
$this -> Mail -> parseAddresses (
'joe@example.com'
),
'Failed to recognise unnamed address (IMAP)'
);
//Test invalid addresses
$this -> assertEmpty (
$this -> Mail -> parseAddresses (
'Joe User <joe@example.com.>, Jill User <jill.@example.net>'
),
'Failed to recognise invalid addresses (IMAP)'
);
//Test invalid addresses
$this -> assertEmpty (
$this -> Mail -> parseAddresses (
'Joe User <joe@example.com.>, Jill User <jill.@example.net>' ,
false
),
'Failed to recognise invalid addresses'
);
}
2013-03-21 11:10:52 +01:00
/**
2015-03-04 10:29:46 +01:00
* Test address escaping .
2013-03-21 11:10:52 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testAddressEscaping ()
2013-03-21 11:10:52 +01:00
{
$this -> Mail -> Subject .= ': Address escaping' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> clearAddresses ();
$this -> Mail -> addAddress ( 'foo@example.com' , 'Tim "The Book" O\'Reilly' );
2013-03-21 11:10:52 +01:00
$this -> Mail -> Body = 'Test correct escaping of quotes in addresses.' ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> Mail -> preSend ();
$b = $this -> Mail -> getSentMIMEMessage ();
$this -> assertTrue (( strpos ( $b , 'To: "Tim \"The Book\" O\'Reilly" <foo@example.com>' ) !== false ));
2013-03-21 11:10:52 +01:00
}
2015-12-18 13:16:51 +01:00
/**
* Test MIME structure assembly .
*/
public function testMIMEStructure ()
{
$this -> Mail -> Subject .= ': MIME structure' ;
$this -> Mail -> Body = '<h3>MIME structure test.</h3>' ;
$this -> Mail -> AltBody = 'MIME structure test.' ;
$this -> buildBody ();
$this -> Mail -> preSend ();
$this -> assertRegExp (
" /Content-Transfer-Encoding: 8bit \r \n \r \n " .
" This is a multi-part message in MIME format./ " ,
$this -> Mail -> getSentMIMEMessage (),
'MIME structure broken'
);
}
2013-03-06 17:34:28 +01:00
/**
2015-03-04 10:29:46 +01:00
* Test BCC - only addressing .
2013-03-06 17:34:28 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testBCCAddressing ()
2013-03-06 17:34:28 +01:00
{
$this -> Mail -> Subject .= ': BCC-only addressing' ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
$this -> Mail -> clearAllRecipients ();
$this -> assertTrue ( $this -> Mail -> addBCC ( 'a@example.com' ), 'BCC addressing failed' );
$this -> assertTrue ( $this -> Mail -> send (), 'send failed' );
2013-03-06 17:34:28 +01:00
}
2012-11-21 16:12:07 +01:00
/**
2015-03-04 10:29:46 +01:00
* Encoding and charset tests .
2012-11-21 16:12:07 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testEncodings ()
2012-11-21 16:12:07 +01:00
{
$this -> Mail -> CharSet = 'iso-8859-1' ;
$this -> assertEquals (
'=A1Hola!_Se=F1or!' ,
2013-07-09 13:46:44 +02:00
$this -> Mail -> encodeQ ( " \xa1 Hola! Se \xf1 or! " , 'text' ),
2012-11-21 16:12:07 +01:00
'Q Encoding (text) failed'
);
$this -> assertEquals (
'=A1Hola!_Se=F1or!' ,
2013-07-09 13:46:44 +02:00
$this -> Mail -> encodeQ ( " \xa1 Hola! Se \xf1 or! " , 'comment' ),
2012-11-21 16:12:07 +01:00
'Q Encoding (comment) failed'
);
$this -> assertEquals (
'=A1Hola!_Se=F1or!' ,
2013-07-09 13:46:44 +02:00
$this -> Mail -> encodeQ ( " \xa1 Hola! Se \xf1 or! " , 'phrase' ),
2012-11-21 16:12:07 +01:00
'Q Encoding (phrase) failed'
);
2013-02-28 14:21:00 +01:00
$this -> Mail -> CharSet = 'UTF-8' ;
$this -> assertEquals (
'=C2=A1Hola!_Se=C3=B1or!' ,
2013-07-09 13:46:44 +02:00
$this -> Mail -> encodeQ ( " \xc2 \xa1 Hola! Se \xc3 \xb1 or! " , 'text' ),
2013-02-28 14:21:00 +01:00
'Q Encoding (text) failed'
);
2013-08-01 09:55:26 +02:00
//Strings containing '=' are a special case
$this -> assertEquals (
'Nov=C3=A1=3D' ,
$this -> Mail -> encodeQ ( " Nov \xc3 \xa1 = " , 'text' ),
'Q Encoding (text) failed 2'
);
2012-11-21 16:12:07 +01:00
}
2015-03-04 10:29:46 +01:00
/**
* Test base - 64 encoding .
*/
2013-08-02 16:19:01 +02:00
public function testBase64 ()
{
$this -> Mail -> Subject .= ': Base-64 encoding' ;
$this -> Mail -> Encoding = 'base64' ;
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), 'Base64 encoding failed' );
}
2012-11-21 16:12:07 +01:00
/**
2015-03-06 16:35:29 +01:00
* S / MIME Signing tests ( self - signed ) .
2015-03-16 14:10:14 +01:00
* @ requires extension openssl
2012-11-21 16:12:07 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testSigning ()
2012-11-21 16:12:07 +01:00
{
2013-07-09 13:46:44 +02:00
$this -> Mail -> Subject .= ': S/MIME signing' ;
2013-01-31 12:04:26 +01:00
$this -> Mail -> Body = 'This message is S/MIME signed.' ;
2013-07-09 13:46:44 +02:00
$this -> buildBody ();
2013-01-31 11:48:45 +01:00
$dn = array (
2013-01-31 12:04:26 +01:00
'countryName' => 'UK' ,
'stateOrProvinceName' => 'Here' ,
'localityName' => 'There' ,
'organizationName' => 'PHP' ,
'organizationalUnitName' => 'PHPMailer' ,
'commonName' => 'PHPMailer Test' ,
'emailAddress' => 'phpmailer@example.com'
2013-01-31 11:48:45 +01:00
);
2015-03-06 16:35:29 +01:00
$keyconfig = array (
" digest_alg " => " sha256 " ,
" private_key_bits " => 2048 ,
" private_key_type " => OPENSSL_KEYTYPE_RSA ,
);
2013-01-31 11:48:45 +01:00
$password = 'password' ;
2016-03-16 10:02:38 +01:00
$certfile = 'certfile.pem' ;
$keyfile = 'keyfile.pem' ;
2013-01-31 11:48:45 +01:00
//Make a new key pair
2015-03-06 16:35:29 +01:00
$pk = openssl_pkey_new ( $keyconfig );
2013-01-31 11:48:45 +01:00
//Create a certificate signing request
$csr = openssl_csr_new ( $dn , $pk );
//Create a self-signed cert
$cert = openssl_csr_sign ( $csr , null , $pk , 1 );
//Save the cert
openssl_x509_export ( $cert , $certout );
file_put_contents ( $certfile , $certout );
//Save the key
openssl_pkey_export ( $pk , $pkeyout , $password );
file_put_contents ( $keyfile , $pkeyout );
2013-07-09 13:46:44 +02:00
$this -> Mail -> sign (
2013-01-31 11:48:45 +01:00
$certfile ,
$keyfile ,
$password
);
2013-07-09 13:46:44 +02:00
$this -> assertTrue ( $this -> Mail -> send (), 'S/MIME signing failed' );
2015-03-16 14:10:14 +01:00
$msg = $this -> Mail -> getSentMIMEMessage ();
$this -> assertNotContains ( " \r \n \r \n MIME-Version: " , $msg , 'Incorrect MIME headers' );
2013-01-31 11:48:45 +01:00
unlink ( $certfile );
unlink ( $keyfile );
2012-11-21 16:12:07 +01:00
}
2015-03-06 16:35:29 +01:00
/**
* S / MIME Signing tests using a CA chain cert .
* To test that a generated message is signed correctly , save the message in a file
* and use openssl along with the certs generated by this script :
* `openssl smime -verify -in signed.eml -signer certfile.pem -CAfile cacertfile.pem`
2015-03-16 14:10:14 +01:00
* @ requires extension openssl
2015-03-06 16:35:29 +01:00
*/
public function testSigningWithCA ()
{
$this -> Mail -> Subject .= ': S/MIME signing with CA' ;
$this -> Mail -> Body = 'This message is S/MIME signed with an extra CA cert.' ;
$this -> buildBody ();
$certprops = array (
'countryName' => 'UK' ,
'stateOrProvinceName' => 'Here' ,
'localityName' => 'There' ,
'organizationName' => 'PHP' ,
'organizationalUnitName' => 'PHPMailer' ,
'commonName' => 'PHPMailer Test' ,
'emailAddress' => 'phpmailer@example.com'
);
$cacertprops = array (
'countryName' => 'UK' ,
'stateOrProvinceName' => 'Here' ,
'localityName' => 'There' ,
'organizationName' => 'PHP' ,
'organizationalUnitName' => 'PHPMailer CA' ,
'commonName' => 'PHPMailer Test CA' ,
'emailAddress' => 'phpmailer@example.com'
);
$keyconfig = array (
" digest_alg " => " sha256 " ,
" private_key_bits " => 2048 ,
" private_key_type " => OPENSSL_KEYTYPE_RSA ,
);
$password = 'password' ;
$cacertfile = 'cacertfile.pem' ;
$cakeyfile = 'cakeyfile.pem' ;
$certfile = 'certfile.pem' ;
$keyfile = 'keyfile.pem' ;
//Create a CA cert
//Make a new key pair
$capk = openssl_pkey_new ( $keyconfig );
//Create a certificate signing request
$csr = openssl_csr_new ( $cacertprops , $capk );
//Create a self-signed cert
$cert = openssl_csr_sign ( $csr , null , $capk , 1 );
//Save the CA cert
openssl_x509_export ( $cert , $certout );
file_put_contents ( $cacertfile , $certout );
//Save the CA key
openssl_pkey_export ( $capk , $pkeyout , $password );
file_put_contents ( $cakeyfile , $pkeyout );
//Create a cert signed by our CA
//Make a new key pair
$pk = openssl_pkey_new ( $keyconfig );
//Create a certificate signing request
$csr = openssl_csr_new ( $certprops , $pk );
//Create a self-signed cert
$cert = openssl_csr_sign ( $csr , 'file://' . $cacertfile , $capk , 1 );
//Save the cert
openssl_x509_export ( $cert , $certout );
file_put_contents ( $certfile , $certout );
//Save the key
openssl_pkey_export ( $pk , $pkeyout , $password );
file_put_contents ( $keyfile , $pkeyout );
$this -> Mail -> sign (
$certfile ,
$keyfile ,
$password ,
$cacertfile
);
$this -> assertTrue ( $this -> Mail -> send (), 'S/MIME signing with CA failed' );
unlink ( $cacertfile );
unlink ( $cakeyfile );
unlink ( $certfile );
unlink ( $keyfile );
}
2013-07-09 13:46:44 +02:00
/**
2015-03-04 10:29:46 +01:00
* DKIM Signing tests .
2015-03-16 14:10:14 +01:00
* @ requires extension openssl
2013-07-09 13:46:44 +02:00
*/
public function testDKIM ()
{
$this -> Mail -> Subject .= ': DKIM signing' ;
$this -> Mail -> Body = 'This message is DKIM signed.' ;
$this -> buildBody ();
$privatekeyfile = 'dkim_private.key' ;
//Make a new key pair
//(2048 bits is the recommended minimum key length -
//gmail won't accept less than 1024 bits)
$pk = openssl_pkey_new (
array (
'private_key_bits' => 2048 ,
'private_key_type' => OPENSSL_KEYTYPE_RSA
)
);
openssl_pkey_export_to_file ( $pk , $privatekeyfile );
$this -> Mail -> DKIM_domain = 'example.com' ;
$this -> Mail -> DKIM_private = $privatekeyfile ;
$this -> Mail -> DKIM_selector = 'phpmailer' ;
$this -> Mail -> DKIM_passphrase = '' ; //key is not encrypted
$this -> assertTrue ( $this -> Mail -> send (), 'DKIM signed mail failed' );
unlink ( $privatekeyfile );
}
2013-05-29 02:15:17 +02:00
/**
2015-03-04 10:29:46 +01:00
* Test line break reformatting .
2013-05-29 02:15:17 +02:00
*/
2013-07-09 13:46:44 +02:00
public function testLineBreaks ()
2013-05-29 02:15:17 +02:00
{
2013-07-09 13:46:44 +02:00
$unixsrc = " hello \n World \n Again \n " ;
$macsrc = " hello \r World \r Again \r " ;
$windowssrc = " hello \r \n World \r \n Again \r \n " ;
$mixedsrc = " hello \n World \r Again \r \n " ;
$target = " hello \r \n World \r \n Again \r \n " ;
$this -> assertEquals ( $target , PHPMailer :: normalizeBreaks ( $unixsrc ), 'UNIX break reformatting failed' );
$this -> assertEquals ( $target , PHPMailer :: normalizeBreaks ( $macsrc ), 'Mac break reformatting failed' );
$this -> assertEquals ( $target , PHPMailer :: normalizeBreaks ( $windowssrc ), 'Windows break reformatting failed' );
$this -> assertEquals ( $target , PHPMailer :: normalizeBreaks ( $mixedsrc ), 'Mixed break reformatting failed' );
2013-05-29 02:15:17 +02:00
}
2014-11-14 10:15:45 +01:00
/**
* Test line length detection
*/
public function testLineLength ()
{
$oklen = str_repeat ( str_repeat ( '0' , PHPMailer :: MAX_LINE_LENGTH ) . " \r \n " , 10 );
$badlen = str_repeat ( str_repeat ( '1' , PHPMailer :: MAX_LINE_LENGTH + 1 ) . " \r \n " , 2 );
$this -> assertTrue ( PHPMailer :: hasLineLongerThanMax ( $badlen ), 'Long line not detected (only)' );
$this -> assertTrue ( PHPMailer :: hasLineLongerThanMax ( $oklen . $badlen ), 'Long line not detected (first)' );
$this -> assertTrue ( PHPMailer :: hasLineLongerThanMax ( $badlen . $oklen ), 'Long line not detected (last)' );
2015-03-20 20:58:32 +01:00
$this -> assertTrue (
PHPMailer :: hasLineLongerThanMax ( $oklen . $badlen . $oklen ),
'Long line not detected (middle)'
);
2014-11-14 10:15:45 +01:00
$this -> assertFalse ( PHPMailer :: hasLineLongerThanMax ( $oklen ), 'Long line false positive' );
2015-03-20 20:58:32 +01:00
$this -> Mail -> isHTML ( false );
$this -> Mail -> Subject .= " : Line length test " ;
$this -> Mail -> CharSet = 'UTF-8' ;
$this -> Mail -> Encoding = '8bit' ;
$this -> Mail -> Body = $oklen . $badlen . $oklen . $badlen ;
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
$this -> assertEquals ( 'quoted-printable' , $this -> Mail -> Encoding , 'Long line did not override transfer encoding' );
2014-11-14 10:15:45 +01:00
}
2013-08-30 18:51:37 +02:00
/**
2015-03-04 10:29:46 +01:00
* Test setting and retrieving message ID .
2013-08-30 18:51:37 +02:00
*/
public function testMessageID ()
{
$this -> Mail -> Body = 'Test message ID.' ;
$id = md5 ( 12345 );
$this -> Mail -> MessageID = $id ;
$this -> buildBody ();
$this -> Mail -> preSend ();
$lastid = $this -> Mail -> getLastMessageID ();
2016-01-07 16:28:39 +01:00
$this -> assertNotEquals ( $lastid , $id , 'Invalid Message ID allowed' );
$id = '<' . md5 ( 12345 ) . '@example.com>' ;
$this -> Mail -> MessageID = $id ;
$this -> buildBody ();
$this -> Mail -> preSend ();
$lastid = $this -> Mail -> getLastMessageID ();
$this -> assertEquals ( $lastid , $id , 'Custom Message ID not used' );
$this -> Mail -> MessageID = '' ;
$this -> buildBody ();
$this -> Mail -> preSend ();
$lastid = $this -> Mail -> getLastMessageID ();
$this -> assertRegExp ( '/^<.*@.*>$/' , $lastid , 'Invalid default Message ID' );
2013-08-30 18:51:37 +02:00
}
2012-11-21 16:12:07 +01:00
/**
2015-03-04 10:29:46 +01:00
* Miscellaneous calls to improve test coverage and some small tests .
2012-11-21 16:12:07 +01:00
*/
2013-07-09 13:46:44 +02:00
public function testMiscellaneous ()
2012-11-21 16:12:07 +01:00
{
$this -> assertEquals ( 'application/pdf' , PHPMailer :: _mime_types ( 'pdf' ), 'MIME TYPE lookup failed' );
2013-07-09 13:46:44 +02:00
$this -> Mail -> addCustomHeader ( 'SomeHeader: Some Value' );
$this -> Mail -> clearCustomHeaders ();
$this -> Mail -> clearAttachments ();
$this -> Mail -> isHTML ( false );
$this -> Mail -> isSMTP ();
$this -> Mail -> isMail ();
$this -> Mail -> isSendmail ();
$this -> Mail -> isQmail ();
$this -> Mail -> setLanguage ( 'fr' );
2012-11-21 16:12:07 +01:00
$this -> Mail -> Sender = '' ;
2013-07-09 13:46:44 +02:00
$this -> Mail -> createHeader ();
2012-11-21 16:12:07 +01:00
$this -> assertFalse ( $this -> Mail -> set ( 'x' , 'y' ), 'Invalid property set succeeded' );
$this -> assertTrue ( $this -> Mail -> set ( 'Timeout' , 11 ), 'Valid property set failed' );
2015-01-28 12:18:11 +01:00
$this -> assertTrue ( $this -> Mail -> set ( 'AllowEmpty' , null ), 'Null property set failed' );
$this -> assertTrue ( $this -> Mail -> set ( 'AllowEmpty' , false ), 'Valid property set of null property failed' );
2013-04-25 12:24:43 +02:00
//Test pathinfo
$a = '/mnt/files/飛兒樂 團光茫.mp3' ;
$q = PHPMailer :: mb_pathinfo ( $a );
$this -> assertEquals ( $q [ 'dirname' ], '/mnt/files' , 'UNIX dirname not matched' );
$this -> assertEquals ( $q [ 'basename' ], '飛兒樂 團光茫.mp3' , 'UNIX basename not matched' );
$this -> assertEquals ( $q [ 'extension' ], 'mp3' , 'UNIX extension not matched' );
$this -> assertEquals ( $q [ 'filename' ], '飛兒樂 團光茫' , 'UNIX filename not matched' );
2013-07-09 13:46:44 +02:00
$this -> assertEquals (
PHPMailer :: mb_pathinfo ( $a , PATHINFO_DIRNAME ),
'/mnt/files' ,
'Dirname path element not matched'
);
2013-04-25 12:24:43 +02:00
$this -> assertEquals ( PHPMailer :: mb_pathinfo ( $a , 'filename' ), '飛兒樂 團光茫' , 'Filename path element not matched' );
$a = 'c:\mnt\files\飛兒樂 團光茫.mp3' ;
$q = PHPMailer :: mb_pathinfo ( $a );
$this -> assertEquals ( $q [ 'dirname' ], 'c:\mnt\files' , 'Windows dirname not matched' );
$this -> assertEquals ( $q [ 'basename' ], '飛兒樂 團光茫.mp3' , 'Windows basename not matched' );
$this -> assertEquals ( $q [ 'extension' ], 'mp3' , 'Windows extension not matched' );
$this -> assertEquals ( $q [ 'filename' ], '飛兒樂 團光茫' , 'Windows filename not matched' );
2012-11-21 16:12:07 +01:00
}
2015-11-01 11:12:04 +01:00
public function testBadSMTP ()
2014-04-08 13:32:46 +02:00
{
2015-11-01 11:12:04 +01:00
$this -> Mail -> smtpConnect ();
$smtp = $this -> Mail -> getSMTPInstance ();
$this -> assertFalse ( $smtp -> mail ( " somewhere \n bad " ), 'Bad SMTP command containing breaks accepted' );
2014-04-08 13:32:46 +02:00
}
2015-04-23 18:41:17 +02:00
/**
* Tests the Custom header getter
*/
public function testCustomHeaderGetter ()
{
$this -> Mail -> addCustomHeader ( 'foo' , 'bar' );
$this -> assertEquals ( array ( array ( 'foo' , 'bar' )), $this -> Mail -> getCustomHeaders ());
$this -> Mail -> addCustomHeader ( 'foo' , 'baz' );
$this -> assertEquals ( array (
array ( 'foo' , 'bar' ),
array ( 'foo' , 'baz' )
), $this -> Mail -> getCustomHeaders ());
$this -> Mail -> clearCustomHeaders ();
$this -> assertEmpty ( $this -> Mail -> getCustomHeaders ());
$this -> Mail -> addCustomHeader ( 'yux' );
$this -> assertEquals ( array ( array ( 'yux' )), $this -> Mail -> getCustomHeaders ());
$this -> Mail -> addCustomHeader ( 'Content-Type: application/json' );
$this -> assertEquals ( array (
array ( 'yux' ),
array ( 'Content-Type' , ' application/json' )
), $this -> Mail -> getCustomHeaders ());
}
2015-10-14 21:39:29 +02:00
/**
* Tests setting and retrieving ConfirmReadingTo address , also known as " read receipt " address .
*/
public function testConfirmReadingTo ()
{
$this -> Mail -> CharSet = 'utf-8' ;
$this -> buildBody ();
$this -> Mail -> ConfirmReadingTo = 'test@example..com' ; //Invalid address
$this -> assertFalse ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
$this -> Mail -> ConfirmReadingTo = ' test@example.com' ; //Extra space to trim
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
$this -> assertEquals (
'test@example.com' ,
$this -> Mail -> ConfirmReadingTo ,
'Unexpected read receipt address' );
$this -> Mail -> ConfirmReadingTo = 'test@françois.ch' ; //Address with IDN
if ( $this -> Mail -> idnSupported ()) {
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
$this -> assertEquals (
'test@xn--franois-xxa.ch' ,
$this -> Mail -> ConfirmReadingTo ,
'IDN address not converted to punycode' );
} else {
$this -> assertFalse ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
}
}
/**
* Tests CharSet and Unicode -> ASCII conversions for addresses with IDN .
*/
public function testConvertEncoding ()
{
if ( ! $this -> Mail -> idnSupported ()) {
$this -> markTestSkipped ( 'intl and/or mbstring extensions are not available' );
}
$this -> Mail -> clearAllRecipients ();
$this -> Mail -> clearReplyTos ();
// This file is UTF-8 encoded. Create a domain encoded in "iso-8859-1".
$domain = '@' . mb_convert_encoding ( 'françois.ch' , 'ISO-8859-1' , 'UTF-8' );
$this -> Mail -> addAddress ( 'test' . $domain );
$this -> Mail -> addCC ( 'test+cc' . $domain );
$this -> Mail -> addBCC ( 'test+bcc' . $domain );
$this -> Mail -> addReplyTo ( 'test+replyto' . $domain );
// Queued addresses are not returned by get*Addresses() before send() call.
$this -> assertEmpty ( $this -> Mail -> getToAddresses (), 'Bad "to" recipients' );
$this -> assertEmpty ( $this -> Mail -> getCcAddresses (), 'Bad "cc" recipients' );
$this -> assertEmpty ( $this -> Mail -> getBccAddresses (), 'Bad "bcc" recipients' );
$this -> assertEmpty ( $this -> Mail -> getReplyToAddresses (), 'Bad "reply-to" recipients' );
// Clear queued BCC recipient.
$this -> Mail -> clearBCCs ();
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
// Addresses with IDN are returned by get*Addresses() after send() call.
$domain = $this -> Mail -> punyencodeAddress ( $domain );
$this -> assertEquals (
array ( array ( 'test' . $domain , '' )),
$this -> Mail -> getToAddresses (),
'Bad "to" recipients' );
$this -> assertEquals (
array ( array ( 'test+cc' . $domain , '' )),
$this -> Mail -> getCcAddresses (),
'Bad "cc" recipients' );
$this -> assertEmpty ( $this -> Mail -> getBccAddresses (), 'Bad "bcc" recipients' );
$this -> assertEquals (
array ( 'test+replyto' . $domain => array ( 'test+replyto' . $domain , '' )),
$this -> Mail -> getReplyToAddresses (),
'Bad "reply-to" addresses' );
}
/**
* Tests removal of duplicate recipients and reply - tos .
*/
public function testDuplicateIDNRemoved ()
{
if ( ! $this -> Mail -> idnSupported ()) {
$this -> markTestSkipped ( 'intl and/or mbstring extensions are not available' );
}
$this -> Mail -> clearAllRecipients ();
$this -> Mail -> clearReplyTos ();
$this -> Mail -> CharSet = 'utf-8' ;
$this -> assertTrue ( $this -> Mail -> addAddress ( 'test@françois.ch' ));
$this -> assertFalse ( $this -> Mail -> addAddress ( 'test@françois.ch' ));
$this -> assertTrue ( $this -> Mail -> addAddress ( 'test@FRANÇOIS.CH' ));
$this -> assertFalse ( $this -> Mail -> addAddress ( 'test@FRANÇOIS.CH' ));
$this -> assertTrue ( $this -> Mail -> addAddress ( 'test@xn--franois-xxa.ch' ));
$this -> assertFalse ( $this -> Mail -> addAddress ( 'test@xn--franois-xxa.ch' ));
$this -> assertFalse ( $this -> Mail -> addAddress ( 'test@XN--FRANOIS-XXA.CH' ));
$this -> assertTrue ( $this -> Mail -> addReplyTo ( 'test+replyto@françois.ch' ));
$this -> assertFalse ( $this -> Mail -> addReplyTo ( 'test+replyto@françois.ch' ));
$this -> assertTrue ( $this -> Mail -> addReplyTo ( 'test+replyto@FRANÇOIS.CH' ));
$this -> assertFalse ( $this -> Mail -> addReplyTo ( 'test+replyto@FRANÇOIS.CH' ));
$this -> assertTrue ( $this -> Mail -> addReplyTo ( 'test+replyto@xn--franois-xxa.ch' ));
$this -> assertFalse ( $this -> Mail -> addReplyTo ( 'test+replyto@xn--franois-xxa.ch' ));
$this -> assertFalse ( $this -> Mail -> addReplyTo ( 'test+replyto@XN--FRANOIS-XXA.CH' ));
$this -> buildBody ();
$this -> assertTrue ( $this -> Mail -> send (), $this -> Mail -> ErrorInfo );
// There should be only one "To" address and one "Reply-To" address.
$this -> assertEquals (
1 ,
count ( $this -> Mail -> getToAddresses ()),
'Bad count of "to" recipients' );
$this -> assertEquals (
1 ,
count ( $this -> Mail -> getReplyToAddresses ()),
'Bad count of "reply-to" addresses' );
}
2015-11-01 11:12:04 +01:00
/**
* Use a fake POP3 server to test POP - before - SMTP auth .
* With a known - good login
*/
public function testPopBeforeSmtpGood ()
{
//Start a fake POP server
$pid = shell_exec ( 'nohup ./runfakepopserver.sh >/dev/null 2>/dev/null & printf "%u" $!' );
$this -> pids [] = $pid ;
sleep ( 2 );
//Test a known-good login
$this -> assertTrue (
POP3 :: popBeforeSmtp ( 'localhost' , 1100 , 10 , 'user' , 'test' , $this -> Mail -> SMTPDebug ),
'POP before SMTP failed'
);
//Kill the fake server
shell_exec ( 'kill -TERM ' . escapeshellarg ( $pid ));
sleep ( 2 );
}
/**
2016-03-29 15:14:19 +02:00
* Use a fake POP3 server to test POP - before - SMTP auth
* with a known - bad login .
2015-11-01 11:12:04 +01:00
*/
public function testPopBeforeSmtpBad ()
{
//Start a fake POP server on a different port
//so we don't inadvertently connect to the previous instance
$pid = shell_exec ( 'nohup ./runfakepopserver.sh 1101 >/dev/null 2>/dev/null & printf "%u" $!' );
$this -> pids [] = $pid ;
sleep ( 2 );
//Test a known-bad login
$this -> assertFalse (
POP3 :: popBeforeSmtp ( 'localhost' , 1101 , 10 , 'user' , 'xxx' , $this -> Mail -> SMTPDebug ),
'POP before SMTP should have failed'
);
shell_exec ( 'kill -TERM ' . escapeshellarg ( $pid ));
sleep ( 2 );
}
/**
* Test SMTP host connections .
* This test can take a long time , so run it last
*/
public function testSmtpConnect ()
{
$this -> Mail -> SMTPDebug = 4 ; //Show connection-level errors
$this -> assertTrue ( $this -> Mail -> smtpConnect (), 'SMTP single connect failed' );
$this -> Mail -> smtpClose ();
$this -> Mail -> Host = " ssl://localhost:12345;tls://localhost:587;10.10.10.10:54321;localhost:12345;10.10.10.10 " ;
$this -> assertFalse ( $this -> Mail -> smtpConnect (), 'SMTP bad multi-connect succeeded' );
$this -> Mail -> smtpClose ();
$this -> Mail -> Host = " localhost:12345;10.10.10.10:54321; " . $_REQUEST [ 'mail_host' ];
$this -> assertTrue ( $this -> Mail -> smtpConnect (), 'SMTP multi-connect failed' );
$this -> Mail -> smtpClose ();
$this -> Mail -> Host = " localhost:12345 ; " . $_REQUEST [ 'mail_host' ] . ' ' ;
$this -> assertTrue ( $this -> Mail -> smtpConnect (), 'SMTP hosts with stray spaces failed' );
$this -> Mail -> smtpClose ();
$this -> Mail -> Host = $_REQUEST [ 'mail_host' ];
//Need to pick a harmless option so as not cause problems of its own! socket:bind doesn't work with Travis-CI
$this -> assertTrue (
$this -> Mail -> smtpConnect ( array ( 'ssl' => array ( 'verify_depth' => 10 ))),
'SMTP connect with options failed'
);
}
2012-11-21 16:12:07 +01:00
}
2009-05-05 13:22:26 +02:00
/**
2012-11-21 16:12:07 +01:00
* This is a sample form for setting appropriate test values through a browser
2015-11-01 11:12:04 +01:00
* These values can also be set using a file called testbootstrap . php ( not in repo ) in the same folder as this script
2012-11-21 16:12:07 +01:00
* which is probably more useful if you run these tests a lot
2013-07-09 13:46:44 +02:00
* < html >
* < body >
* < h3 > phpmailer Unit Test </ h3 >
* By entering a SMTP hostname it will automatically perform tests with SMTP .
*
* < form name = " phpmailer_unit " action = __FILE__ method = " get " >
* < input type = " hidden " name = " submitted " value = " 1 " />
* From Address : < input type = " text " size = " 50 " name = " mail_from " value = " <?php echo get( " mail_from " ); ?> " />
* < br />
* To Address : < input type = " text " size = " 50 " name = " mail_to " value = " <?php echo get( " mail_to " ); ?> " />
* < br />
* Cc Address : < input type = " text " size = " 50 " name = " mail_cc " value = " <?php echo get( " mail_cc " ); ?> " />
* < br />
* SMTP Hostname : < input type = " text " size = " 50 " name = " mail_host " value = " <?php echo get( " mail_host " ); ?> " />
* < p />
* < input type = " submit " value = " Run Test " />
*
* </ form >
* </ body >
* </ html >
2009-05-05 13:22:26 +02:00
*/