Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Jun 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions SendGrid/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

class Mail
{
private $to_list,

private $to_list,
$from,
$from_name,
$reply_to,
$cc_list,
$bcc_list,
$subject,
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -667,4 +718,4 @@ protected function _preferNotToUseHeaders()
return false;
}

}
}
8 changes: 6 additions & 2 deletions SendGrid/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -138,4 +142,4 @@ public function send(Mail $mail)

return true;
}
}
}
12 changes: 10 additions & 2 deletions SendGrid/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -58,7 +66,7 @@ protected function _prepMessageData(Mail $mail)
$params['to'] = $mail->getTos();
}


if($mail->getAttachments())
{
foreach($mail->getAttachments() as $attachment)
Expand Down Expand Up @@ -129,4 +137,4 @@ public function send(Mail $mail)

return $response;
}
}
}
28 changes: 27 additions & 1 deletion Test/SendGrid/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -520,4 +546,4 @@ public function testUseHeaders()

$this->assertTrue($mail->useHeaders());
}
}
}
3 changes: 2 additions & 1 deletion Test/SendGrid/SmtpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')->
Expand Down Expand Up @@ -44,4 +45,4 @@ public function testPorts()
$mock->setPort('52');
$this->assertEquals('52', $mock->getPort());
}
}
}
26 changes: 25 additions & 1 deletion Test/SendGrid/WebTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -71,4 +95,4 @@ public function testSendResponse()

$this->assertEquals("{\"message\": \"error\", \"errors\": [\"Bad username / password\"]}", $response);
}
}
}