2008-04-26 08:32:31 +02:00
|
|
|
<?php
|
2013-01-17 23:24:23 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
/**
|
|
|
|
* Mailer objects are responsible for actually sending emails.
|
2009-03-17 21:20:08 +01:00
|
|
|
* The default Mailer class will use PHP's mail() function.
|
2008-07-05 01:33:02 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-03-17 21:20:08 +01:00
|
|
|
* @subpackage email
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
2012-12-03 00:02:47 +01:00
|
|
|
class Mailer {
|
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
/**
|
2009-03-17 10:07:33 +01:00
|
|
|
* Send a plain-text email.
|
2009-03-17 21:20:08 +01:00
|
|
|
*
|
|
|
|
* @param string $to
|
|
|
|
* @param string $from
|
|
|
|
* @param string §subject
|
|
|
|
* @param string $plainContent
|
|
|
|
* @param bool $attachedFiles
|
|
|
|
* @param array $customheaders
|
2009-03-17 10:07:33 +01:00
|
|
|
* @return bool
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customheaders = false) {
|
2012-12-03 00:02:47 +01:00
|
|
|
// Not ensurely where this is supposed to be set, but defined it false for now to remove php notices
|
|
|
|
$plainEncoding = false;
|
|
|
|
|
|
|
|
if ($customheaders && is_array($customheaders) == false) {
|
|
|
|
echo "htmlEmail($to, $from, $subject, ...) could not send mail: improper \$customheaders passed:<BR>";
|
|
|
|
dieprintr($customheaders);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
// If the subject line contains extended characters, we must encode it
|
|
|
|
$subject = Convert::xml2raw($subject);
|
|
|
|
$subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
|
|
|
|
|
|
|
|
// Make the plain text part
|
|
|
|
$headers["Content-Type"] = "text/plain; charset=utf-8";
|
|
|
|
$headers["Content-Transfer-Encoding"] = $plainEncoding ? $plainEncoding : "quoted-printable";
|
|
|
|
|
|
|
|
$plainContent = ($plainEncoding == "base64")
|
|
|
|
? chunk_split(base64_encode($plainContent),60)
|
|
|
|
: $this->QuotedPrintable_encode($plainContent);
|
|
|
|
|
|
|
|
// Messages with attachments are handled differently
|
|
|
|
if($attachedFiles) {
|
|
|
|
// The first part is the message itself
|
|
|
|
$fullMessage = $this->processHeaders($headers, $plainContent);
|
|
|
|
$messageParts = array($fullMessage);
|
|
|
|
|
|
|
|
// Include any specified attachments as additional parts
|
|
|
|
foreach($attachedFiles as $file) {
|
|
|
|
if(isset($file['tmp_name']) && isset($file['name'])) {
|
|
|
|
$messageParts[] = $this->encodeFileForEmail($file['tmp_name'], $file['name']);
|
|
|
|
} else {
|
|
|
|
$messageParts[] = $this->encodeFileForEmail($file);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
|
|
|
|
// We further wrap all of this into another multipart block
|
|
|
|
list($fullBody, $headers) = $this->encodeMultipart($messageParts, "multipart/mixed");
|
|
|
|
|
|
|
|
// Messages without attachments do not require such treatment
|
|
|
|
} else {
|
|
|
|
$fullBody = $plainContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Email headers
|
|
|
|
$headers["From"] = $this->validEmailAddr($from);
|
|
|
|
|
|
|
|
// Messages with the X-SilverStripeMessageID header can be tracked
|
|
|
|
if(isset($customheaders["X-SilverStripeMessageID"]) && defined('BOUNCE_EMAIL')) {
|
|
|
|
$bounceAddress = BOUNCE_EMAIL;
|
|
|
|
// Get the human name from the from address, if there is one
|
|
|
|
if(preg_match('/^([^<>]+)<([^<>])> *$/', $from, $parts))
|
|
|
|
$bounceAddress = "$parts[1]<$bounceAddress>";
|
|
|
|
} else {
|
|
|
|
$bounceAddress = $from;
|
|
|
|
}
|
|
|
|
|
|
|
|
// $headers["Sender"] = $from;
|
|
|
|
$headers["X-Mailer"] = X_MAILER;
|
|
|
|
if(!isset($customheaders["X-Priority"])) {
|
|
|
|
$headers["X-Priority"] = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = array_merge((array)$headers, (array)$customheaders);
|
|
|
|
|
|
|
|
// the carbon copy header has to be 'Cc', not 'CC' or 'cc' -- ensure this.
|
|
|
|
if (isset($headers['CC'])) { $headers['Cc'] = $headers['CC']; unset($headers['CC']); }
|
|
|
|
if (isset($headers['cc'])) { $headers['Cc'] = $headers['cc']; unset($headers['cc']); }
|
|
|
|
|
|
|
|
// Send the email
|
|
|
|
$headers = $this->processHeaders($headers);
|
|
|
|
$to = $this->validEmailAddr($to);
|
|
|
|
|
|
|
|
// Try it without the -f option if it fails
|
|
|
|
if(!$result = @mail($to, $subject, $fullBody, $headers, "-f$bounceAddress"))
|
|
|
|
$result = mail($to, $subject, $fullBody, $headers);
|
|
|
|
|
|
|
|
if($result)
|
|
|
|
return array($to,$subject,$fullBody,$headers);
|
|
|
|
|
|
|
|
return false;
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an email as a both HTML and plaintext
|
2012-12-03 00:02:47 +01:00
|
|
|
*
|
2008-04-26 08:32:31 +02:00
|
|
|
* $attachedFiles should be an array of file names
|
2012-12-08 12:20:20 +01:00
|
|
|
* - if you pass the entire $_FILES entry, the user-uploaded filename will be preserved
|
2008-04-26 08:32:31 +02:00
|
|
|
* use $plainContent to override default plain-content generation
|
2009-03-17 10:07:33 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false,
|
2012-12-03 00:02:47 +01:00
|
|
|
$plainContent = false) {
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
if ($customheaders && is_array($customheaders) == false) {
|
|
|
|
echo "htmlEmail($to, $from, $subject, ...) could not send mail: improper \$customheaders passed:<BR>";
|
2010-12-17 01:56:03 +01:00
|
|
|
dieprintr($customheaders);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
$bodyIsUnicode = (strpos($htmlContent,"&#") !== false);
|
2012-12-08 12:20:20 +01:00
|
|
|
$plainEncoding = "";
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// We generate plaintext content by default, but you can pass custom stuff
|
|
|
|
$plainEncoding = '';
|
|
|
|
if(!$plainContent) {
|
|
|
|
$plainContent = Convert::xml2raw($htmlContent);
|
|
|
|
if(isset($bodyIsUnicode) && $bodyIsUnicode) $plainEncoding = "base64";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the subject line contains extended characters, we must encode the
|
|
|
|
$subject = Convert::xml2raw($subject);
|
2010-12-17 01:15:01 +01:00
|
|
|
$subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Make the plain text part
|
2011-07-15 07:05:50 +02:00
|
|
|
$headers["Content-Type"] = "text/plain; charset=utf-8";
|
2008-04-26 08:32:31 +02:00
|
|
|
$headers["Content-Transfer-Encoding"] = $plainEncoding ? $plainEncoding : "quoted-printable";
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
$plainPart = $this->processHeaders($headers, ($plainEncoding == "base64")
|
2012-09-26 23:34:00 +02:00
|
|
|
? chunk_split(base64_encode($plainContent),60)
|
2012-12-03 00:02:47 +01:00
|
|
|
: wordwrap($this->QuotedPrintable_encode($plainContent),75));
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Make the HTML part
|
2011-07-15 07:05:50 +02:00
|
|
|
$headers["Content-Type"] = "text/html; charset=utf-8";
|
2012-12-08 12:20:20 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Add basic wrapper tags if the body tag hasn't been given
|
|
|
|
if(stripos($htmlContent, '<body') === false) {
|
|
|
|
$htmlContent =
|
|
|
|
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" .
|
|
|
|
"<HTML><HEAD>\n" .
|
2010-10-19 00:51:54 +02:00
|
|
|
"<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" .
|
|
|
|
"<STYLE type=\"text/css\"></STYLE>\n\n".
|
2008-04-26 08:32:31 +02:00
|
|
|
"</HEAD>\n" .
|
2010-10-19 00:51:54 +02:00
|
|
|
"<BODY bgColor=\"#ffffff\">\n" .
|
2008-04-26 08:32:31 +02:00
|
|
|
$htmlContent .
|
|
|
|
"\n</BODY>\n" .
|
|
|
|
"</HTML>";
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers["Content-Transfer-Encoding"] = "quoted-printable";
|
2012-12-03 00:02:47 +01:00
|
|
|
$htmlPart = $this->processHeaders($headers, wordwrap($this->QuotedPrintable_encode($htmlContent),75));
|
2008-04-26 08:32:31 +02:00
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
list($messageBody, $messageHeaders) = $this->encodeMultipart(
|
|
|
|
array($plainPart,$htmlPart),
|
|
|
|
"multipart/alternative"
|
|
|
|
);
|
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
// Messages with attachments are handled differently
|
|
|
|
if($attachedFiles && is_array($attachedFiles)) {
|
|
|
|
|
|
|
|
// The first part is the message itself
|
2012-12-03 00:02:47 +01:00
|
|
|
$fullMessage = $this->processHeaders($messageHeaders, $messageBody);
|
2008-04-26 08:32:31 +02:00
|
|
|
$messageParts = array($fullMessage);
|
2012-12-03 00:02:47 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
// Include any specified attachments as additional parts
|
|
|
|
foreach($attachedFiles as $file) {
|
2009-04-29 02:07:39 +02:00
|
|
|
if(isset($file['tmp_name']) && isset($file['name'])) {
|
2012-12-03 00:02:47 +01:00
|
|
|
$messageParts[] = $this->encodeFileForEmail($file['tmp_name'], $file['name']);
|
2008-04-26 08:32:31 +02:00
|
|
|
} else {
|
2012-12-03 00:02:47 +01:00
|
|
|
$messageParts[] = $this->encodeFileForEmail($file);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We further wrap all of this into another multipart block
|
2012-12-03 00:02:47 +01:00
|
|
|
list($fullBody, $headers) = $this->encodeMultipart($messageParts, "multipart/mixed");
|
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
// Messages without attachments do not require such treatment
|
|
|
|
} else {
|
|
|
|
$headers = $messageHeaders;
|
|
|
|
$fullBody = $messageBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Email headers
|
2012-12-03 00:02:47 +01:00
|
|
|
$headers["From"] = $this->validEmailAddr($from);
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Messages with the X-SilverStripeMessageID header can be tracked
|
2012-12-08 12:20:20 +01:00
|
|
|
if(isset($customheaders["X-SilverStripeMessageID"]) && defined('BOUNCE_EMAIL')) {
|
|
|
|
$bounceAddress = BOUNCE_EMAIL;
|
|
|
|
} else {
|
|
|
|
$bounceAddress = $from;
|
|
|
|
}
|
2008-10-03 05:57:58 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
// Strip the human name from the bounce address
|
|
|
|
if(preg_match('/^([^<>]*)<([^<>]+)> *$/', $bounceAddress, $parts)) $bounceAddress = $parts[2];
|
2012-02-27 22:14:02 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
// $headers["Sender"] = $from;
|
|
|
|
$headers["X-Mailer"] = X_MAILER;
|
|
|
|
if (!isset($customheaders["X-Priority"])) $headers["X-Priority"] = 3;
|
|
|
|
|
|
|
|
$headers = array_merge((array)$headers, (array)$customheaders);
|
|
|
|
|
|
|
|
// the carbon copy header has to be 'Cc', not 'CC' or 'cc' -- ensure this.
|
|
|
|
if (isset($headers['CC'])) { $headers['Cc'] = $headers['CC']; unset($headers['CC']); }
|
|
|
|
if (isset($headers['cc'])) { $headers['Cc'] = $headers['cc']; unset($headers['cc']); }
|
|
|
|
|
|
|
|
// the carbon copy header has to be 'Bcc', not 'BCC' or 'bcc' -- ensure this.
|
|
|
|
if (isset($headers['BCC'])) {$headers['Bcc']=$headers['BCC']; unset($headers['BCC']); }
|
|
|
|
if (isset($headers['bcc'])) {$headers['Bcc']=$headers['bcc']; unset($headers['bcc']); }
|
2012-12-03 00:02:47 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Send the email
|
2012-12-03 00:02:47 +01:00
|
|
|
$headers = $this->processHeaders($headers);
|
|
|
|
$to = $this->validEmailAddr($to);
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Try it without the -f option if it fails
|
2013-01-17 23:24:23 +01:00
|
|
|
if(!($result = @mail($to, $subject, $fullBody, $headers, escapeshellarg("-f$bounceAddress")))) {
|
2008-04-26 08:32:31 +02:00
|
|
|
$result = mail($to, $subject, $fullBody, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @todo Make visibility protected in 3.2
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
|
|
|
function encodeMultipart($parts, $contentType, $headers = false) {
|
2012-02-27 22:14:02 +01:00
|
|
|
$separator = "----=_NextPart_" . preg_replace('/[^0-9]/', '', rand() * 10000000000);
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
$headers["MIME-Version"] = "1.0";
|
|
|
|
$headers["Content-Type"] = "$contentType; boundary=\"$separator\"";
|
|
|
|
$headers["Content-Transfer-Encoding"] = "7bit";
|
|
|
|
|
|
|
|
if($contentType == "multipart/alternative") {
|
2012-09-26 23:34:00 +02:00
|
|
|
// $baseMessage = "This is an encoded HTML message. There are two parts: a plain text and an HTML message,
|
|
|
|
// open whatever suits you better.";
|
2008-04-26 08:32:31 +02:00
|
|
|
$baseMessage = "\nThis is a multi-part message in MIME format.";
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
// $baseMessage = "This is a message containing attachments. The e-mail body is contained in the first
|
|
|
|
// attachment";
|
2008-04-26 08:32:31 +02:00
|
|
|
$baseMessage = "\nThis is a multi-part message in MIME format.";
|
|
|
|
}
|
2012-12-03 00:02:47 +01:00
|
|
|
|
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
$separator = "\n--$separator\n";
|
|
|
|
$body = "$baseMessage\n" .
|
|
|
|
$separator . implode("\n".$separator, $parts) . "\n" . trim($separator) . "--";
|
2012-12-03 00:02:47 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
return array($body, $headers);
|
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @todo Make visibility protected in 3.2
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
|
|
|
function processHeaders($headers, $body = false) {
|
|
|
|
$res = '';
|
|
|
|
if(is_array($headers)) while(list($k, $v) = each($headers))
|
|
|
|
$res .= "$k: $v\n";
|
|
|
|
if($body) $res .= "\n$body";
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2009-04-29 02:07:39 +02:00
|
|
|
/**
|
2008-04-26 08:32:31 +02:00
|
|
|
* Encode the contents of a file for emailing, including headers
|
2009-04-29 02:07:39 +02:00
|
|
|
*
|
|
|
|
* $file can be an array, in which case it expects these members:
|
|
|
|
* 'filename' - the filename of the file
|
|
|
|
* 'contents' - the raw binary contents of the file as a string
|
|
|
|
* and can optionally include these members:
|
|
|
|
* 'mimetype' - the mimetype of the file (calculated from filename if missing)
|
|
|
|
* 'contentLocation' - the 'Content-Location' header value for the file
|
|
|
|
*
|
|
|
|
* $file can also be a string, in which case it is assumed to be the filename
|
|
|
|
*
|
|
|
|
* h5. contentLocation
|
|
|
|
*
|
2012-12-03 00:02:47 +01:00
|
|
|
* Content Location is one of the two methods allowed for embedding images into an html email.
|
|
|
|
* It's also the simplest, and best supported.
|
2009-04-29 02:07:39 +02:00
|
|
|
*
|
|
|
|
* Assume we have an email with this in the body:
|
|
|
|
*
|
|
|
|
* <img src="http://example.com/image.gif" />
|
|
|
|
*
|
2012-12-03 00:02:47 +01:00
|
|
|
* To display the image, an email viewer would have to download the image from the web every time
|
|
|
|
* it is displayed. Due to privacy issues, most viewers will not display any images unless
|
|
|
|
* the user clicks 'Show images in this email'. Not optimal.
|
2009-04-29 02:07:39 +02:00
|
|
|
*
|
2012-12-03 00:02:47 +01:00
|
|
|
* However, we can also include a copy of this image as an attached file in the email.
|
|
|
|
* By giving it a contentLocation of "http://example.com/image.gif" most email viewers
|
|
|
|
* will use this attached copy instead of downloading it. Better,
|
2012-09-26 23:34:00 +02:00
|
|
|
* most viewers will show it without a 'Show images in this email' conformation.
|
2009-04-29 02:07:39 +02:00
|
|
|
*
|
|
|
|
* Here is an example of passing this information through Email.php:
|
|
|
|
*
|
|
|
|
* $email = new Email();
|
|
|
|
* $email->attachments[] = array(
|
|
|
|
* 'filename' => BASE_PATH . "/themes/mytheme/images/header.gif",
|
|
|
|
* 'contents' => file_get_contents(BASE_PATH . "/themes/mytheme/images/header.gif"),
|
|
|
|
* 'mimetype' => 'image/gif',
|
|
|
|
* 'contentLocation' => Director::absoluteBaseURL() . "/themes/mytheme/images/header.gif"
|
|
|
|
* );
|
|
|
|
*
|
2012-12-03 00:02:47 +01:00
|
|
|
* @todo Make visibility protected in 3.2
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
function encodeFileForEmail($file, $destFileName = false, $disposition = NULL, $extraHeaders = "") {
|
2008-04-26 08:32:31 +02:00
|
|
|
if(!$file) {
|
|
|
|
user_error("encodeFileForEmail: not passed a filename and/or data", E_USER_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($file)) {
|
|
|
|
$file = array('filename' => $file);
|
|
|
|
$fh = fopen($file['filename'], "rb");
|
|
|
|
if ($fh) {
|
2011-04-20 07:37:17 +02:00
|
|
|
$file['contents'] = "";
|
2008-04-26 08:32:31 +02:00
|
|
|
while(!feof($fh)) $file['contents'] .= fread($fh, 10000);
|
|
|
|
fclose($fh);
|
|
|
|
}
|
|
|
|
}
|
2012-12-03 00:02:47 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
// Build headers, including content type
|
|
|
|
if(!$destFileName) $base = basename($file['filename']);
|
|
|
|
else $base = $destFileName;
|
2012-12-03 00:02:47 +01:00
|
|
|
|
2011-04-20 07:37:17 +02:00
|
|
|
$mimeType = !empty($file['mimetype']) ? $file['mimetype'] : HTTP::get_mime_type($file['filename']);
|
2008-04-26 08:32:31 +02:00
|
|
|
if(!$mimeType) $mimeType = "application/unknown";
|
2009-04-29 02:07:39 +02:00
|
|
|
if (empty($disposition)) $disposition = isset($file['contentLocation']) ? 'inline' : 'attachment';
|
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
// Encode for emailing
|
2011-04-20 07:37:17 +02:00
|
|
|
if (substr($mimeType, 0, 4) != 'text') {
|
2008-04-26 08:32:31 +02:00
|
|
|
$encoding = "base64";
|
|
|
|
$file['contents'] = chunk_split(base64_encode($file['contents']));
|
|
|
|
} else {
|
|
|
|
// This mime type is needed, otherwise some clients will show it as an inline attachment
|
|
|
|
$mimeType = 'application/octet-stream';
|
|
|
|
$encoding = "quoted-printable";
|
2012-12-03 00:02:47 +01:00
|
|
|
$file['contents'] = $this->QuotedPrintable_encode($file['contents']);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$headers = "Content-type: $mimeType;\n\tname=\"$base\"\n".
|
|
|
|
"Content-Transfer-Encoding: $encoding\n".
|
|
|
|
"Content-Disposition: $disposition;\n\tfilename=\"$base\"\n";
|
2009-04-29 02:07:39 +02:00
|
|
|
|
|
|
|
if ( isset($file['contentLocation']) ) $headers .= 'Content-Location: ' . $file['contentLocation'] . "\n" ;
|
|
|
|
|
|
|
|
$headers .= $extraHeaders . "\n";
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Return completed packet
|
|
|
|
return $headers . $file['contents'];
|
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @todo Make visibility protected in 3.2
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
function QuotedPrintable_encode($quotprint) {
|
2012-12-03 00:02:47 +01:00
|
|
|
$quotprint = (string)str_replace('\r\n',chr(13).chr(10),$quotprint);
|
|
|
|
$quotprint = (string)str_replace('\n', chr(13).chr(10),$quotprint);
|
2012-12-20 03:35:40 +01:00
|
|
|
$quotprint = (string)preg_replace_callback("~([\x01-\x1F\x3D\x7F-\xFF])~", function($matches) {
|
2012-12-20 01:40:42 +01:00
|
|
|
return sprintf('=%02X', ord($matches[1]));
|
|
|
|
}, $quotprint);
|
2012-12-03 00:02:47 +01:00
|
|
|
//$quotprint = (string)str_replace('\=0D=0A',"=0D=0A",$quotprint);
|
|
|
|
$quotprint = (string)str_replace('=0D=0A',"\n",$quotprint);
|
|
|
|
$quotprint = (string)str_replace('=0A=0D',"\n",$quotprint);
|
|
|
|
$quotprint = (string)str_replace('=0D',"\n",$quotprint);
|
|
|
|
$quotprint = (string)str_replace('=0A',"\n",$quotprint);
|
|
|
|
return (string) $quotprint;
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @todo Make visibility protected in 3.2
|
|
|
|
*/
|
2008-04-26 08:32:31 +02:00
|
|
|
function validEmailAddr($emailAddress) {
|
|
|
|
$emailAddress = trim($emailAddress);
|
|
|
|
$angBrack = strpos($emailAddress, '<');
|
|
|
|
|
|
|
|
if($angBrack === 0) {
|
|
|
|
$emailAddress = substr($emailAddress, 1, strpos($emailAddress,'>')-1);
|
|
|
|
|
|
|
|
} else if($angBrack) {
|
|
|
|
$emailAddress = str_replace('@', '', substr($emailAddress, 0, $angBrack))
|
|
|
|
.substr($emailAddress, $angBrack);
|
2012-12-03 00:02:47 +01:00
|
|
|
}
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
return $emailAddress;
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2013-01-30 12:48:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a multipart/related e-mail chunk for the given HTML message and its linked images
|
|
|
|
* Decodes absolute URLs, accessing the appropriate local images
|
|
|
|
*/
|
|
|
|
function wrapImagesInline($htmlContent) {
|
|
|
|
global $_INLINED_IMAGES;
|
|
|
|
$_INLINED_IMAGES = null;
|
|
|
|
|
|
|
|
$replacedContent = imageRewriter($htmlContent, 'wrapImagesInline_rewriter($URL)');
|
|
|
|
|
|
|
|
|
|
|
|
// Make the HTML part
|
|
|
|
$headers["Content-Type"] = "text/html; charset=utf-8";
|
|
|
|
$headers["Content-Transfer-Encoding"] = "quoted-printable";
|
|
|
|
$multiparts[] = processHeaders($headers, QuotedPrintable_encode($replacedContent));
|
|
|
|
|
|
|
|
// Make all the image parts
|
|
|
|
global $_INLINED_IMAGES;
|
|
|
|
foreach($_INLINED_IMAGES as $url => $cid) {
|
|
|
|
$multiparts[] = encodeFileForEmail($url, false, "inline", "Content-ID: <$cid>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge together in a multipart
|
|
|
|
list($body, $headers) = encodeMultipart($multiparts, "multipart/related");
|
|
|
|
return processHeaders($headers, $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapImagesInline_rewriter($url) {
|
|
|
|
$url = relativiseURL($url);
|
|
|
|
|
|
|
|
global $_INLINED_IMAGES;
|
|
|
|
if(!$_INLINED_IMAGES[$url]) {
|
|
|
|
$identifier = "automatedmessage." . rand(1000,1000000000) . "@silverstripe.com";
|
|
|
|
$_INLINED_IMAGES[$url] = $identifier;
|
|
|
|
}
|
|
|
|
return "cid:" . $_INLINED_IMAGES[$url];
|
|
|
|
|
|
|
|
}
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 3.1
|
|
|
|
*/
|
|
|
|
function htmlEmail($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false,
|
|
|
|
$plainContent = false) {
|
|
|
|
|
|
|
|
Deprecation::notice('3.1', 'Use Email->sendHTML() instead');
|
|
|
|
|
|
|
|
$mailer = Injector::inst()->create('Mailer');
|
|
|
|
return $mailer->sendHTML($to, $from, $subject, $plainContent, $attachedFiles, $customheaders = false);
|
|
|
|
}
|
2008-04-26 08:32:31 +02:00
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 3.1
|
|
|
|
*/
|
|
|
|
function plaintextEmail($to, $from, $subject, $plainContent, $attachedFiles, $customheaders = false) {
|
|
|
|
Deprecation::notice('3.1', 'Use Email->sendPlain() instead');
|
2008-04-26 08:32:31 +02:00
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
$mailer = Injector::inst()->create('Mailer');
|
|
|
|
return $mailer->sendPlain($to, $from, $subject, $plainContent, $attachedFiles, $customheaders = false);
|
|
|
|
}
|
2008-04-26 08:32:31 +02:00
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 3.1
|
|
|
|
*/
|
|
|
|
function encodeMultipart($parts, $contentType, $headers = false) {
|
|
|
|
Deprecation::notice('3.1', 'Use Email->$this->encodeMultipart() instead');
|
2008-04-26 08:32:31 +02:00
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
$mailer = Injector::inst()->create('Mailer');
|
|
|
|
return $mailer->encodeMultipart($parts, $contentType, $headers = false);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 3.1
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
|
|
|
function wrapImagesInline($htmlContent) {
|
2012-12-03 00:02:47 +01:00
|
|
|
Deprecation::notice('3.1', 'Functionality removed from core');
|
2008-04-26 08:32:31 +02:00
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
$mailer = Injector::inst()->create('Mailer');
|
|
|
|
return $mailer->wrapImagesInline($htmlContent);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
2012-12-03 00:02:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 3.1
|
|
|
|
*/
|
2008-04-26 08:32:31 +02:00
|
|
|
function wrapImagesInline_rewriter($url) {
|
2012-12-03 00:02:47 +01:00
|
|
|
Deprecation::notice('3.1', 'Functionality removed from core');
|
|
|
|
|
|
|
|
$mailer = Injector::inst()->create('Mailer');
|
|
|
|
return $mailer->wrapImagesInline_rewriter($url);
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 3.1
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
|
|
|
function processHeaders($headers, $body = false) {
|
2012-12-03 00:02:47 +01:00
|
|
|
Deprecation::notice('3.1', 'Set headers through Email->addCustomHeader()');
|
|
|
|
|
|
|
|
$mailer = Injector::inst()->create('Mailer');
|
|
|
|
return $mailer->processHeaders($headers, $url);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2009-04-29 02:07:39 +02:00
|
|
|
/**
|
2012-12-03 00:02:47 +01:00
|
|
|
* @deprecated 3.1
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
function encodeFileForEmail($file, $destFileName = false, $disposition = NULL, $extraHeaders = "") {
|
2012-12-03 00:02:47 +01:00
|
|
|
Deprecation::notice('3.1', 'Please add files through Email->attachFile()');
|
2008-04-26 08:32:31 +02:00
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
$mailer = Injector::inst()->create('Mailer');
|
|
|
|
return $mailer->encodeFileForEmail($file, $destFileName, $disposition, $extraHeaders);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 3.1
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
function QuotedPrintable_encode($quotprint) {
|
2012-12-03 00:02:47 +01:00
|
|
|
Deprecation::notice('3.1', 'No longer available, handled internally');
|
|
|
|
|
|
|
|
$mailer = Injector::inst()->create('Mailer');
|
|
|
|
return $mailer->QuotedPrintable_encode($quotprint);
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2012-12-03 00:02:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 3.1
|
|
|
|
*/
|
2008-04-26 08:32:31 +02:00
|
|
|
function validEmailAddr($emailAddress) {
|
2012-12-03 00:02:47 +01:00
|
|
|
Deprecation::notice('3.1', 'Use Email->validEmailAddr() instead');
|
|
|
|
|
|
|
|
$mailer = Injector::inst()->create('Mailer');
|
|
|
|
return $mailer->validEmailAddr($emailAddress);
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|