public function testSubstitutionAccessors() { $email = new \SendGrid\Email(); $substitutions = ['sub_1' => ['val_1.1', 'val_1.2', 'val_1.3'], 'sub_2' => ['val_2.1', 'val_2.2'], 'sub_3' => ['val_3.1', 'val_3.2', 'val_3.3', 'val_3.4'], 'sub_4' => ['val_4.1', 'val_4.2', 'val_4.3']]; $email->setSubstitutions($substitutions); $this->assertEquals('{"sub":{"sub_1":["val_1.1","val_1.2","val_1.3"],"sub_2":["val_2.1","val_2.2"],"sub_3":["val_3.1","val_3.2","val_3.3","val_3.4"],"sub_4":["val_4.1","val_4.2","val_4.3"]}}', $email->getSmtpapi()->jsonString()); }
/** * @param string|null $subject * @param string|null $template * @return bool * @throws \SendGrid\Exception */ public function send($subject = null, $template = null) { if (null !== $subject) { $this->setSubject($subject); } if (null !== $template) { $this->setTemplate($template); } $email = new \SendGrid\Email(); $email->setFrom($this->fromEmail); $email->setFromName($this->fromName); $email->setSubject($this->subject); $email->setHtml(' '); $email->setTemplateId($this->template); foreach ($this->recipients as $recipient) { /** * SendGrid API library requires to use 'addSmtpapiTo' method in case of many recipients. * On the other side, it does not allow to mix Smtpapi methods with non-Smtpapi methods, therefore, * original methods 'addBcc' and 'addCc' cannot be used here. */ $email->addSmtpapiTo($recipient->getEmail(), $recipient->getName()); } $email->setSubstitutions($this->getLocalVariables()); $email->setAttachments($this->attachments); $this->setSections($email); if (null !== $this->replyTo) { $email->setReplyTo($this->replyTo); } if (false === empty($this->headers)) { $email->setHeaders($this->headers); } try { $this->sendgrid->send($email); $this->cleanAttachments(); return true; } catch (\SendGrid\Exception $e) { $this->cleanAttachments(); throw $e; } }
public function testSubstitutionAccessors() { $email = new SendGrid\Email(); $substitutions = array("sub_1" => array("val_1.1", "val_1.2", "val_1.3"), "sub_2" => array("val_2.1", "val_2.2"), "sub_3" => array("val_3.1", "val_3.2", "val_3.3", "val_3.4"), "sub_4" => array("val_4.1", "val_4.2", "val_4.3")); $email->setSubstitutions($substitutions); $header = $email->getSmtpapiHeaders(); $this->assertEquals($substitutions, $header['sub']); $this->assertEquals("{\"sub\":{\"sub_1\":[\"val_1.1\",\"val_1.2\",\"val_1.3\"],\"sub_2\":[\"val_2.1\",\"val_2.2\"],\"sub_3\":[\"val_3.1\",\"val_3.2\",\"val_3.3\",\"val_3.4\"],\"sub_4\":[\"val_4.1\",\"val_4.2\",\"val_4.3\"]}}", $email->getSmtpapiHeadersJson()); // ensure that addSubstitution appends to the list of substitutions $sub_vals = array("val_5.1", "val_5.2", "val_5.3", "val_5.4"); $email->addSubstitution("sub_5", $sub_vals); $substitutions["sub_5"] = $sub_vals; $header = $email->getSmtpapiHeaders(); $this->assertEquals(5, count($header['sub'])); $this->assertEquals($substitutions, $header['sub']); }
public function it_sets_mailer_data(\SendGrid $sendGrid) { $this->setData(['from_name' => 'Master Jedi Yoda', 'from_email' => '*****@*****.**', 'subject' => 'Example subject', 'template' => 'example template', 'recipients' => ['*****@*****.**' => new Recipient('Jane Doe', '*****@*****.**')], 'global_vars' => ['global_one' => 'Example', 'global_two' => 'Another example'], 'local_vars' => ['*****@*****.**' => [new Variable('local', 'Yet another example')]], 'headers' => [], 'reply_to' => null, 'attachments' => []]); $email = new \SendGrid\Email(); $email->setFrom('*****@*****.**'); $email->setFromName('Master Jedi Yoda'); $email->addSmtpapiTo('*****@*****.**', 'Jane Doe'); $email->setSubject('Example subject'); $email->setHtml(' '); $email->setTemplateId('example template'); $email->setSections(['global_one_SECTION' => 'Example', 'global_two_SECTION' => 'Another example']); $email->setSubstitutions(['local' => ['Yet another example'], 'global_one' => ['global_one_SECTION'], 'global_two' => ['global_two_SECTION']]); $sendGrid->send($email)->shouldBeCalled(); $this->send('Example subject', 'example template')->shouldReturn(true); }
public function testSubstitutionAccessors() { $email = new SendGrid\Email(); $substitutions = array("sub_1" => array("val_1.1", "val_1.2", "val_1.3"), "sub_2" => array("val_2.1", "val_2.2"), "sub_3" => array("val_3.1", "val_3.2", "val_3.3", "val_3.4"), "sub_4" => array("val_4.1", "val_4.2", "val_4.3")); $email->setSubstitutions($substitutions); $this->assertEquals("{\"sub\":{\"sub_1\":[\"val_1.1\",\"val_1.2\",\"val_1.3\"],\"sub_2\":[\"val_2.1\",\"val_2.2\"],\"sub_3\":[\"val_3.1\",\"val_3.2\",\"val_3.3\",\"val_3.4\"],\"sub_4\":[\"val_4.1\",\"val_4.2\",\"val_4.3\"]}}", $email->smtpapi->jsonString()); }