From 6fa6777e6b4a6fe2c5bcf8718d82b0e2a15efc01 Mon Sep 17 00:00:00 2001 From: Mike Swift Date: Tue, 5 Jun 2012 15:07:42 -0600 Subject: [PATCH 1/2] Add optional fromname to Web and SMTP API calls. --- SendGrid/Mail.php | 36 ++++++++++++++++++++++++++++++++---- SendGrid/Smtp.php | 4 ++-- SendGrid/Web.php | 6 +++++- Test/SendGrid/MailTest.php | 16 +++++++++++++++- Test/SendGrid/SmtpTest.php | 3 ++- Test/SendGrid/WebTest.php | 20 +++++++++++++++++++- 6 files changed, 75 insertions(+), 10 deletions(-) diff --git a/SendGrid/Mail.php b/SendGrid/Mail.php index 746df5b60..e54246c5c 100644 --- a/SendGrid/Mail.php +++ b/SendGrid/Mail.php @@ -7,6 +7,7 @@ class Mail private $to_list, $from, + $from_name, $cc_list, $bcc_list, $subject, @@ -19,7 +20,7 @@ class Mail public function __construct() { - + $this->from_name = false; } /** @@ -119,11 +120,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,6 +143,28 @@ 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; + } /** * getCc @@ -667,4 +695,4 @@ protected function _preferNotToUseHeaders() return false; } -} \ No newline at end of file +} diff --git a/SendGrid/Smtp.php b/SendGrid/Smtp.php index 7acd4609a..32bdea953 100644 --- a/SendGrid/Smtp.php +++ b/SendGrid/Smtp.php @@ -70,7 +70,7 @@ 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()); @@ -138,4 +138,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..d02851c55 100644 --- a/SendGrid/Web.php +++ b/SendGrid/Web.php @@ -42,6 +42,10 @@ protected function _prepMessageData(Mail $mail) 'x-smtpapi' => $mail->getHeadersJson() ); + if(($fromname = $mail->getFromName())) { + $params['fromname'] = $fromname; + } + // determine if we should send our recipients through our headers, // and set the properties accordingly if($mail->useHeaders()) @@ -129,4 +133,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..159189d97 100644 --- a/Test/SendGrid/MailTest.php +++ b/Test/SendGrid/MailTest.php @@ -58,8 +58,22 @@ 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 testCcAccessors() @@ -520,4 +534,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..05da4dbd6 100644 --- a/Test/SendGrid/WebTest.php +++ b/Test/SendGrid/WebTest.php @@ -55,6 +55,24 @@ 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); + + // Set optional params + $message->setFromName('John Doe'); + + $actual_with_optional_params = $mock->testPrepMessageData($message); + + $this->assertArrayHasKey('fromname', $actual_with_optional_params); + } + public function testSendResponse() { $sendgrid = new SendGrid("foo", "bar"); @@ -71,4 +89,4 @@ public function testSendResponse() $this->assertEquals("{\"message\": \"error\", \"errors\": [\"Bad username / password\"]}", $response); } -} \ No newline at end of file +} From e725d5afb30c6fb43556e71f1e3d151f5caa811a Mon Sep 17 00:00:00 2001 From: Mike Swift Date: Tue, 5 Jun 2012 15:47:11 -0600 Subject: [PATCH 2/2] Add optional Reply-to to Web and SMTP API calls. --- SendGrid/Mail.php | 31 +++++++++++++++++++++++++++---- SendGrid/Smtp.php | 4 ++++ SendGrid/Web.php | 6 +++++- Test/SendGrid/MailTest.php | 12 ++++++++++++ Test/SendGrid/WebTest.php | 6 ++++++ 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/SendGrid/Mail.php b/SendGrid/Mail.php index e54246c5c..4a12c71b8 100644 --- a/SendGrid/Mail.php +++ b/SendGrid/Mail.php @@ -4,10 +4,11 @@ class Mail { - - private $to_list, + + private $to_list, $from, $from_name, + $reply_to, $cc_list, $bcc_list, $subject, @@ -21,8 +22,9 @@ 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 @@ -165,7 +167,28 @@ 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 diff --git a/SendGrid/Smtp.php b/SendGrid/Smtp.php index 32bdea953..1e5f910b8 100644 --- a/SendGrid/Smtp.php +++ b/SendGrid/Smtp.php @@ -76,6 +76,10 @@ protected function _mapToSwift(Mail $mail) $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()) { diff --git a/SendGrid/Web.php b/SendGrid/Web.php index d02851c55..6d69f4971 100644 --- a/SendGrid/Web.php +++ b/SendGrid/Web.php @@ -46,6 +46,10 @@ protected function _prepMessageData(Mail $mail) $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()) @@ -62,7 +66,7 @@ protected function _prepMessageData(Mail $mail) $params['to'] = $mail->getTos(); } - + if($mail->getAttachments()) { foreach($mail->getAttachments() as $attachment) diff --git a/Test/SendGrid/MailTest.php b/Test/SendGrid/MailTest.php index 159189d97..37b4b44ec 100644 --- a/Test/SendGrid/MailTest.php +++ b/Test/SendGrid/MailTest.php @@ -76,6 +76,18 @@ public function testFromNameAccessors() $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() { $message = new SendGrid\Mail(); diff --git a/Test/SendGrid/WebTest.php b/Test/SendGrid/WebTest.php index 05da4dbd6..e6f1d1c14 100644 --- a/Test/SendGrid/WebTest.php +++ b/Test/SendGrid/WebTest.php @@ -64,13 +64,19 @@ public function testOptionalParamters() $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()