This repository has been archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Mailer.php
107 lines (92 loc) · 2.46 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
*
* Mailer implements a mailer based on Amazon SES.
*
* To use Mailer, you should configure it in the application configuration like the following,
*
* ~~~
* 'components' => [
* ...
* 'mail' => [
* 'class' => 'yashop\ses\Mailer',
* 'access_key' => 'Your access key',
* 'secret_key' => 'Your secret key'
* ],
* ...
* ],
* ~~~
*
* To send an email, you may use the following code:
*
* ~~~
* Yii::$app->mail->compose('contact/html', ['contactForm' => $form])
* ->setFrom('[email protected]')
* ->setTo($form->email)
* ->setSubject($form->subject)
* ->send();
* ~~~
*
* @author Vitaliy Ofat <[email protected]>
*/
namespace yashop\ses;
use yashop\ses\libs\SimpleEmailService;
use Yii;
use yii\mail\BaseMailer;
class Mailer extends BaseMailer
{
/**
* @var string message default class name.
*/
public $messageClass = 'yashop\ses\Message';
/**
* @var string Amazon ses api access key
*/
public $access_key;
/**
* @var string Amazon ses api secret key
*/
public $secret_key;
/**
* @var string A default from address to send email
*/
public $default_from;
/**
* @var string Amazon ses host
*/
public $host = 'email.us-east-1.amazonaws.com';
/**
* @var \yashop\ses\libs\SimpleEmailService SimpleEmailService instance.
*/
private $_ses;
/**
* @return \yashop\ses\libs\SimpleEmailService SimpleEmailService instance.
*/
public function getSES()
{
if (!is_object($this->_ses)) {
$this->_ses = new SimpleEmailService($this->access_key, $this->secret_key, $this->host);
}
return $this->_ses;
}
/**
* @inheritdoc
*/
protected function sendMessage($message)
{
$address = $message->getTo();
if (is_array($address)) {
$address = implode(', ', array_keys($address));
}
Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
if ( is_null($message->getFrom()) && isset($this->default_from)) {
if(!is_array($this->default_from)){
$this->default_from = array($this->default_from => $this->default_from);
}
$message->setFrom($this->default_from);
}
$res = $this->getSES()->sendEmail($message->getSesMessage());
$message->setDate(time());
return count($res) > 0;
}
}