Skip to content

Commit 37131aa

Browse files
author
Mark Guinn
committed
Merge pull request #17 from xini/feature-emogrified-mailer-using-mail
add EmogrifiedMailer which uses mail() function for sending
2 parents 5fcdef7 + a47f780 commit 37131aa

File tree

1 file changed

+248
-0
lines changed

1 file changed

+248
-0
lines changed

code/EmogrifiedMailer.php

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
<?php
2+
/**
3+
* This is a simple extension of the built in SS email class
4+
* that uses the PHPMailer library to send emails via mail() and Emogifier to inline CSS.
5+
*
6+
* Usage: (in mysite/_config.php)
7+
*
8+
* @example $mailer = new EmogrifiedMailer('UTF-8');
9+
* Email::set_mailer($mailer);
10+
* @package smtpmailer
11+
*/
12+
class EmogrifiedMailer extends Mailer
13+
{
14+
/**
15+
* @var string $charset - charset for mail message
16+
*/
17+
protected $charset;
18+
19+
/**
20+
* CSS file containing classes to inline into the email's HTML
21+
*
22+
* @var string $cssfile path to css file from project root
23+
*/
24+
protected $cssfile;
25+
26+
/**
27+
* creates and configures the mailer
28+
*/
29+
public function __construct($charset=false, $cssfile=false)
30+
{
31+
if ($charset === false) {
32+
$charset = Config::inst()->get('SmtpMailer', 'charset');
33+
}
34+
$this->setCharset($charset);
35+
36+
if ($cssfile === false) {
37+
$cssfile = Config::inst()->get('EmogrifiedMailer', 'cssfile');
38+
}
39+
$this->setCSSfile($cssfile);
40+
}
41+
42+
/**
43+
* @param string $charset
44+
* @return $this
45+
*/
46+
public function setCharset($charset)
47+
{
48+
$this->charset = $charset;
49+
return $this;
50+
}
51+
52+
/**
53+
* @return string
54+
*/
55+
public function getCharset()
56+
{
57+
return $this->charset;
58+
}
59+
60+
/**
61+
* @return string
62+
*/
63+
public function getCSSfile()
64+
{
65+
return $this->cssfile;
66+
}
67+
68+
/**
69+
* @param integer $cssfile
70+
* @return $this
71+
*/
72+
public function setCSSfile($cssfile)
73+
{
74+
$this->cssfile = (string)$cssfile;
75+
return $this;
76+
}
77+
78+
/**
79+
* creates a new phpmailer object
80+
*/
81+
protected function initMailer()
82+
{
83+
$mail = new PHPMailer();
84+
$mail->isMail();
85+
86+
if ($this->charset) {
87+
$mail->CharSet = $this->charset;
88+
}
89+
90+
return $mail;
91+
}
92+
93+
/**
94+
* shared setup for both html and plain
95+
*/
96+
protected function initEmail($to, $from, $subject, $attachedFiles = false, $customheaders = false)
97+
{
98+
$mail = $this->initMailer();
99+
100+
// set the from
101+
list($mail->From, $mail->FromName) = $this->splitName($from);
102+
103+
// set the to
104+
$this->explodeList($to, $mail, 'AddAddress');
105+
106+
// set cc and bcc if needed
107+
if (is_array($customheaders) && isset($customheaders['Cc'])) {
108+
$this->explodeList($customheaders['Cc'], $mail, 'AddCC');
109+
unset($customheaders['Cc']);
110+
}
111+
112+
if (is_array($customheaders) && isset($customheaders['Bcc'])) {
113+
$this->explodeList($customheaders['Bcc'], $mail, 'AddBCC');
114+
unset($customheaders['Bcc']);
115+
}
116+
117+
// set up the subject
118+
$mail->Subject = $subject;
119+
120+
// add any attachments
121+
if (is_array($attachedFiles)) {
122+
// include any specified attachments as additional parts
123+
foreach ($attachedFiles as $file) {
124+
if (isset($file['tmp_name']) && isset($file['name'])) {
125+
$mail->AddAttachment($file['tmp_name'], $file['name']);
126+
} elseif (isset($file['contents'])) {
127+
$mail->AddStringAttachment($file['contents'], $file['filename']);
128+
} else {
129+
$mail->AddAttachment($file);
130+
}
131+
}
132+
}
133+
134+
// Messages with the X-SilverStripeMessageID header can be tracked
135+
if (isset($customheaders["X-SilverStripeMessageID"]) && defined('BOUNCE_EMAIL')) {
136+
$bounceAddress = BOUNCE_EMAIL;
137+
// Get the human name from the from address, if there is one
138+
if (ereg('^([^<>]+)<([^<>])> *$', $from, $parts)) {
139+
$bounceAddress = "$parts[1]<$bounceAddress>";
140+
}
141+
} else {
142+
$bounceAddress = $from;
143+
}
144+
145+
$headers["X-Mailer"] = X_MAILER;
146+
if (!isset($customheaders["X-Priority"])) {
147+
$headers["X-Priority"] = 3;
148+
}
149+
150+
$headers = array_merge((array)$headers, (array)$customheaders);
151+
152+
foreach ($headers as $k => $v) {
153+
$mail->AddCustomHeader("$k: $v");
154+
}
155+
156+
return $mail;
157+
}
158+
159+
/**
160+
* takes an email with or without a name and returns
161+
* email and name as separate parts
162+
* @param string $in
163+
* @return array ($email, $name)
164+
*/
165+
protected function splitName($in)
166+
{
167+
if (preg_match('/^\s*(.+)\s+<(.+)>\s*$/', $in, $m)) {
168+
return array($m[2], $m[1]);
169+
} else {
170+
return array($in, '');
171+
}
172+
}
173+
174+
/**
175+
* takes a list of emails, splits out the name and calls
176+
* the given function. meant to be used with AddAddress, AddBcc and AddCc
177+
*/
178+
protected function explodeList($in, $mail, $func)
179+
{
180+
$list = explode(',', $in);
181+
foreach ($list as $item) {
182+
list($a, $b) = $this->splitName(trim($item));
183+
$mail->$func($a, $b);
184+
}
185+
}
186+
187+
/**
188+
* Send a plain-text email.
189+
*
190+
* @param string $to
191+
* @param string $from
192+
* @param string $subject
193+
* @param string $plainContent
194+
* @param array|bool $attachedFiles
195+
* @param array|bool $customheaders
196+
*
197+
* @return bool
198+
*/
199+
public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customheaders = false)
200+
{
201+
$mail = $this->initEmail($to, $from, $subject, $attachedFiles, $customheaders);
202+
203+
// set up the body
204+
$mail->Body = $plainContent;
205+
206+
// send and return
207+
if ($mail->Send()) {
208+
return array($to,$subject,$plainContent,$customheaders);
209+
} else {
210+
return false;
211+
}
212+
}
213+
214+
/**
215+
* Send a multi-part HTML email with inlined CSS
216+
*
217+
* @param string $to
218+
* @param string $from
219+
* @param string $subject
220+
* @param string $htmlContent
221+
* @param array|bool $attachedFiles
222+
* @param array|bool $customheaders
223+
* @param bool $plainContent
224+
* @param bool $inlineImages
225+
*
226+
* @return bool
227+
*/
228+
public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false, $plainContent = false, $inlineImages = false)
229+
{
230+
$mail = $this->initEmail($to, $from, $subject, $attachedFiles, $customheaders);
231+
232+
// set up the body
233+
// @todo inlineimages
234+
$mail->Body = InlineCSS::convert($htmlContent, $this->getCSSfile());
235+
$mail->IsHTML(true);
236+
if ($plainContent) {
237+
$mail->AltBody = $plainContent;
238+
}
239+
240+
// send and return
241+
if ($mail->Send()) {
242+
return array($to, $subject, $mail->Body, $customheaders);
243+
} else {
244+
return false;
245+
}
246+
}
247+
}
248+

0 commit comments

Comments
 (0)