Permalink
Browse files

Add optional fromname to Web and SMTP API calls.

  • Loading branch information...
1 parent 60abf1b commit 6fa6777e6b4a6fe2c5bcf8718d82b0e2a15efc01 @theycallmeswift theycallmeswift committed Jun 5, 2012
Showing with 75 additions and 10 deletions.
  1. +32 −4 SendGrid/Mail.php
  2. +2 −2 SendGrid/Smtp.php
  3. +5 −1 SendGrid/Web.php
  4. +15 −1 Test/SendGrid/MailTest.php
  5. +2 −1 Test/SendGrid/SmtpTest.php
  6. +19 −1 Test/SendGrid/WebTest.php
View
@@ -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;
}
-}
+}
View
@@ -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;
}
-}
+}
View
@@ -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;
}
-}
+}
View
@@ -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());
}
-}
+}
@@ -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());
}
-}
+}
View
@@ -55,6 +55,24 @@ 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);
+
+ // 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);
}
-}
+}

0 comments on commit 6fa6777

Please sign in to comment.