-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathYiiSendGridMail.php
More file actions
executable file
·169 lines (150 loc) · 3.91 KB
/
YiiSendGridMail.php
File metadata and controls
executable file
·169 lines (150 loc) · 3.91 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
require __DIR__.'/vendors/sendgrid-php/lib/SendGrid/Email.php';
/**
* YiiSendGridMail class file.
*
* @author Gustavo Salomé Silva <gustavonips@gmail.com>
*/
/**
* Extends {@link SendGrid\Email} to render a view file if needed
*/
class YiiSendGridMail extends SendGrid\Email {
/**
* the view to use for rendering the body, null if no view is
* used. An extra variable $mail will be passed to the view .which you may
* use to set e.g. the email subject from within the view
* @var string
*/
public $view;
/**
* layout to use to render files
* @var string
*/
public $layout;
/**
* @var string the path to the location where mail views are stored.
* Defaults to 'application.views.mail'.
*/
public $viewPath='application.views.mail';
/**
* set the view path, if avaliable
*/
public function __construct($viewPath=null)
{
if($viewPath!==null)
$this->viewPath=$viewPath;
return parent::__construct();
}
/**
* if set to null, no layout is used
* @param string $value path or alias to the layout
* @return YiiSendGridMail
*/
public function setLayout($value)
{
$this->layout=$value;
return $this;
}
/**
* set a view to be used
* @param string $name view name
* @return YiiSendGridMail
*/
public function setView($name)
{
$this->view=$name;
return $this;
}
/**
* Set the body of this entity, either as html or array of view parameters
*
* @param string|array the body of the message.
* if view is not set, set
* If a $this->view is set and this is a string, this is passed to the view as $content.
* If $this->view is set and this is an array, the array values are passed to the view like in the
* controller render() method
* @return SendGrid\Email
*/
public function setHtml($html = '')
{
if ($this->view !== null)
{
//if it's an array, we pass as properties to render the view, otherwise we use as the view $content variable
if (!is_array($html))
$data=array('content'=>$html);
else
$data=$html;
// if Yii::app()->controller doesn't exist create a dummy
// controller to render the view (needed in the console app)
if(isset(Yii::app()->controller))
$controller = Yii::app()->controller;
else
$controller = new CController('SendGridMail');
// renderPartial won't work with CConsoleApplication, so use
// renderInternal - this requires that we use an actual path to the
// view rather than the usual alias
$viewPath = Yii::getPathOfAlias($this->viewPath.'.'.$this->view).'.php';
if($viewPath===false)
throw new Exception("Invalid mail view '{$this->view}' in path '{$this->viewPath}'");
$html = $controller->renderInternal($viewPath, $data, true);
//applys the layout if needed
if($this->layout!==null)
{
$layout=$controller->getLayoutFile($this->layout);
$html=$controller->renderInternal($layout,array_merge($data,array('content'=>$html)),true);
}
}
return parent::setHtml($html);
}
/**
* (non-PHPdoc)
* @see \SendGrid\Email::setTos()
*/
public function setTos(array $emails)
{
return $this->setTo($emails);
}
/**
* add a new recipients for the email
* you can use to add one or more
* example:
* <pre>
* $mail->setTo(array("john@email.com"=>"John","maria@email.com"));
* $mail->setTo("joana@email.com","Joana");
* </pre>
* @param string|array $email
* @param string $name
* @return \SendGrid\Email
*/
public function setTo($email, $name=null)
{
if(!is_array($email))
$email=array($email=>$name);
foreach($email as $mail=>$nam)
{
if(is_int($mail))
{
$mail=$nam;
$nam=null;
}
parent::addTo($mail,$nam);
}
return $this;
}
/**
* (non-PHPdoc)
* @see YiiSendGridMail::setTo()
*/
public function addTo($email,$name=null)
{
return $this->setTo($email,$name);
}
/**
* Get a list of recipients
* @return array
*/
public function getTo()
{
return $this->smtpapi->to;
}
}