From 09b51a77b0efc7dfe5e4050da6823b4e1dbb802f Mon Sep 17 00:00:00 2001 From: Vladimir Voronin Date: Wed, 7 Dec 2016 11:29:08 +0300 Subject: [PATCH 1/3] fix for headers in message --- libs/SimpleEmailService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/SimpleEmailService.php b/libs/SimpleEmailService.php index 8e46e3b..02f44ef 100644 --- a/libs/SimpleEmailService.php +++ b/libs/SimpleEmailService.php @@ -273,7 +273,7 @@ public function sendEmail($sesMessage) { } $rest = new SimpleEmailServiceRequest($this, 'POST'); - $action = empty($sesMessage->attachments) ? 'SendEmail' : 'SendRawEmail'; + $action = empty($sesMessage->attachments) && empty($sesMessage->headers) ? 'SendEmail' : 'SendRawEmail'; $rest->setParameter('Action', $action); if($action == 'SendRawEmail') { From cdaedc7ee703506c396b615cfaaba036052ce6c0 Mon Sep 17 00:00:00 2001 From: Vladimir Voronin Date: Wed, 7 Dec 2016 11:34:04 +0300 Subject: [PATCH 2/3] fix, $error may not always be a string --- libs/SimpleEmailService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/SimpleEmailService.php b/libs/SimpleEmailService.php index 02f44ef..4b3debd 100644 --- a/libs/SimpleEmailService.php +++ b/libs/SimpleEmailService.php @@ -375,7 +375,7 @@ public function __triggerError($functionname, $error) trigger_error($message, E_USER_WARNING); } else { - trigger_error(sprintf("SimpleEmailService::%s(): Encountered an error: %s", $functionname, $error), E_USER_WARNING); + trigger_error(sprintf("SimpleEmailService::%s(): Encountered an error: %s", $functionname, serialize($error)), E_USER_WARNING); } } } \ No newline at end of file From e8f2a76bdf62a9cb6fb41d9723d8a94592b87269 Mon Sep 17 00:00:00 2001 From: Vladimir Voronin Date: Wed, 7 Dec 2016 19:13:05 +0300 Subject: [PATCH 3/3] add support keep alive --- README.md | 38 +++++++++++++++++++++++++++++- libs/SimpleEmailService.php | 24 +++++++++++++++++++ libs/SimpleEmailServiceRequest.php | 17 +++++++++++-- 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9845c76..0285bbc 100644 --- a/README.md +++ b/README.md @@ -47,4 +47,40 @@ Yii::$app->mail->compose('contact/html', ['contactForm' => $form]) ->setFrom('from@domain.com') ->setTo($form->email) ->setSubject($form->subject) - ->send(); \ No newline at end of file + ->send(); +``` + + +To send an email with headers, you may use the following code: + +```php +Yii::$app->mail->compose('contact/html', ['contactForm' => $form]) + ->setFrom('from@domain.com') + ->setTo($form->email) + ->setSubject($form->subject) + ->setHeader('Precedence', 'bulk') + ->setHeader('List-id', '<1>') + ->setHeader('List-Unsubscribe', Url::to(['user/unsubscribe'], true)) + ->send(); +``` + +Increase the speed of sending emails: + +```php +Yii::$app->mailer->getSES()->enableVerifyHost(false); +Yii::$app->mailer->getSES()->enableVerifyPeer(false); +Yii::$app->mailer->getSES()->enableKeepAlive(); + +foreach ($emails as $email) { + Yii::$app->mail->compose('delivery/mail', []) + ->setFrom('from@domain.com') + ->setTo($email) + ->setSubject($subject) + ->setHeader('Precedence', 'bulk') + ->setHeader('List-id', '<1>') + ->setHeader('List-Unsubscribe', Url::to(['user/unsubscribe'], true)) + ->send(); +} + +Yii::$app->mailer->getSES()->enableKeepAlive(false); +``` diff --git a/libs/SimpleEmailService.php b/libs/SimpleEmailService.php index 4b3debd..5992ee1 100644 --- a/libs/SimpleEmailService.php +++ b/libs/SimpleEmailService.php @@ -64,6 +64,11 @@ public function getHost() { return $this->__host; } protected $__verifyHost = 1; protected $__verifyPeer = 1; + protected $__curl = false; + + public function getCurl() { return $this->__curl; } + public function setCurl($curl) { $this->__curl = $curl; } + // verifyHost and verifyPeer determine whether curl verifies ssl certificates. // It may be necessary to disable these checks on certain systems. // These only have an effect if SSL is enabled. @@ -87,6 +92,25 @@ public function __construct($accessKey = null, $secretKey = null, $host = 'email $this->__host = $host; } + function __destruct() { + if ($this->__curl && $this->__curl !== true) { + @curl_close($this->__curl); + $this->__curl = null; + } + } + + public function enableKeepAlive($enable = true) { + if (!$enable) { + if ($this->__curl && $this->__curl !== true) { + @curl_close($this->__curl); + } + + $this->__curl = false; + } else if ($enable && $this->__curl === false) { + $this->__curl = true; + } + } + /** * Set AWS access key and secret key * diff --git a/libs/SimpleEmailServiceRequest.php b/libs/SimpleEmailServiceRequest.php index 97a9dd2..fe25291 100644 --- a/libs/SimpleEmailServiceRequest.php +++ b/libs/SimpleEmailServiceRequest.php @@ -94,7 +94,18 @@ public function getResponse() { $url = 'https://'.$this->ses->getHost().'/'; // Basic setup - $curl = curl_init(); + $curl = $this->ses->getCurl(); + + if ($curl === true) { + $curl = curl_init(); + curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); + $this->ses->setCurl($curl); + } + + if (!$curl) { + $curl = curl_init(); + } + curl_setopt($curl, CURLOPT_USERAGENT, 'SimpleEmailService/php'); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, ($this->ses->verifyHost() ? 2 : 0)); @@ -140,7 +151,9 @@ public function getResponse() { ); } - @curl_close($curl); + if (!$this->ses->getCurl()) { + @curl_close($curl); + } // Parse body into XML if ($this->response->error === false && isset($this->response->body)) {