| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224 |
- <?php
- /**
- * PHPMailer - PHP email transport unit tests.
- *
- * PHP version 5.5
- * @package PHPMailer
- * @author Marcus Bointon <phpmailer@synchromedia.co.uk>
- * @author Andy Prevost
- * @copyright 2012 - 2016 Marcus Bointon
- * @copyright 2004 - 2009 Andy Prevost
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- */
- namespace PHPMailer\PHPMailer;
- /**
- * PHPMailer - PHP email transport unit test class.
- */
- class PHPMailerTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * Holds the phpmailer instance.
- * @private
- * @var PHPMailer
- */
- public $Mail;
- /**
- * Holds the SMTP mail host.
- * @public
- * @var string
- */
- public $Host = '';
- /**
- * Holds the change log.
- * @private
- * @var string[]
- */
- public $ChangeLog = [];
- /**
- * Holds the note log.
- * @private
- * @var string[]
- */
- public $NoteLog = [];
- /**
- * Default include path
- * @var string
- */
- public $INCLUDE_DIR = '..';
- /**
- * PIDs of any processes we need to kill
- * @var array
- * @access private
- */
- private $pids = [];
- /**
- * Run before each test is started.
- */
- public function setUp()
- {
- $this->INCLUDE_DIR = dirname(__DIR__); //Default to the dir above the test dir, i.e. the project home dir
- if (file_exists($this->INCLUDE_DIR . '/test/testbootstrap.php')) {
- include $this->INCLUDE_DIR . '/test/testbootstrap.php'; //Overrides go in here
- }
- $this->Mail = new PHPMailer;
- $this->Mail->SMTPDebug = 3; //Full debug output
- $this->Mail->Priority = 3;
- $this->Mail->Encoding = '8bit';
- $this->Mail->CharSet = 'iso-8859-1';
- if (array_key_exists('mail_from', $_REQUEST)) {
- $this->Mail->From = $_REQUEST['mail_from'];
- } else {
- $this->Mail->From = 'unit_test@phpmailer.example.com';
- }
- $this->Mail->FromName = 'Unit Tester';
- $this->Mail->Sender = '';
- $this->Mail->Subject = 'Unit Test';
- $this->Mail->Body = '';
- $this->Mail->AltBody = '';
- $this->Mail->WordWrap = 0;
- if (array_key_exists('mail_host', $_REQUEST)) {
- $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;
- }
- $this->Mail->Helo = 'localhost.localdomain';
- $this->Mail->SMTPAuth = false;
- $this->Mail->Username = '';
- $this->Mail->Password = '';
- $this->Mail->addReplyTo('no_reply@phpmailer.example.com', 'Reply Guy');
- $this->Mail->Sender = 'unit_test@phpmailer.example.com';
- if (strlen($this->Mail->Host) > 0) {
- $this->Mail->Mailer = 'smtp';
- } else {
- $this->Mail->Mailer = 'mail';
- }
- if (array_key_exists('mail_to', $_REQUEST)) {
- $this->setAddress($_REQUEST['mail_to'], 'Test User', 'to');
- }
- if (array_key_exists('mail_cc', $_REQUEST) and strlen($_REQUEST['mail_cc']) > 0) {
- $this->setAddress($_REQUEST['mail_cc'], 'Carbon User', 'cc');
- }
- }
- /**
- * Run after each test is completed.
- */
- public function tearDown()
- {
- // Clean global variables
- $this->Mail = null;
- $this->ChangeLog = [];
- $this->NoteLog = [];
- foreach ($this->pids as $pid) {
- $p = escapeshellarg($pid);
- shell_exec("ps $p && kill -TERM $p");
- }
- }
- /**
- * Build the body of the message in the appropriate format.
- *
- * @private
- * @return void
- */
- public function buildBody()
- {
- $this->checkChanges();
- // Determine line endings for message
- if ('text/html' == $this->Mail->ContentType || strlen($this->Mail->AltBody) > 0) {
- $eol = "<br>\r\n";
- $bullet_start = '<li>';
- $bullet_end = "</li>\r\n";
- $list_start = "<ul>\r\n";
- $list_end = "</ul>\r\n";
- } else {
- $eol = "\r\n";
- $bullet_start = ' - ';
- $bullet_end = "\r\n";
- $list_start = '';
- $list_end = '';
- }
- $ReportBody = '';
- $ReportBody .= '---------------------' . $eol;
- $ReportBody .= 'Unit Test Information' . $eol;
- $ReportBody .= '---------------------' . $eol;
- $ReportBody .= 'phpmailer version: ' . PHPMailer::VERSION . $eol;
- $ReportBody .= 'Content Type: ' . $this->Mail->ContentType . $eol;
- $ReportBody .= 'CharSet: ' . $this->Mail->CharSet . $eol;
- if (strlen($this->Mail->Host) > 0) {
- $ReportBody .= 'Host: ' . $this->Mail->Host . $eol;
- }
- // If attachments then create an attachment list
- $attachments = $this->Mail->getAttachments();
- if (count($attachments) > 0) {
- $ReportBody .= 'Attachments:' . $eol;
- $ReportBody .= $list_start;
- foreach ($attachments as $attachment) {
- $ReportBody .= $bullet_start . 'Name: ' . $attachment[1] . ', ';
- $ReportBody .= 'Encoding: ' . $attachment[3] . ', ';
- $ReportBody .= 'Type: ' . $attachment[4] . $bullet_end;
- }
- $ReportBody .= $list_end . $eol;
- }
- // If there are changes then list them
- if (count($this->ChangeLog) > 0) {
- $ReportBody .= 'Changes' . $eol;
- $ReportBody .= '-------' . $eol;
- $ReportBody .= $list_start;
- for ($i = 0; $i < count($this->ChangeLog); $i++) {
- $ReportBody .= $bullet_start . $this->ChangeLog[$i][0] . ' was changed to [' .
- $this->ChangeLog[$i][1] . ']' . $bullet_end;
- }
- $ReportBody .= $list_end . $eol . $eol;
- }
- // If there are notes then list them
- if (count($this->NoteLog) > 0) {
- $ReportBody .= 'Notes' . $eol;
- $ReportBody .= '-----' . $eol;
- $ReportBody .= $list_start;
- for ($i = 0; $i < count($this->NoteLog); $i++) {
- $ReportBody .= $bullet_start . $this->NoteLog[$i] . $bullet_end;
- }
- $ReportBody .= $list_end;
- }
- // Re-attach the original body
- $this->Mail->Body .= $eol . $ReportBody;
- }
- /**
- * Check which default settings have been changed for the report.
- * @private
- * @return void
- */
- public function checkChanges()
- {
- if (3 != $this->Mail->Priority) {
- $this->addChange('Priority', $this->Mail->Priority);
- }
- if ('8bit' != $this->Mail->Encoding) {
- $this->addChange('Encoding', $this->Mail->Encoding);
- }
- if ('iso-8859-1' != $this->Mail->CharSet) {
- $this->addChange('CharSet', $this->Mail->CharSet);
- }
- if ('' != $this->Mail->Sender) {
- $this->addChange('Sender', $this->Mail->Sender);
- }
- if (0 != $this->Mail->WordWrap) {
- $this->addChange('WordWrap', $this->Mail->WordWrap);
- }
- if ('mail' != $this->Mail->Mailer) {
- $this->addChange('Mailer', $this->Mail->Mailer);
- }
- if (25 != $this->Mail->Port) {
- $this->addChange('Port', $this->Mail->Port);
- }
- if ('localhost.localdomain' != $this->Mail->Helo) {
- $this->addChange('Helo', $this->Mail->Helo);
- }
- if ($this->Mail->SMTPAuth) {
- $this->addChange('SMTPAuth', 'true');
- }
- }
- /**
- * Add a changelog entry.
- * @access private
- * @param string $sName
- * @param string $sNewValue
- * @return void
- */
- public function addChange($sName, $sNewValue)
- {
- $this->ChangeLog[] = [$sName, $sNewValue];
- }
- /**
- * Adds a simple note to the message.
- * @public
- * @param string $sValue
- * @return void
- */
- public function addNote($sValue)
- {
- $this->NoteLog[] = $sValue;
- }
- /**
- * Adds all of the addresses
- * @access public
- * @param string $sAddress
- * @param string $sName
- * @param string $sType
- * @return boolean
- */
- public function setAddress($sAddress, $sName = '', $sType = 'to')
- {
- switch ($sType) {
- case 'to':
- return $this->Mail->addAddress($sAddress, $sName);
- case 'cc':
- return $this->Mail->addCC($sAddress, $sName);
- case 'bcc':
- return $this->Mail->addBCC($sAddress, $sName);
- }
- return false;
- }
- /**
- * 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(
- file_exists($this->INCLUDE_DIR . '/test/testbootstrap.php'),
- 'Test config params missing - copy testbootstrap.php to testbootstrap-dist.php and change as appropriate'
- );
- }
- /**
- * Test CRAM-MD5 authentication.
- * Needs a connection to a server that supports this auth mechanism, so commented out by default
- */
- public function testAuthCRAMMD5()
- {
- $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';
- $this->Mail->Subject .= ': Auth CRAM-MD5';
- $this->Mail->From = 'from@example.com';
- $this->Mail->Sender = 'from@example.com';
- $this->Mail->clearAllRecipients();
- $this->Mail->addAddress('user@example.com');
- //$this->assertTrue($this->mail->send(), $this->mail->ErrorInfo);
- }
- /**
- * Test email address validation.
- * Test addresses obtained from http://isemail.info
- * Some failing cases commented out that are apparently up for debate!
- */
- public function testValidate()
- {
- $validaddresses = [
- '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]',
- 'first.last@[IPv6:1111:2222:3333::4444:12.34.56.78]',
- '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',
- 'first.last@[IPv6:1111:2222:3333::4444:5555:12.34.56.78]',
- 'first.last@[IPv6:1111:2222:3333::4444:5555:6666:7777]',
- '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',
- '"first\"last"@iana.org',
- 'first."mid\dle"."last"@iana.org',
- '"test blah"@iana.org',
- '(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',
- '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',
- 'first.last@[IPv6:::a2:a3:a4:b1:b2:b3:b4]',
- 'first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3::]',
- '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]',
- 'first.last@[IPv6:::a2:a3:a4:b1:ffff:11.22.33.44]',
- 'first.last@[IPv6:a1:a2:a3:a4::11.22.33.44]',
- 'first.last@[IPv6:a1:a2:a3:a4:b1::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.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',
- 'test@example.com'
- ];
- $invalidaddresses = [
- 'first.last@sub.do,com',
- 'first\@last@iana.org',
- '123456789012345678901234567890123456789012345678901234567890' .
- '@12345678901234567890123456789012345678901234 [...]',
- 'first.last',
- '12345678901234567890123456789012345678901234567890123456789012345@iana.org',
- '.first.last@iana.org',
- 'first.last.@iana.org',
- 'first..last@iana.org',
- '"first"last"@iana.org',
- '"""@iana.org',
- '"\"@iana.org',
- //'""@iana.org',
- 'first\@last@iana.org',
- 'first.last@',
- 'x@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.' .
- 'x23456789.x23456789.x23456789.x23 [...]',
- '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',
- //'helloworld@iana .org',
- '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',
- 'test@12345678901234567890123456789012345678901234567890123456789012345678901234567890' .
- '12345678901234567890 [...]',
- '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]',
- //'first."".last@iana.org',
- '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',
- //'"foo"(yay)@(hoopla)[1.2.3.4]',
- 'cal(foo(bar)@iamcal.com',
- 'cal(foo)bar)@iamcal.com',
- 'cal(foo\)@iamcal.com',
- 'first(12345678901234567890123456789012345678901234567890)last@(1234567890123456789' .
- '01234567890123456789012 [...]',
- 'first(middle)last@iana.org',
- 'first(abc("def".ghi).mno)middle(abc("def".ghi).mno).last@(abc("def".ghi).mno)example' .
- '(abc("def".ghi).mno). [...]',
- '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',
- '"Unicode NULL' . chr(0) . '"@char.com',
- 'Unicode NULL' . chr(0) . '@char.com',
- '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]',
- 'first.last@[IPv6:a1:a2:a3:a4::b1:b2:b3:b4]',
- //This is a valid RCC5322 address, but we don't want to allow it for obvious reasons!
- "(\r\n RCPT TO:user@example.com\r\n DATA \\\nSubject: spam10\\\n\r\n Hello," .
- "\r\n this is a spam mail.\\\n.\r\n QUIT\r\n ) a@example.net"
- ];
- // IDNs in Unicode and ASCII forms.
- $unicodeaddresses = [
- 'first.last@bücher.ch',
- 'first.last@кто.рф',
- 'first.last@phplíst.com',
- ];
- $asciiaddresses = [
- 'first.last@xn--bcher-kva.ch',
- 'first.last@xn--j1ail.xn--p1ai',
- 'first.last@xn--phplst-6va.com',
- ];
- $goodfails = [];
- foreach (array_merge($validaddresses, $asciiaddresses) as $address) {
- if (!PHPMailer::validateAddress($address)) {
- $goodfails[] = $address;
- }
- }
- $badpasses = [];
- foreach (array_merge($invalidaddresses, $unicodeaddresses) as $address) {
- if (PHPMailer::validateAddress($address)) {
- $badpasses[] = $address;
- }
- }
- $err = '';
- if (count($goodfails) > 0) {
- $err .= "Good addresses that failed validation:\n";
- $err .= implode("\n", $goodfails);
- }
- if (count($badpasses) > 0) {
- if (!empty($err)) {
- $err .= "\n\n";
- }
- $err .= "Bad addresses that passed validation:\n";
- $err .= implode("\n", $badpasses);
- }
- $this->assertEmpty($err, $err);
- //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'));
- }
- /**
- * Test injecting a custom validator.
- */
- public function testCustomValidator()
- {
- //Inject a one-off custom validator
- $this->assertTrue(
- PHPMailer::validateAddress(
- 'user@example.com',
- function ($address) {
- return (strpos($address, '@') !== false);
- }
- ),
- 'Custom validator false negative'
- );
- $this->assertFalse(
- PHPMailer::validateAddress(
- 'userexample.com',
- function ($address) {
- return (strpos($address, '@') !== false);
- }
- ),
- 'Custom validator false positive'
- );
- //Set the default validator to an injected function
- PHPMailer::$validator = function ($address) {
- return ('user@example.com' === $address);
- };
- $this->assertTrue(
- $this->Mail->addAddress('user@example.com'),
- 'Custom default validator false negative'
- );
- $this->assertFalse(
- //Need to pick a failing value which would pass all other validators
- //to be sure we're using our custom one
- $this->Mail->addAddress('bananas@example.com'),
- 'Custom default validator false positive'
- );
- //Set default validator to PHP built-in
- PHPMailer::$validator = 'php';
- $this->assertFalse(
- //This is a valid address that FILTER_VALIDATE_EMAIL thinks is invalid
- $this->Mail->addAddress('first.last@example.123'),
- 'PHP validator not behaving as expected'
- );
- }
- /**
- * Word-wrap an ASCII message.
- */
- public function testWordWrap()
- {
- $this->Mail->WordWrap = 40;
- $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
- );
- $nBodyLen = strlen($my_body);
- $my_body .= "\n\nThis is the above body length: " . $nBodyLen;
- $this->Mail->Body = $my_body;
- $this->Mail->Subject .= ': Wordwrap';
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Word-wrap a multibyte message.
- */
- public function testWordWrapMultibyte()
- {
- $this->Mail->WordWrap = 40;
- $my_body = str_repeat(
- '飛兒樂 團光茫 飛兒樂 團光茫 飛兒樂 團光茫 飛兒樂 團光茫 ' .
- '飛飛兒樂 團光茫兒樂 團光茫飛兒樂 團光飛兒樂 團光茫飛兒樂 團光茫兒樂 團光茫 ' .
- '飛兒樂 團光茫飛兒樂 團飛兒樂 團光茫光茫飛兒樂 團光茫. ',
- 10
- );
- $nBodyLen = strlen($my_body);
- $my_body .= "\n\nThis 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.
- */
- public function testLowPriority()
- {
- $this->Mail->Priority = 5;
- $this->Mail->Body = 'Here is the main body. There should be ' .
- 'a reply to address in this message.';
- $this->Mail->Subject .= ': Low Priority';
- $this->Mail->addReplyTo('nobody@nobody.com', 'Nobody (Unit Test)');
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Simple plain file attachment test.
- */
- public function testMultiplePlainFileAttachment()
- {
- $this->Mail->Body = 'Here is the text body';
- $this->Mail->Subject .= ': Plain + Multiple FileAttachments';
- if (!$this->Mail->addAttachment(realpath($this->INCLUDE_DIR . '/examples/images/phpmailer.png'))) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- if (!$this->Mail->addAttachment(__FILE__, 'test.txt')) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Simple plain string attachment test.
- */
- public function testPlainStringAttachment()
- {
- $this->Mail->Body = 'Here is the text body';
- $this->Mail->Subject .= ': Plain + StringAttachment';
- $sAttachment = 'These characters are the content of the ' .
- "string attachment.\nThis might be taken from a " .
- 'database or some other such thing. ';
- $this->Mail->addStringAttachment($sAttachment, 'string_attach.txt');
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Plain quoted-printable message.
- */
- public function testQuotedPrintable()
- {
- $this->Mail->Body = 'Here is the main body';
- $this->Mail->Subject .= ': Plain + Quoted-printable';
- $this->Mail->Encoding = 'quoted-printable';
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- //Check that a quoted printable encode and decode results in the same as went in
- $t = file_get_contents(__FILE__); //Use this file as test content
- //Force line breaks to UNIX-style
- $t = str_replace(["\r\n", "\r"], "\n", $t);
- $this->assertEquals(
- $t,
- quoted_printable_decode($this->Mail->encodeQP($t)),
- 'Quoted-Printable encoding round-trip failed'
- );
- //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)'
- );
- }
- /**
- * Send an HTML message.
- */
- public function testHtml()
- {
- $this->Mail->isHTML(true);
- $this->Mail->Subject .= ": HTML only";
- $this->Mail->Body = <<<EOT
- <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>
- Go to <a href="https://github.com/PHPMailer/PHPMailer/">https://github.com/PHPMailer/PHPMailer/</a>
- for new versions of PHPMailer.</p>
- <p>Thank you!</p>
- </body>
- </html>
- EOT;
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- $msg = $this->Mail->getSentMIMEMessage();
- $this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
- }
- /**
- * Send a message containing ISO-8859-1 text.
- */
- public function testHtmlIso8859()
- {
- $this->Mail->isHTML(true);
- $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
- $content = file_get_contents(realpath($this->INCLUDE_DIR . '/examples/contents.html'));
- // This is the string 'éèîüçÅñæß' in ISO-8859-1, base-64 encoded
- $check = base64_decode('6eju/OfF8ebf');
- //Make sure it really is in ISO-8859-1!
- $this->Mail->msgHTML(
- mb_convert_encoding(
- $content,
- "ISO-8859-1",
- mb_detect_encoding($content, "UTF-8, ISO-8859-1, ISO-8859-15", true)
- ),
- realpath($this->INCLUDE_DIR . '/examples')
- );
- $this->buildBody();
- $this->assertTrue(
- strpos($this->Mail->Body, $check) !== false,
- 'ISO message body does not contain expected text'
- );
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * 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>
- <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>
- </body>
- </html>
- EOT;
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- $msg = $this->Mail->getSentMIMEMessage();
- $this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
- }
- /**
- * 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(
- realpath($this->INCLUDE_DIR . '/examples/images/phpmailer.png'),
- 'my-attach',
- 'phpmailer.png',
- 'base64',
- 'image/png'
- );
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * 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\nMIME-Version:", $msg, 'Incorrect MIME headers');
- }
- /**
- * Test simple message builder and html2text converters
- */
- public function testMsgHTML()
- {
- $message = file_get_contents(realpath($this->INCLUDE_DIR . '/examples/contentsutf8.html'));
- $this->Mail->CharSet = 'utf-8';
- $this->Mail->Body = '';
- $this->Mail->AltBody = '';
- //Uses internal HTML to text conversion
- $this->Mail->msgHTML($message, realpath($this->INCLUDE_DIR . '/examples'));
- $sub = $this->Mail->Subject . ': msgHTML';
- $this->Mail->Subject .= $sub;
- $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);
- //Again, using a custom HTML to text converter
- $this->Mail->AltBody = '';
- $this->Mail->msgHTML(
- $message,
- realpath($this->INCLUDE_DIR .'/examples'),
- function ($html) {
- return strtoupper(strip_tags($html));
- }
- );
- $this->Mail->Subject = $sub . ' + custom html2text';
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Simple HTML and attachment test
- */
- public function testHTMLAttachment()
- {
- $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
- $this->Mail->Subject .= ': HTML + Attachment';
- $this->Mail->isHTML(true);
- if (!$this->Mail->addAttachment(
- realpath($this->INCLUDE_DIR . '/examples/images/phpmailer_mini.png'),
- 'phpmailer_mini.png'
- )
- ) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- //Make sure that trying to attach a nonexistent file fails
- $this->assertFalse($this->Mail->addAttachment(__FILE__ . md5(microtime()), 'nonexistent_file.txt'));
- $this->buildBody();
- $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(
- file_get_contents(realpath($this->INCLUDE_DIR . '/examples/images/phpmailer_mini.png')),
- md5('phpmailer_mini.png').'@phpmailer.0',
- '', //Intentionally empty name
- 'base64',
- '', //Intentionally empty MIME type
- 'inline'
- )) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * 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);
- if (!$this->Mail->addAttachment(
- realpath($this->INCLUDE_DIR . '/examples/images/phpmailer_mini.png'),
- 'phpmailer_mini.png'
- )
- ) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- if (!$this->Mail->addAttachment(
- realpath($this->INCLUDE_DIR . '/examples/images/phpmailer.png'),
- 'phpmailer.png'
- )
- ) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * An embedded attachment test.
- */
- public function testEmbeddedImage()
- {
- $this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="' .
- 'cid:my-attach">' .
- 'Here is an image!';
- $this->Mail->Subject .= ': Embedded Image';
- $this->Mail->isHTML(true);
- if (!$this->Mail->addEmbeddedImage(
- realpath($this->INCLUDE_DIR . '/examples/images/phpmailer.png'),
- 'my-attach',
- 'phpmailer.png',
- 'base64',
- 'image/png'
- )
- ) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- //For code coverage
- $this->Mail->addEmbeddedImage('thisfiledoesntexist', 'xyz'); //Non-existent file
- $this->Mail->addEmbeddedImage(__FILE__, '123'); //Missing name
- }
- /**
- * An embedded attachment test.
- */
- public function testMultiEmbeddedImage()
- {
- $this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="' .
- 'cid:my-attach">' .
- 'Here is an image!</a>';
- $this->Mail->Subject .= ': Embedded Image + Attachment';
- $this->Mail->isHTML(true);
- if (!$this->Mail->addEmbeddedImage(
- realpath($this->INCLUDE_DIR . '/examples/images/phpmailer.png'),
- 'my-attach',
- 'phpmailer.png',
- 'base64',
- 'image/png'
- )
- ) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- if (!$this->Mail->addAttachment(__FILE__, 'test.txt')) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Simple multipart/alternative test.
- */
- public function testAltBody()
- {
- $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.';
- $this->Mail->WordWrap = 40;
- $this->addNote('This is a mulipart alternative email');
- $this->Mail->Subject .= ': AltBody + Word Wrap';
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Simple HTML and attachment test
- */
- public function testAltBodyAttachment()
- {
- $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';
- $this->Mail->isHTML(true);
- if (!$this->Mail->addAttachment(__FILE__, 'test_attach.txt')) {
- $this->assertTrue(false, $this->Mail->ErrorInfo);
- return;
- }
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Test sending multiple messages with separate connections.
- */
- public function testMultipleSend()
- {
- $this->Mail->Body = 'Sending two messages without keepalive';
- $this->buildBody();
- $subject = $this->Mail->Subject;
- $this->Mail->Subject = $subject . ': SMTP 1';
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- $this->Mail->Subject = $subject . ': SMTP 2';
- $this->Mail->Sender = 'blah@example.com';
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Test sending using SendMail.
- */
- public function testSendmailSend()
- {
- $this->Mail->Body = 'Sending via sendmail';
- $this->buildBody();
- $subject = $this->Mail->Subject;
- $this->Mail->Subject = $subject . ': sendmail';
- $this->Mail->isSendmail();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Test sending using Qmail.
- */
- 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);
- } else {
- $this->markTestSkipped('Qmail is not installed');
- }
- }
- /**
- * Test sending using PHP mail() function.
- */
- public function testMailSend()
- {
- $sendmail = ini_get('sendmail_path');
- //No path in sendmail_path
- if (strpos($sendmail, '/') === false) {
- ini_set('sendmail_path', '/usr/sbin/sendmail -t -i ');
- }
- $this->Mail->Body = 'Sending via mail()';
- $this->buildBody();
- $this->Mail->Subject = $this->Mail->Subject . ': mail()';
- $this->Mail->isMail();
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- $msg = $this->Mail->getSentMIMEMessage();
- $this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
- }
- /**
- * Test sending an empty body.
- */
- public function testEmptyBody()
- {
- $this->buildBody();
- $this->Mail->Body = '';
- $this->Mail->Subject = $this->Mail->Subject . ': Empty Body';
- $this->Mail->isMail();
- $this->Mail->AllowEmpty = true;
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- $this->Mail->AllowEmpty = false;
- $this->assertFalse($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * 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.'
- );
- }
- /**
- * Test keepalive (sending multiple messages in a single connection).
- */
- public function testSmtpKeepAlive()
- {
- $this->Mail->Body = 'SMTP keep-alive test.';
- $this->buildBody();
- $subject = $this->Mail->Subject;
- $this->Mail->SMTPKeepAlive = true;
- $this->Mail->Subject = $subject . ': SMTP keep-alive 1';
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- $this->Mail->Subject = $subject . ': SMTP keep-alive 2';
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- $this->Mail->smtpClose();
- }
- /**
- * Tests this denial of service attack:
- * @link http://www.cybsec.com/vuln/PHPMailer-DOS.pdf
- */
- public function testDenialOfServiceAttack()
- {
- $this->Mail->Body = 'This should no longer cause a denial of service.';
- $this->buildBody();
- $this->Mail->Subject = substr(str_repeat('0123456789', 100), 0, 998);
- $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
- }
- /**
- * Tests this denial of service attack:
- * @link https://sourceforge.net/p/phpmailer/bugs/383/
- * According to the ticket, this should get stuck in a loop, though I can't make it happen.
- */
- public function testDenialOfServiceAttack2()
- {
- //Encoding name longer than 68 chars
- $this->Mail->Encoding = '1234567890123456789012345678901234567890123456789012345678901234567890';
- //Call wrapText with a zero length value
- $this->Mail->wrapText(str_repeat('This should no longer cause a denial of service. ', 30), 0);
- }
- /**
- * Test error handling.
- */
- public function testError()
- {
- $this->Mail->Subject .= ': Error handling test - this should be sent ok';
- $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');
- $this->assertEquals('You must provide at least one recipient email address.', $this->Mail->ErrorInfo);
- $this->Mail->addAddress($_REQUEST['mail_to']);
- $this->assertTrue($this->Mail->send(), 'send failed');
- }
- /**
- * Test addressing.
- */
- public function testAddressing()
- {
- $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();
- }
- /**
- * Test RFC822 address splitting.
- */
- public function testAddressSplitting()
- {
- //Test built-in address parser
- $this->assertCount(
- 2,
- PHPMailer::parseAddresses(
- 'Joe User <joe@example.com>, Jill User <jill@example.net>'
- ),
- 'Failed to recognise address list (IMAP parser)'
- );
- $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'
- );
- //Test simple address parser
- $this->assertCount(
- 2,
- PHPMailer::parseAddresses(
- 'Joe User <joe@example.com>, Jill User <jill@example.net>',
- false
- ),
- 'Failed to recognise address list'
- );
- //Test single address
- $this->assertNotEmpty(
- PHPMailer::parseAddresses(
- 'Joe User <joe@example.com>',
- false
- ),
- 'Failed to recognise single address'
- );
- //Test quoted name IMAP
- $this->assertNotEmpty(
- PHPMailer::parseAddresses(
- 'Tim "The Book" O\'Reilly <foo@example.com>'
- ),
- 'Failed to recognise quoted name (IMAP)'
- );
- //Test quoted name
- $this->assertNotEmpty(
- PHPMailer::parseAddresses(
- 'Tim "The Book" O\'Reilly <foo@example.com>',
- false
- ),
- 'Failed to recognise quoted name'
- );
- //Test single address IMAP
- $this->assertNotEmpty(
- PHPMailer::parseAddresses(
- 'Joe User <joe@example.com>'
- ),
- 'Failed to recognise single address (IMAP)'
- );
- //Test unnamed address
- $this->assertNotEmpty(
- PHPMailer::parseAddresses(
- 'joe@example.com',
- false
- ),
- 'Failed to recognise unnamed address'
- );
- //Test unnamed address IMAP
- $this->assertNotEmpty(
- PHPMailer::parseAddresses(
- 'joe@example.com'
- ),
- 'Failed to recognise unnamed address (IMAP)'
- );
- //Test invalid addresses
- $this->assertEmpty(
- PHPMailer::parseAddresses(
- 'Joe User <joe@example.com.>, Jill User <jill.@example.net>'
- ),
- 'Failed to recognise invalid addresses (IMAP)'
- );
- //Test invalid addresses
- $this->assertEmpty(
- PHPMailer::parseAddresses(
- 'Joe User <joe@example.com.>, Jill User <jill.@example.net>',
- false
- ),
- 'Failed to recognise invalid addresses'
- );
- }
- /**
- * Test address escaping.
- */
- public function testAddressEscaping()
- {
- $this->Mail->Subject .= ': Address escaping';
- $this->Mail->clearAddresses();
- $this->Mail->addAddress('foo@example.com', 'Tim "The Book" O\'Reilly');
- $this->Mail->Body = 'Test correct escaping of quotes in addresses.';
- $this->buildBody();
- $this->Mail->preSend();
- $b = $this->Mail->getSentMIMEMessage();
- $this->assertTrue((strpos($b, 'To: "Tim \"The Book\" O\'Reilly" <foo@example.com>') !== false));
- }
- /**
- * 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'
- );
- }
- /**
- * Test BCC-only addressing.
- */
- public function testBCCAddressing()
- {
- $this->Mail->Subject .= ': BCC-only addressing';
- $this->buildBody();
- $this->Mail->clearAllRecipients();
- $this->assertTrue($this->Mail->addBCC('a@example.com'), 'BCC addressing failed');
- $this->assertTrue($this->Mail->send(), 'send failed');
- }
- /**
- * Encoding and charset tests.
- */
- public function testEncodings()
- {
- $this->Mail->CharSet = 'iso-8859-1';
- $this->assertEquals(
- '=A1Hola!_Se=F1or!',
- $this->Mail->encodeQ("\xa1Hola! Se\xf1or!", 'text'),
- 'Q Encoding (text) failed'
- );
- $this->assertEquals(
- '=A1Hola!_Se=F1or!',
- $this->Mail->encodeQ("\xa1Hola! Se\xf1or!", 'comment'),
- 'Q Encoding (comment) failed'
- );
- $this->assertEquals(
- '=A1Hola!_Se=F1or!',
- $this->Mail->encodeQ("\xa1Hola! Se\xf1or!", 'phrase'),
- 'Q Encoding (phrase) failed'
- );
- $this->Mail->CharSet = 'UTF-8';
- $this->assertEquals(
- '=C2=A1Hola!_Se=C3=B1or!',
- $this->Mail->encodeQ("\xc2\xa1Hola! Se\xc3\xb1or!", 'text'),
- 'Q Encoding (text) failed'
- );
- //Strings containing '=' are a special case
- $this->assertEquals(
- 'Nov=C3=A1=3D',
- $this->Mail->encodeQ("Nov\xc3\xa1=", 'text'),
- 'Q Encoding (text) failed 2'
- );
- $this->assertEquals($this->Mail->encodeString('hello', 'binary'), 'hello', 'Binary encoding changed input');
- $this->Mail->ErrorInfo = '';
- $this->Mail->encodeString('hello', 'asdfghjkl');
- $this->assertNotEmpty($this->Mail->ErrorInfo, 'Invalid encoding not detected');
- }
- /**
- * Test base-64 encoding.
- */
- public function testBase64()
- {
- $this->Mail->Subject .= ': Base-64 encoding';
- $this->Mail->Encoding = 'base64';
- $this->buildBody();
- $this->assertTrue($this->Mail->send(), 'Base64 encoding failed');
- }
- /**
- * S/MIME Signing tests (self-signed).
- * @requires extension openssl
- */
- public function testSigning()
- {
- $this->Mail->Subject .= ': S/MIME signing';
- $this->Mail->Body = 'This message is S/MIME signed.';
- $this->buildBody();
- $dn = [
- 'countryName' => 'UK',
- 'stateOrProvinceName' => 'Here',
- 'localityName' => 'There',
- 'organizationName' => 'PHP',
- 'organizationalUnitName' => 'PHPMailer',
- 'commonName' => 'PHPMailer Test',
- 'emailAddress' => 'phpmailer@example.com'
- ];
- $keyconfig = [
- "digest_alg" => "sha256",
- "private_key_bits" => 2048,
- "private_key_type" => OPENSSL_KEYTYPE_RSA,
- ];
- $password = 'password';
- $certfile = 'certfile.pem';
- $keyfile = 'keyfile.pem';
- //Make a new key pair
- $pk = openssl_pkey_new($keyconfig);
- //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);
- $this->Mail->sign(
- $certfile,
- $keyfile,
- $password
- );
- $this->assertTrue($this->Mail->send(), 'S/MIME signing failed');
- $msg = $this->Mail->getSentMIMEMessage();
- $this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
- unlink($certfile);
- unlink($keyfile);
- }
- /**
- * S/MIME Signing tests using a CA chain cert.
- * To test that a generated message is signed correctly, save the message in a file called `signed.eml`
- * and use openssl along with the certs generated by this script:
- * `openssl smime -verify -in signed.eml -signer certfile.pem -CAfile cacertfile.pem`
- * @requires extension openssl
- */
- 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 = [
- 'countryName' => 'UK',
- 'stateOrProvinceName' => 'Here',
- 'localityName' => 'There',
- 'organizationName' => 'PHP',
- 'organizationalUnitName' => 'PHPMailer',
- 'commonName' => 'PHPMailer Test',
- 'emailAddress' => 'phpmailer@example.com'
- ];
- $cacertprops = [
- 'countryName' => 'UK',
- 'stateOrProvinceName' => 'Here',
- 'localityName' => 'There',
- 'organizationName' => 'PHP',
- 'organizationalUnitName' => 'PHPMailer CA',
- 'commonName' => 'PHPMailer Test CA',
- 'emailAddress' => 'phpmailer@example.com'
- ];
- $keyconfig = [
- '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);
- }
- /**
- * DKIM body canonicalization tests.
- * @link https://tools.ietf.org/html/rfc6376#section-3.4.4
- */
- public function testDKIMBodyCanonicalization()
- {
- //Example from https://tools.ietf.org/html/rfc6376#section-3.4.5
- $prebody = " C \r\nD \t E\r\n\r\n\r\n";
- $postbody = " C \r\nD \t E\r\n";
- $this->assertEquals($this->Mail->DKIM_BodyC(''), "\r\n", 'DKIM empty body canonicalization incorrect');
- $this->assertEquals(
- base64_encode(sha1($this->Mail->DKIM_BodyC(''), true)),
- 'uoq1oCgLlTqpdDX/iUbLy7J1Wic=',
- 'DKIM canonicalized empty body hash mismatch'
- );
- $this->assertEquals($this->Mail->DKIM_BodyC($prebody), $postbody, 'DKIM body canonicalization incorrect');
- }
- /**
- * DKIM header canonicalization tests.
- * @link https://tools.ietf.org/html/rfc6376#section-3.4.2
- */
- public function testDKIMHeaderCanonicalization()
- {
- //Example from https://tools.ietf.org/html/rfc6376#section-3.4.5
- $preheaders = "A: X\r\nB : Y\t\r\n\tZ \r\n";
- $postheaders = "a:X\r\nb:Y Z\r\n";
- $this->assertEquals(
- $this->Mail->DKIM_HeaderC($preheaders),
- $postheaders,
- 'DKIM header canonicalization incorrect'
- );
- }
- /**
- * DKIM Signing tests.
- * @requires extension openssl
- */
- public function testDKIM()
- {
- $this->Mail->Subject .= ': DKIM signing';
- $this->Mail->Body = 'This message is DKIM signed.';
- $this->buildBody();
- $privatekeyfile = 'dkim_private.pem';
- //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(
- [
- '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);
- }
- /**
- * Test line break reformatting.
- */
- public function testLineBreaks()
- {
- $unixsrc = "hello\nWorld\nAgain\n";
- $macsrc = "hello\rWorld\rAgain\r";
- $windowssrc = "hello\r\nWorld\r\nAgain\r\n";
- $mixedsrc = "hello\nWorld\rAgain\r\n";
- $target = "hello\r\nWorld\r\nAgain\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');
- }
- /**
- * 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)');
- $this->assertTrue(
- PHPMailer::hasLineLongerThanMax($oklen . $badlen . $oklen),
- 'Long line not detected (middle)'
- );
- $this->assertFalse(PHPMailer::hasLineLongerThanMax($oklen), 'Long line false positive');
- $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');
- }
- /**
- * Test setting and retrieving message ID.
- */
- 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();
- $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');
- }
- /**
- * Miscellaneous calls to improve test coverage and some small tests.
- */
- public function testMiscellaneous()
- {
- $this->assertEquals('application/pdf', PHPMailer::_mime_types('pdf'), 'MIME TYPE lookup failed');
- $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');
- $this->Mail->Sender = '';
- $this->Mail->createHeader();
- $this->assertFalse($this->Mail->set('x', 'y'), 'Invalid property set succeeded');
- $this->assertTrue($this->Mail->set('Timeout', 11), 'Valid property set failed');
- $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');
- //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');
- $this->assertEquals(
- PHPMailer::mb_pathinfo($a, PATHINFO_DIRNAME),
- '/mnt/files',
- 'Dirname path element not matched'
- );
- $this->assertEquals(
- PHPMailer::mb_pathinfo($a, PATHINFO_BASENAME),
- '飛兒樂 團光茫.mp3',
- 'Basename path element not matched'
- );
- $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');
- $this->assertEquals(
- PHPMailer::filenameToType('abc.jpg?xyz=1'),
- 'image/jpeg',
- 'Query string not ignored in filename'
- );
- $this->assertEquals(
- PHPMailer::filenameToType('abc.xyzpdq'),
- 'application/octet-stream',
- 'Default MIME type not applied to unknown extension'
- );
-
- //Line break normalization
- $eol = $this->Mail->LE;
- $b1 = "1\r2\r3\r";
- $b2 = "1\n2\n3\n";
- $b3 = "1\r\n2\r3\n";
- $this->Mail->LE = "\n";
- $t1 = "1{$this->Mail->LE}2{$this->Mail->LE}3{$this->Mail->LE}";
- $this->assertEquals($this->Mail->fixEOL($b1), $t1, 'Failed to normalize line breaks (1)');
- $this->assertEquals($this->Mail->fixEOL($b2), $t1, 'Failed to normalize line breaks (2)');
- $this->assertEquals($this->Mail->fixEOL($b3), $t1, 'Failed to normalize line breaks (3)');
- $this->Mail->LE = "\r\n";
- $t1 = "1{$this->Mail->LE}2{$this->Mail->LE}3{$this->Mail->LE}";
- $this->assertEquals($this->Mail->fixEOL($b1), $t1, 'Failed to normalize line breaks (4)');
- $this->assertEquals($this->Mail->fixEOL($b2), $t1, 'Failed to normalize line breaks (5)');
- $this->assertEquals($this->Mail->fixEOL($b3), $t1, 'Failed to normalize line breaks (6)');
- $this->Mail->LE = $eol;
- }
- public function testBadSMTP()
- {
- $this->Mail->smtpConnect();
- $smtp = $this->Mail->getSMTPInstance();
- $this->assertFalse($smtp->mail("somewhere\nbad"), 'Bad SMTP command containing breaks accepted');
- }
- /**
- * Tests the Custom header getter
- */
- public function testCustomHeaderGetter()
- {
- $this->Mail->addCustomHeader('foo', 'bar');
- $this->assertEquals([['foo', 'bar']], $this->Mail->getCustomHeaders());
- $this->Mail->addCustomHeader('foo', 'baz');
- $this->assertEquals(
- [
- ['foo', 'bar'],
- ['foo', 'baz']
- ],
- $this->Mail->getCustomHeaders()
- );
- $this->Mail->clearCustomHeaders();
- $this->assertEmpty($this->Mail->getCustomHeaders());
- $this->Mail->addCustomHeader('yux');
- $this->assertEquals([['yux']], $this->Mail->getCustomHeaders());
- $this->Mail->addCustomHeader('Content-Type: application/json');
- $this->assertEquals(
- [
- ['yux'],
- ['Content-Type', ' application/json']
- ],
- $this->Mail->getCustomHeaders()
- );
- }
- /**
- * 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(
- [['test' . $domain, '']],
- $this->Mail->getToAddresses(),
- 'Bad "to" recipients'
- );
- $this->assertEquals(
- [['test+cc' . $domain, '']],
- $this->Mail->getCcAddresses(),
- 'Bad "cc" recipients'
- );
- $this->assertEmpty($this->Mail->getBccAddresses(), 'Bad "bcc" recipients');
- $this->assertEquals(
- ['test+replyto' . $domain => ['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'
- );
- }
- /**
- * Use a fake POP3 server to test POP-before-SMTP auth.
- * With a known-good login
- * @group pop3
- */
- public function testPopBeforeSmtpGood()
- {
- //Start a fake POP server
- $pid = shell_exec(
- '/usr/bin/nohup ' .
- $this->INCLUDE_DIR .
- '/test/runfakepopserver.sh 1100 >/dev/null 2>/dev/null & printf "%u" $!'
- );
- $this->pids[] = $pid;
- sleep(1);
- //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, don't care if it fails
- @shell_exec('kill -TERM ' . escapeshellarg($pid));
- sleep(2);
- }
- /**
- * Use a fake POP3 server to test POP-before-SMTP auth
- * with a known-bad login.
- * @group pop3
- */
- 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(
- '/usr/bin/nohup ' .
- $this->INCLUDE_DIR .
- '/test/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'
- );
- //Kill the fake server, don't care if it fails
- @shell_exec('kill -TERM ' . escapeshellarg($pid));
- sleep(2);
- }
- /**
- * Test SMTP host connections.
- * This test can take a long time, so run it last
- * @group slow
- */
- 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(['ssl' => ['verify_depth' => 10]]),
- 'SMTP connect with options failed'
- );
- }
- }
- /**
- * This is a sample form for setting appropriate test values through a browser
- * These values can also be set using a file called testbootstrap.php (not in repo) in the same folder as this script
- * which is probably more useful if you run these tests a lot
- * <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>
- */
|