Skip to content
This repository has been archived by the owner on Apr 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #15 from evaldemar/master
Browse files Browse the repository at this point in the history
fix for headers in message
  • Loading branch information
ofat authored Dec 7, 2016
2 parents 2a9f110 + e8f2a76 commit 41c5e6e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,40 @@ Yii::$app->mail->compose('contact/html', ['contactForm' => $form])
->setFrom('from@domain.com')
->setTo($form->email)
->setSubject($form->subject)
->send();
->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);
```
28 changes: 26 additions & 2 deletions libs/SimpleEmailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
*
Expand Down Expand Up @@ -273,7 +297,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') {
Expand Down Expand Up @@ -375,7 +399,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);
}
}
}
17 changes: 15 additions & 2 deletions libs/SimpleEmailServiceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 41c5e6e

Please sign in to comment.