diff --git a/SendGrid/Mail.php b/SendGrid/Mail.php index 746df5b60..4a12c71b8 100644 --- a/SendGrid/Mail.php +++ b/SendGrid/Mail.php @@ -4,9 +4,11 @@ class Mail { - - private $to_list, + + private $to_list, $from, + $from_name, + $reply_to, $cc_list, $bcc_list, $subject, @@ -19,9 +21,10 @@ class Mail public function __construct() { - + $this->from_name = false; + $this->reply_to = false; } - + /** * _removeFromList * Given a list of key/value pairs, removes the associated keys @@ -119,11 +122,16 @@ public function removeTo($search_term) /** * getFrom * get the from email address + * @param Boolean $as_array - return the from as an assocative array * @return the from email address */ - public function getFrom() + public function getFrom($as_array = false) { - return $this->from; + if($as_array && ($name = $this->getFromName())) { + return array("$this->from" => $name); + } else { + return $this->from; + } } /** @@ -137,7 +145,50 @@ public function setFrom($email) $this->from = $email; return $this; } - + + /** + * getFromName + * get the from name + * @return the from name + */ + public function getFromName() + { + return $this->from_name; + } + + /** + * setFromName + * set the name appended to the from email + * @param String $name - a name to append + * @return the SendGrid\Mail object. + */ + public function setFromName($name) + { + $this->from_name = $name; + return $this; + } + + /** + * getReplyTo + * get the reply-to address + * @return the reply to address + */ + public function getReplyTo() + { + return $this->reply_to; + } + + /** + * setReplyTo + * set the reply-to address + * @param String $email - the email to reply to + * @return the SendGrid\Mail object. + */ + public function setReplyTo($email) + { + $this->reply_to = $email; + return $this; + } /** * getCc * get the Carbon Copy list of recipients @@ -667,4 +718,4 @@ protected function _preferNotToUseHeaders() return false; } -} \ No newline at end of file +} diff --git a/SendGrid/Smtp.php b/SendGrid/Smtp.php index 7acd4609a..1e5f910b8 100644 --- a/SendGrid/Smtp.php +++ b/SendGrid/Smtp.php @@ -70,12 +70,16 @@ protected function _mapToSwift(Mail $mail) * ignored anyway. */ $message->setTo($mail->getFrom()); - $message->setFrom($mail->getFrom()); + $message->setFrom($mail->getFrom(true)); $message->setBody($mail->getHtml(), 'text/html'); $message->addPart($mail->getText(), 'text/plain'); $message->setCc($mail->getCcs()); $message->setBcc($mail->getBccs()); + if(($replyto = $mail->getReplyTo())) { + $message->setReplyTo($replyto); + } + // determine whether or not we can use SMTP recipients (non header based) if($mail->useHeaders()) { @@ -138,4 +142,4 @@ public function send(Mail $mail) return true; } -} \ No newline at end of file +} diff --git a/SendGrid/Web.php b/SendGrid/Web.php index aa99fc957..6d69f4971 100644 --- a/SendGrid/Web.php +++ b/SendGrid/Web.php @@ -42,6 +42,14 @@ protected function _prepMessageData(Mail $mail) 'x-smtpapi' => $mail->getHeadersJson() ); + if(($fromname = $mail->getFromName())) { + $params['fromname'] = $fromname; + } + + if(($replyto = $mail->getReplyTo())) { + $params['replyto'] = $replyto; + } + // determine if we should send our recipients through our headers, // and set the properties accordingly if($mail->useHeaders()) @@ -58,7 +66,7 @@ protected function _prepMessageData(Mail $mail) $params['to'] = $mail->getTos(); } - + if($mail->getAttachments()) { foreach($mail->getAttachments() as $attachment) @@ -129,4 +137,4 @@ public function send(Mail $mail) return $response; } -} \ No newline at end of file +} diff --git a/Test/SendGrid/MailTest.php b/Test/SendGrid/MailTest.php index 9e86648a7..37b4b44ec 100644 --- a/Test/SendGrid/MailTest.php +++ b/Test/SendGrid/MailTest.php @@ -58,8 +58,34 @@ public function testFromAccessors() $message = new SendGrid\Mail(); $message->setFrom("foo@bar.com"); + $message->setFromName("John Doe"); $this->assertEquals("foo@bar.com", $message->getFrom()); + $this->assertEquals(array("foo@bar.com" => "John Doe"), $message->getFrom(true)); + } + + public function testFromNameAccessors() + { + $message = new SendGrid\Mail(); + + // Defaults to false + $this->assertFalse($message->getFromName()); + + $message->setFromName("Swift"); + + $this->assertEquals("Swift", $message->getFromName()); + } + + public function testReplyToAccessors() + { + $message = new SendGrid\Mail(); + + // Defaults to false + $this->assertFalse($message->getReplyTo()); + + $message->setReplyTo("swift@sendgrid.com"); + + $this->assertEquals("swift@sendgrid.com", $message->getReplyTo()); } public function testCcAccessors() @@ -520,4 +546,4 @@ public function testUseHeaders() $this->assertTrue($mail->useHeaders()); } -} \ No newline at end of file +} diff --git a/Test/SendGrid/SmtpTest.php b/Test/SendGrid/SmtpTest.php index 69611119f..7ad2e65cf 100644 --- a/Test/SendGrid/SmtpTest.php +++ b/Test/SendGrid/SmtpTest.php @@ -13,6 +13,7 @@ public function testConstruction() $message = new SendGrid\Mail(); $message-> setFrom('bar@foo.com')-> + setFromName('John Doe')-> setSubject('foobar subject')-> setText('foobar text')-> addTo('foo@bar.com')-> @@ -44,4 +45,4 @@ public function testPorts() $mock->setPort('52'); $this->assertEquals('52', $mock->getPort()); } -} \ No newline at end of file +} diff --git a/Test/SendGrid/WebTest.php b/Test/SendGrid/WebTest.php index 2b85152ad..e6f1d1c14 100644 --- a/Test/SendGrid/WebTest.php +++ b/Test/SendGrid/WebTest.php @@ -55,6 +55,30 @@ public function testMockFunctions() $this->assertEquals("¶m[]=foo¶m[]=bar¶m[]=car¶m[]=doo", $url_part); } + public function testOptionalParamters() + { + $message = new SendGrid\Mail(); + $mock = new WebMock("foo", "bar"); + + // Default Values + $actual_without_optional_params = $mock->testPrepMessageData($message); + + $this->assertArrayNotHasKey('fromname', $actual_without_optional_params); + $this->assertArrayNotHasKey('replyto', $actual_without_optional_params); + + // Set optional params + $message->setFromName('John Doe'); + $message->setReplyTo('swift@sendgrid.com'); + + $actual_with_optional_params = $mock->testPrepMessageData($message); + + $this->assertArrayHasKey('fromname', $actual_with_optional_params); + $this->assertEquals('John Doe', $actual_with_optional_params['fromname']); + + $this->assertArrayHasKey('replyto', $actual_with_optional_params); + $this->assertEquals('swift@sendgrid.com', $actual_with_optional_params['replyto']); + } + public function testSendResponse() { $sendgrid = new SendGrid("foo", "bar"); @@ -71,4 +95,4 @@ public function testSendResponse() $this->assertEquals("{\"message\": \"error\", \"errors\": [\"Bad username / password\"]}", $response); } -} \ No newline at end of file +}