Skip to content
Browse files

Fixed smtp api and web api to use either headers or non headers depen…

…ding on the users situation, and to intelligently decide when to use them
  • Loading branch information...
1 parent fa6367a commit 9f953acf93f5c39e9199298e4d22cecdb2fd8f62 CJ Buchmann committed
Showing with 121 additions and 11 deletions.
  1. +61 −0 SendGrid/Mail.php
  2. +31 −7 SendGrid/Smtp.php
  3. +29 −4 SendGrid/Web.php
View
61 SendGrid/Mail.php
@@ -15,6 +15,8 @@ class Mail
$attachment_list,
$header_list = array();
+ protected $use_headers;
+
public function __construct()
{
@@ -157,6 +159,65 @@ public function setCcs(array $email_list)
$this->cc_list = $email_list;
return $this;
}
+
+ /**
+ * useHeaders
+ * Checks to see whether or not we can or should you headers. In most cases,
+ * we prefer to send our recipients through the headers, but in some cases,
+ * we actually don't want to. However, there are certain circumstances in
+ * which we have to.
+ */
+ public function useHeaders()
+ {
+ if($this->_preferNotToUseHeaders() && !$this->_isHeadersRequired())
+ {
+ return false;
+ }
+ return true;
+ }
+
+ public function tomagotchi($preference)
+ {
+ $this->use_headers = $preference;
+
+ return $this;
+ }
+
+ /**
+ * isHeaderRequired
+ * determines whether or not we need to force recipients through the smtpapi headers
+ * @return boolean, if true headers are required
+ */
+ private function _isHeadersRequired()
+ {
+ if(count($this->getAttachments()) > 0 || $this->use_headers )
+ {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * _preferNotToUseHeaders
+ * There are certain cases in which headers are not a preferred choice
+ * to send email, as it limits some basic email functionality. Here, we
+ * check for any of those rules, and add them in to decide whether or
+ * not to use headers
+ * @return boolean, if true we don't
+ */
+ private function _preferNotToUseHeaders()
+ {
+ if (count($this->getBccs()) > 0 || count($this->getBCcs()) > 0 || ($this->use_headers !== null && !$this->use_headers))
+ {
+ return true;
+ }
+ return false;
+ }
+
+ public function forceHeaders()
+ {
+
+ }
/**
* setCc
View
38 SendGrid/Smtp.php
@@ -62,10 +62,10 @@ private function _getSwiftInstance($port)
protected function _mapToSwift(Mail $mail)
{
$message = new \Swift_Message($mail->getSubject());
-
+
/*
* Since we're sending transactional email, we want the message to go to one person at a time, rather
- * than a bulk send. In order to do this, we'll have to send the list of recipients through the headers
+ * than a bulk send on one message. In order to do this, we'll have to send the list of recipients through the headers
* but Swift still requires a 'to' address. So we'll falsify it with the from address, as it will be
* ignored anyway.
*/
@@ -76,6 +76,35 @@ protected function _mapToSwift(Mail $mail)
$message->setCc($mail->getCcs());
$message->setBcc($mail->getBccs());
+ // determine whether or not we can use SMTP recipients (non header based)
+ if($mail->useHeaders())
+ {
+ //send header based email
+ $message->setTo($mail->getFrom());
+
+ //here we'll add the recipients list to the headers
+ $headers = $mail->getHeaders();
+ $headers['to'] = $mail->getTos();
+ $mail->setHeaders($headers);
+ }
+ else
+ {
+ $recipients = array();
+ foreach ($mail->getTos() as $recipient)
+ {
+ if(preg_match("/(.*)<(.*)>/", $recipient, $results))
+ {
+ $recipients[trim($results[2])] = trim($results[1]);
+ }
+ else
+ {
+ $recipients[] = $recipient;
+ }
+ }
+
+ $message->setTo($recipients);
+ }
+
$attachments = $mail->getAttachments();
//add any attachments that were added
@@ -87,11 +116,6 @@ protected function _mapToSwift(Mail $mail)
}
}
- //here we'll add the recipients list to the headers
- $headers = $mail->getHeaders();
- $headers['to'] = $mail->getTos();
- $mail->setHeaders($headers);
-
//add all the headers
$headers = $message->getHeaders();
$headers->addTextHeader('X-SMTPAPI', $mail->getHeadersJson());
View
33 SendGrid/Web.php
@@ -25,10 +25,6 @@ public function __construct($username, $password)
*/
protected function _prepMessageData(Mail $mail)
{
- // workaround for posting recipients (in case there are a lot)
- $headers = $mail->getHeaders();
- $headers['to'] = $mail->getTos();
- $mail->setHeaders($headers);
/* the api expects a 'to' parameter, but this parameter will be ignored
* since we're sending the recipients through the header. The from
@@ -46,6 +42,23 @@ protected function _prepMessageData(Mail $mail)
'x-smtpapi' => $mail->getHeadersJson()
);
+ // determine if we should send our recipients through our headers,
+ // and set the properties accordingly
+ if($mail->useHeaders())
+ {
+ // workaround for posting recipients through SendGrid headers
+ $headers = $mail->getHeaders();
+ $headers['to'] = $mail->getTos();
+ $mail->setHeaders($headers);
+
+ $params['x-smtpapi'] = $mail->getHeadersJson();
+ }
+ else
+ {
+ $params['to'] = $mail->getTos();
+ }
+
+
if($mail->getAttachments())
{
foreach($mail->getAttachments() as $attachment)
@@ -89,6 +102,18 @@ public function send(Mail $mail)
{
$data = $this->_prepMessageData($mail);
+ //if we're not using headers, we need to send a url friendly post
+ if(!$mail->useHeaders())
+ {
+ $data = http_build_query($data);
+
+ echo " \r\n\r\n sending web non-headers \r\n\r\n";
+ }
+ else
+ {
+ echo "\r\n\r\n sending web headers \r\n\r\n";
+ }
+
$request = $this->domain . $this->endpoint;
// we'll append the Bcc and Cc recipients to the url endpoint (GET)

0 comments on commit 9f953ac

Please sign in to comment.
Something went wrong with that request. Please try again.