Skip to content

Commit

Permalink
Merge pull request #2 from eduardoconceicao/unsubscribe-email
Browse files Browse the repository at this point in the history
Added unsubscribe functionality
  • Loading branch information
seeruk committed Sep 30, 2015
2 parents c822c84 + a4b24c4 commit 557f833
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 42 deletions.
5 changes: 4 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ services:
arguments:
username: "%savch_sendgrid.api_user%"
password: "%savch_sendgrid.api_key%"

SendGrid.SendGridMailerService:
class: Savch\SendgridBundle\Service\SendGridTemplatingMailerService
arguments:
sendGrid: "@SendGrid.SendGridService"
templating: "@templating"
throwExceptionsOnFail: true
credentials:
username: "%savch_sendgrid.api_user%"
password: "%savch_sendgrid.api_key%"
108 changes: 67 additions & 41 deletions Service/SendGridTemplatingMailerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
/**
* SendGridTemplatingMailerService.php
* Definition of class SendGridTemplatingMailerService
*
*
* Created 29-Jan-2014 17:43:59
*
*
* @author M.D.Ward <[email protected]>
* @copyright (c) 2014, Byng Systems/SkillsWeb Ltd
*/
Expand All @@ -24,66 +24,70 @@
* SendGridTemplatingMailerService - Registered as a Symfony2 service to provide quick,
* convenient methods of sending a multipart plain & HTML email via the
* SendGrid mail API
*
*
* @author M.D.Ward <[email protected]>
*/
class SendGridTemplatingMailerService
{

/**
*
* @var \SendGrid
*/
private $sendGrid;

/**
*
*
* @var \Symfony\Bundle\TwigBundle\TwigEngine
*/
private $templating;

/**
*
* @var boolean
*/
protected $throwExceptionsOnFail;




/**
*
* @param SendGrid $sendGrid
* @param \Symfony\Bundle\TwigBundle\TwigEngine $templating
* @param boolean $throwExceptionsOnFail
* @var array
*/
private $credentials;

/**
*
* @param SendGrid $sendGrid
* @param TwigEngine $templating
* @param boolean $throwExceptionsOnFail
* @param array $credentials
*/
public function __construct(SendGrid $sendGrid, TwigEngine $templating, $throwExceptionsOnFail = true)
public function __construct(SendGrid $sendGrid, TwigEngine $templating, $throwExceptionsOnFail = true, $credentials = array())
{
$this->sendGrid = $sendGrid;
$this->templating = $templating;

$this->credentials = $credentials;

$this->setThrowExceptionsOnFail($throwExceptionsOnFail);
}

/**
*
*
* @return \Symfony\Bundle\TwigBundle\TwigEngine
*/
public function getTemplating()
{
return $this->templating;
}

/**
*
*
* @return type
*/
protected function getSendGrid()
{
return $this->sendGrid;
}

/**
*
*
* @return boolean
*/
public function getThrowExceptionsOnFail()
Expand All @@ -92,40 +96,41 @@ public function getThrowExceptionsOnFail()
}

/**
*
*
* @param boolean $throwExceptionsOnFail
*/
public function setThrowExceptionsOnFail($throwExceptionsOnFail)
{
$this->throwExceptionsOnFail = ($throwExceptionsOnFail === true);
}

public function sendHtmlEmail(array $from, array $to, $subject, $bodyHtml, array $additionalHeaders = array(), array $attachments = null)
{
//
//
$email = static::buildBaseEmail($from, $to, $subject, $additionalHeaders, $attachments);

// If the given body is a TemplatedEmailBody object, populate and reassign the string value to itself
if ($bodyHtml instanceof TemplatedEmailBody) {
$bodyHtml = $this->templating->renderResponse(
$bodyHtml->getTemplateName(),
$bodyHtml->getVariables()
)->getContent();
}

$email->setHtml($bodyHtml);

return $this->processResponse($this->sendGrid->web->send($email));
}

/**
*
*
* @param type $response
*
* @return boolean
*/
protected function processResponse(StdClass $response, $throwExceptionOnFail = true) {
$result = (isset($response->message) && $response->message == "success");

if ($result === false && $this->throwExceptionsOnFail === true) {
throw new MailNotSentException(
(
Expand All @@ -135,45 +140,66 @@ protected function processResponse(StdClass $response, $throwExceptionOnFail = t
)
);
}

return $result;
}

protected static function buildBaseEmail(array $from, array $to, $subject, array $additionalHeaders = array(), array $attachments = null)
{
$email = new Email();

$fromAddress = current(array_keys($from));
$fromName = current($from);

$email->setFrom($fromAddress)
->setFromName($fromName)
->setSubject($subject);

// Set to headers
foreach ($to as $toAddress => $toName) {
$email->addTo($toAddress, $toName);
}

// Set CC header if a value is given
if (isset($additionalHeaders["cc"]) && is_array(($cc = $additionalHeaders["cc"]))) {
$email->setCcs($cc);
}

// Set BCC header if a valud is given
if (isset($additionalHeaders["bcc"]) && is_array(($bcc = $additionalHeaders["bcc"]))) {
$email->setBccs($bcc);
}

if (isset($additionalHeaders["reply-to"])) {
$email->setReplyTo($additionalHeaders["reply-to"]);
}

if (isset($attachments)) {
$email->setAttachments($attachments);
}

return $email;
}


/**
* Unsubscribe an email from the global mailing list
*
* @param string $email
*
* @return \Unirest\HttpResponse
*/
public function unsubscribeEmail($email)
{
$url = "https://api.sendgrid.com/api/unsubscribes.add.json";

$form = array();
$form['api_user'] = $this->credentials['username'];
$form['api_key'] = $this->credentials['password'];
$form['email'] = $email;

$response = \Unirest::post($url, array(), $form);

return $response;
}

}

0 comments on commit 557f833

Please sign in to comment.