How to use addTo method of mailer class

Best Atoum code snippet using mailer.addTo

MailHelperTest.php

Source:MailHelperTest.php Github

copy

Full Screen

...75 $swiftMailer = new \Swift_Mailer(new BatchTransport());76 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);77 // Enable queue mode78 $mailer->enableQueue();79 $mailer->addTo('somebody@somewhere.com');80 $mailer->addTo('somebodyelse@somewhere.com');81 $mailer->addTo('somebodyelse2@somewhere.com');82 $mailer->addTo('somebodyelse3@somewhere.com');83 $mailer->addTo('somebodyelse4@somewhere.com');84 }85 public function testQueueModeDisabledDoesNotThrowsExceptionWhenBatchLimitHit()86 {87 $mockFactory = $this->getMockBuilder(MauticFactory::class)88 ->disableOriginalConstructor()89 ->getMock();90 $mockFactory->method('getParameter')91 ->will(92 $this->returnValueMap(93 [94 ['mailer_return_path', false, null],95 ['mailer_spool_type', false, 'memory'],96 ]97 )98 );99 $swiftMailer = new \Swift_Mailer(new BatchTransport());100 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);101 // Enable queue mode102 try {103 $mailer->addTo('somebody@somewhere.com');104 $mailer->addTo('somebodyelse@somewhere.com');105 } catch (BatchQueueMaxException $exception) {106 $this->fail('BatchQueueMaxException thrown');107 }108 // Otherwise success109 $this->assertTrue(true);110 }111 public function testQueuedEmailFromOverride()112 {113 $mockFactory = $this->getMockFactory(false);114 $transport = new BatchTransport();115 $swiftMailer = new \Swift_Mailer($transport);116 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);117 $mailer->enableQueue();118 $email = new Email();119 $email->setFromAddress('override@nowhere.com');120 $email->setFromName('Test');121 $mailer->setEmail($email);122 foreach ($this->contacts as $contact) {123 $mailer->addTo($contact['email']);124 $mailer->setLead($contact);125 $mailer->queue();126 }127 $mailer->flushQueue();128 $from = $mailer->message->getFrom();129 $this->assertTrue(array_key_exists('override@nowhere.com', $from));130 $this->assertTrue(count($from) === 1);131 $mailer->reset();132 foreach ($this->contacts as $contact) {133 $mailer->addTo($contact['email']);134 $mailer->setLead($contact);135 $mailer->queue();136 }137 $mailer->flushQueue();138 $from = $mailer->message->getFrom();139 $this->assertTrue(array_key_exists('nobody@nowhere.com', $from));140 $this->assertTrue(count($from) === 1);141 }142 public function testBatchMode()143 {144 $mockFactory = $this->getMockFactory(false);145 $transport = new BatchTransport(true);146 $swiftMailer = new \Swift_Mailer($transport);147 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);148 $mailer->enableQueue();149 $email = new Email();150 $email->setSubject('Hello');151 $mailer->setEmail($email);152 $mailer->addTo($this->contacts[0]['email']);153 $mailer->setLead($this->contacts[0]);154 $mailer->queue();155 $mailer->flushQueue();156 $errors = $mailer->getErrors();157 $this->assertEmpty($errors['failures'], var_export($errors, true));158 $mailer->reset(false);159 $mailer->setEmail($email);160 $mailer->addTo($this->contacts[1]['email']);161 $mailer->setLead($this->contacts[1]);162 $mailer->queue();163 $mailer->flushQueue();164 $errors = $mailer->getErrors();165 $this->assertEmpty($errors['failures'], var_export($errors, true));166 }167 public function testQueuedOwnerAsMailer()168 {169 $mockFactory = $this->getMockFactory();170 $transport = new BatchTransport();171 $swiftMailer = new \Swift_Mailer($transport);172 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);173 $mailer->enableQueue();174 $mailer->setSubject('Hello');175 foreach ($this->contacts as $contact) {176 $mailer->addTo($contact['email']);177 $mailer->setLead($contact);178 $mailer->queue();179 }180 $mailer->flushQueue([]);181 $this->assertEmpty($mailer->getErrors()['failures']);182 $fromAddresses = $transport->getFromAddresses();183 $metadatas = $transport->getMetadatas();184 $this->assertEquals(3, count($fromAddresses));185 $this->assertEquals(3, count($metadatas));186 $this->assertEquals(['owner1@owner.com', 'nobody@nowhere.com', 'owner2@owner.com'], $fromAddresses);187 foreach ($metadatas as $key => $metadata) {188 $this->assertTrue(isset($metadata[$this->contacts[$key]['email']]));189 if (0 === $key) {190 // Should have two contacts191 $this->assertEquals(2, count($metadata));192 $this->assertTrue(isset($metadata['contact4@somewhere.com']));193 } else {194 $this->assertEquals(1, count($metadata));195 }196 // Check that signatures are valid197 if (1 === $key) {198 // There should not be a signature token because owner was not set and token events have not been dispatched199 $this->assertFalse(isset($metadata['tokens']['{signature}']));200 } else {201 $this->assertEquals($metadata[$this->contacts[$key]['email']]['tokens']['{signature}'], 'owner '.$this->contacts[$key]['owner_id']);202 if (0 === $key) {203 // Ensure the last contact has the correct signature204 $this->assertEquals($metadata['contact4@somewhere.com']['tokens']['{signature}'], 'owner '.$this->contacts[$key]['owner_id']);205 }206 }207 }208 // Validate that the message object only has the contacts for the last "from" group to ensure we aren't sending duplicates209 $this->assertEquals(['contact3@somewhere.com' => null], $mailer->message->getTo());210 }211 public function testMailAsOwnerWithEncodedCharactersInName()212 {213 $mockFactory = $this->getMockFactory();214 $transport = new BatchTransport();215 $swiftMailer = new \Swift_Mailer($transport);216 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body's Business']);217 $mailer->enableQueue();218 $mailer->setSubject('Hello');219 $contacts = $this->contacts;220 $contacts[3]['owner_id'] = 3;221 foreach ($contacts as $contact) {222 $mailer->addTo($contact['email']);223 $mailer->setLead($contact);224 $mailer->queue();225 }226 $mailer->flushQueue([]);227 $fromAddresses = $transport->getFromAddresses();228 $fromNames = $transport->getFromNames();229 $this->assertEquals(4, count($fromAddresses));230 $this->assertEquals(4, count($fromNames));231 $this->assertEquals(['owner1@owner.com', 'nobody@nowhere.com', 'owner2@owner.com', 'owner3@owner.com'], $fromAddresses);232 $this->assertEquals([null, "No Body's Business", null, "John S'mith"], $fromNames);233 }234 public function testBatchIsEnabledWithBcTokenInterface()235 {236 $mockFactory = $this->getMockFactory();237 $transport = new BcInterfaceTokenTransport();238 $swiftMailer = new \Swift_Mailer($transport);239 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);240 $mailer->enableQueue();241 $mailer->setSubject('Hello');242 foreach ($this->contacts as $contact) {243 $mailer->addTo($contact['email']);244 $mailer->setLead($contact);245 $mailer->queue();246 }247 $mailer->flushQueue([]);248 $this->assertEmpty($mailer->getErrors()['failures']);249 $fromAddresses = $transport->getFromAddresses();250 $metadatas = $transport->getMetadatas();251 $this->assertEquals(3, count($fromAddresses));252 $this->assertEquals(3, count($metadatas));253 }254 public function testGlobalFromThatAllFromAddressesAreTheSame()255 {256 $mockFactory = $this->getMockFactory();257 $transport = new BatchTransport();258 $swiftMailer = new \Swift_Mailer($transport);259 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);260 $mailer->enableQueue();261 $mailer->setSubject('Hello');262 $mailer->setFrom('override@owner.com');263 foreach ($this->contacts as $contact) {264 $mailer->addTo($contact['email']);265 $mailer->setLead($contact);266 $mailer->queue();267 }268 $mailer->flushQueue();269 $this->assertEmpty($mailer->getErrors()['failures']);270 $fromAddresses = $transport->getFromAddresses();271 $this->assertEquals(['override@owner.com'], array_unique($fromAddresses));272 }273 public function testStandardOwnerAsMailer()274 {275 $mockFactory = $this->getMockFactory();276 $transport = new SmtpTransport();277 $swiftMailer = new \Swift_Mailer($transport);278 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);279 $mailer->setBody('{signature}');280 foreach ($this->contacts as $key => $contact) {281 $mailer->addTo($contact['email']);282 $mailer->setLead($contact);283 $mailer->send();284 $body = $mailer->message->getBody();285 $from = key($mailer->message->getFrom());286 if ($contact['owner_id']) {287 $this->assertEquals('owner'.$contact['owner_id'].'@owner.com', $from);288 $this->assertEquals('owner '.$contact['owner_id'], $body);289 } else {290 $this->assertEquals('nobody@nowhere.com', $from);291 $this->assertEquals('{signature}', $body);292 }293 }294 }295 public function testValidateValidEmails()296 {297 $helper = $this->mockEmptyMailHelper();298 $addresses = [299 'john@doe.com',300 'john@doe.email',301 'john.doe@email.com',302 'john+doe@email.com',303 'john@doe.whatevertldtheycomewithinthefuture',304 ];305 foreach ($addresses as $address) {306 // will throw Swift_RfcComplianceException if it will find the address invalid307 $helper::validateEmail($address);308 }309 }310 public function testValidateEmailWithoutTld()311 {312 $helper = $this->mockEmptyMailHelper();313 $this->expectException(\Swift_RfcComplianceException::class);314 $helper::validateEmail('john@doe');315 }316 public function testValidateEmailWithSpaceInIt()317 {318 $helper = $this->mockEmptyMailHelper();319 $this->expectException(\Swift_RfcComplianceException::class);320 $helper::validateEmail('jo hn@doe.email');321 }322 public function testValidateEmailWithCaretInIt()323 {324 $helper = $this->mockEmptyMailHelper();325 $this->expectException(\Swift_RfcComplianceException::class);326 $helper::validateEmail('jo^hn@doe.email');327 }328 public function testValidateEmailWithApostropheInIt()329 {330 $helper = $this->mockEmptyMailHelper();331 $this->expectException(\Swift_RfcComplianceException::class);332 $helper::validateEmail('jo\'hn@doe.email');333 }334 public function testValidateEmailWithSemicolonInIt()335 {336 $helper = $this->mockEmptyMailHelper();337 $this->expectException(\Swift_RfcComplianceException::class);338 $helper::validateEmail('jo;hn@doe.email');339 }340 public function testValidateEmailWithAmpersandInIt()341 {342 $helper = $this->mockEmptyMailHelper();343 $this->expectException(\Swift_RfcComplianceException::class);344 $helper::validateEmail('jo&hn@doe.email');345 }346 public function testValidateEmailWithStarInIt()347 {348 $helper = $this->mockEmptyMailHelper();349 $this->expectException(\Swift_RfcComplianceException::class);350 $helper::validateEmail('jo*hn@doe.email');351 }352 public function testValidateEmailWithPercentInIt()353 {354 $helper = $this->mockEmptyMailHelper();355 $this->expectException(\Swift_RfcComplianceException::class);356 $helper::validateEmail('jo%hn@doe.email');357 }358 public function testQueueModeIsReset()359 {360 $contacts = $this->contacts;361 $contacts[] = [362 'id' => 5,363 'email' => 'contact5@somewhere.com',364 'firstname' => 'Contact',365 'lastname' => '5',366 'owner_id' => 1,367 ];368 $helper = $this->mockEmptyMailHelper(false);369 $helper->enableQueue();370 $helper->addTo($contacts[0]['email']);371 $helper->addTo($contacts[1]['email']);372 $helper->addTo($contacts[2]['email']);373 $helper->addTo($contacts[3]['email']);374 $exceptionCaught = true;375 try {376 $helper->addTo($contacts[4]['email']);377 $exceptionCaught = false;378 } catch (BatchQueueMaxException $exception) {379 }380 if (!$exceptionCaught) {381 $this->fail('BatchQueueMaxException should have been thrown');382 }383 // Reset which should now reset qeue mode so that each to address is accepted384 $helper->reset();385 try {386 foreach ($contacts as $contact) {387 $helper->addTo($contact['email']);388 }389 } catch (BatchQueueMaxException $exception) {390 $this->fail('Queue mode was not reset');391 }392 $to = $helper->message->getTo();393 $this->assertEquals(394 [395 'contact1@somewhere.com' => null,396 'contact2@somewhere.com' => null,397 'contact3@somewhere.com' => null,398 'contact4@somewhere.com' => null,399 'contact5@somewhere.com' => null,400 ],401 $to402 );403 }404 public function testGlobalHeadersAreSet()405 {406 $parameterMap = [407 ['mailer_custom_headers', [], ['X-Mautic-Test' => 'test', 'X-Mautic-Test2' => 'test']],408 ];409 $mockFactory = $this->getMockFactory(true, $parameterMap);410 $transport = new SmtpTransport();411 $swiftMailer = new \Swift_Mailer($transport);412 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);413 $mailer->setBody('{signature}');414 $mailer->addTo($this->contacts[0]['email']);415 $mailer->send();416 $customHeadersFounds = [];417 /** @var \Swift_Mime_Headers_ParameterizedHeader[] $headers */418 $headers = $mailer->message->getHeaders()->getAll();419 foreach ($headers as $header) {420 if (strpos($header->getFieldName(), 'X-Mautic-Test') !== false) {421 $customHeadersFounds[] = $header->getFieldName();422 $this->assertEquals('test', $header->getValue());423 }424 }425 $this->assertCount(2, $customHeadersFounds);426 }427 public function testGlobalHeadersAreIgnoredIfEmailEntityIsSet()428 {429 $parameterMap = [430 ['mailer_custom_headers', [], ['X-Mautic-Test' => 'test', 'X-Mautic-Test2' => 'test']],431 ];432 $mockFactory = $this->getMockFactory(true, $parameterMap);433 $transport = new SmtpTransport();434 $swiftMailer = new \Swift_Mailer($transport);435 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);436 $mailer->addTo($this->contacts[0]['email']);437 $email = new Email();438 $email->setSubject('Test');439 $email->setCustomHtml('{signature}');440 $mailer->setEmail($email);441 $mailer->send();442 /** @var \Swift_Mime_Headers_ParameterizedHeader[] $headers */443 $headers = $mailer->message->getHeaders()->getAll();444 foreach ($headers as $header) {445 if (strpos($header->getFieldName(), 'X-Mautic-Test') !== false) {446 $this->fail('System headers were not supposed to be set');447 }448 }449 }450 public function testEmailHeadersAreSet()451 {452 $parameterMap = [453 ['mailer_custom_headers', [], ['X-Mautic-Test' => 'test', 'X-Mautic-Test2' => 'test']],454 ];455 $mockFactory = $this->getMockFactory(true, $parameterMap);456 $transport = new SmtpTransport();457 $swiftMailer = new \Swift_Mailer($transport);458 $mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);459 $mailer->addTo($this->contacts[0]['email']);460 $email = new Email();461 $email->setSubject('Test');462 $email->setCustomHtml('{signature}');463 $email->setHeaders(['X-Mautic-Test3' => 'test2', 'X-Mautic-Test4' => 'test2']);464 $mailer->setEmail($email);465 $mailer->send();466 $customHeadersFounds = [];467 /** @var \Swift_Mime_Headers_ParameterizedHeader[] $headers */468 $headers = $mailer->message->getHeaders()->getAll();469 foreach ($headers as $header) {470 if (strpos($header->getFieldName(), 'X-Mautic-Test') !== false) {471 $customHeadersFounds[] = $header->getFieldName();472 $this->assertEquals('test2', $header->getValue());473 }...

Full Screen

Full Screen

addTo

Using AI Code Generation

copy

Full Screen

1require_once 'class.phpmailer.php';2require_once 'class.smtp.php';3$mail = new PHPMailer();4$mail->IsSMTP();5$mail->SMTPSecure = 'ssl';6$mail->Host = "smtp.gmail.com";7$mail->Port = 465;

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Atoum automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Trigger addTo code on LambdaTest Cloud Grid

Execute automation tests with addTo on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful