Skip to content

Commit

Permalink
Merge pull request #5 from theycallmeswift/missing-api-params
Browse files Browse the repository at this point in the history
Add optional from name and reply-to fields to web and smtp API calls

Closes #4
Closes #5

WIP #3
  • Loading branch information
theycallmeswift committed Jun 18, 2012
2 parents 60abf1b + e725d5a commit c0da924
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 15 deletions.
67 changes: 59 additions & 8 deletions SendGrid/Mail.php
Expand Up @@ -4,9 +4,11 @@


class Mail class Mail
{ {

private $to_list, private $to_list,
$from, $from,
$from_name,
$reply_to,
$cc_list, $cc_list,
$bcc_list, $bcc_list,
$subject, $subject,
Expand All @@ -19,9 +21,10 @@ class Mail


public function __construct() public function __construct()
{ {

$this->from_name = false;
$this->reply_to = false;
} }

/** /**
* _removeFromList * _removeFromList
* Given a list of key/value pairs, removes the associated keys * 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 * getFrom
* get the from email address * get the from email address
* @param Boolean $as_array - return the from as an assocative array
* @return the from email address * @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; $this->from = $email;
return $this; 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 * getCc
* get the Carbon Copy list of recipients * get the Carbon Copy list of recipients
Expand Down Expand Up @@ -667,4 +718,4 @@ protected function _preferNotToUseHeaders()
return false; return false;
} }


} }
8 changes: 6 additions & 2 deletions SendGrid/Smtp.php
Expand Up @@ -70,12 +70,16 @@ protected function _mapToSwift(Mail $mail)
* ignored anyway. * ignored anyway.
*/ */
$message->setTo($mail->getFrom()); $message->setTo($mail->getFrom());
$message->setFrom($mail->getFrom()); $message->setFrom($mail->getFrom(true));
$message->setBody($mail->getHtml(), 'text/html'); $message->setBody($mail->getHtml(), 'text/html');
$message->addPart($mail->getText(), 'text/plain'); $message->addPart($mail->getText(), 'text/plain');
$message->setCc($mail->getCcs()); $message->setCc($mail->getCcs());
$message->setBcc($mail->getBccs()); $message->setBcc($mail->getBccs());


if(($replyto = $mail->getReplyTo())) {
$message->setReplyTo($replyto);
}

// determine whether or not we can use SMTP recipients (non header based) // determine whether or not we can use SMTP recipients (non header based)
if($mail->useHeaders()) if($mail->useHeaders())
{ {
Expand Down Expand Up @@ -138,4 +142,4 @@ public function send(Mail $mail)


return true; return true;
} }
} }
12 changes: 10 additions & 2 deletions SendGrid/Web.php
Expand Up @@ -42,6 +42,14 @@ protected function _prepMessageData(Mail $mail)
'x-smtpapi' => $mail->getHeadersJson() '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, // determine if we should send our recipients through our headers,
// and set the properties accordingly // and set the properties accordingly
if($mail->useHeaders()) if($mail->useHeaders())
Expand All @@ -58,7 +66,7 @@ protected function _prepMessageData(Mail $mail)
$params['to'] = $mail->getTos(); $params['to'] = $mail->getTos();
} }



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


return $response; return $response;
} }
} }
28 changes: 27 additions & 1 deletion Test/SendGrid/MailTest.php
Expand Up @@ -58,8 +58,34 @@ public function testFromAccessors()
$message = new SendGrid\Mail(); $message = new SendGrid\Mail();


$message->setFrom("foo@bar.com"); $message->setFrom("foo@bar.com");
$message->setFromName("John Doe");


$this->assertEquals("foo@bar.com", $message->getFrom()); $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() public function testCcAccessors()
Expand Down Expand Up @@ -520,4 +546,4 @@ public function testUseHeaders()


$this->assertTrue($mail->useHeaders()); $this->assertTrue($mail->useHeaders());
} }
} }
3 changes: 2 additions & 1 deletion Test/SendGrid/SmtpTest.php
Expand Up @@ -13,6 +13,7 @@ public function testConstruction()
$message = new SendGrid\Mail(); $message = new SendGrid\Mail();
$message-> $message->
setFrom('bar@foo.com')-> setFrom('bar@foo.com')->
setFromName('John Doe')->
setSubject('foobar subject')-> setSubject('foobar subject')->
setText('foobar text')-> setText('foobar text')->
addTo('foo@bar.com')-> addTo('foo@bar.com')->
Expand Down Expand Up @@ -44,4 +45,4 @@ public function testPorts()
$mock->setPort('52'); $mock->setPort('52');
$this->assertEquals('52', $mock->getPort()); $this->assertEquals('52', $mock->getPort());
} }
} }
26 changes: 25 additions & 1 deletion Test/SendGrid/WebTest.php
Expand Up @@ -55,6 +55,30 @@ public function testMockFunctions()
$this->assertEquals("&param[]=foo&param[]=bar&param[]=car&param[]=doo", $url_part); $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() public function testSendResponse()
{ {
$sendgrid = new SendGrid("foo", "bar"); $sendgrid = new SendGrid("foo", "bar");
Expand All @@ -71,4 +95,4 @@ public function testSendResponse()


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

0 comments on commit c0da924

Please sign in to comment.