Permalink
Browse files

Merge pull request #5 from theycallmeswift/missing-api-params

Add optional from name and reply-to fields to web and smtp API calls

Closes #4
Closes #5

WIP #3
  • Loading branch information...
2 parents 60abf1b + e725d5a commit c0da9247e30c430c2ab4251f7659e6b586b67f0e @theycallmeswift theycallmeswift committed Jun 18, 2012
Showing with 129 additions and 15 deletions.
  1. +59 −8 SendGrid/Mail.php
  2. +6 −2 SendGrid/Smtp.php
  3. +10 −2 SendGrid/Web.php
  4. +27 −1 Test/SendGrid/MailTest.php
  5. +2 −1 Test/SendGrid/SmtpTest.php
  6. +25 −1 Test/SendGrid/WebTest.php
View
@@ -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;
}
-}
+}
View
@@ -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;
}
-}
+}
View
@@ -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;
}
-}
+}
@@ -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());
}
-}
+}
@@ -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());
}
-}
+}
@@ -55,6 +55,30 @@ public function testMockFunctions()
$this->assertEquals("&param[]=foo&param[]=bar&param[]=car&param[]=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);
}
-}
+}

0 comments on commit c0da924

Please sign in to comment.