Fixed indentation in Mailer

No functional changes
This commit is contained in:
Ingo Schommer 2013-01-30 12:52:18 +01:00
parent 7327f120aa
commit 7f4c173369

View File

@ -27,7 +27,7 @@ class Mailer {
if ($customheaders && is_array($customheaders) == false) { if ($customheaders && is_array($customheaders) == false) {
echo "htmlEmail($to, $from, $subject, ...) could not send mail: improper \$customheaders passed:<BR>"; echo "htmlEmail($to, $from, $subject, ...) could not send mail: improper \$customheaders passed:<BR>";
dieprintr($customheaders); dieprintr($customheaders);
} }
// If the subject line contains extended characters, we must encode it // If the subject line contains extended characters, we must encode it
$subject = Convert::xml2raw($subject); $subject = Convert::xml2raw($subject);
@ -53,9 +53,8 @@ class Mailer {
$messageParts[] = $this->encodeFileForEmail($file['tmp_name'], $file['name']); $messageParts[] = $this->encodeFileForEmail($file['tmp_name'], $file['name']);
} else { } else {
$messageParts[] = $this->encodeFileForEmail($file); $messageParts[] = $this->encodeFileForEmail($file);
} }
} }
// We further wrap all of this into another multipart block // We further wrap all of this into another multipart block
list($fullBody, $headers) = $this->encodeMultipart($messageParts, "multipart/mixed"); list($fullBody, $headers) = $this->encodeMultipart($messageParts, "multipart/mixed");
@ -105,63 +104,60 @@ class Mailer {
} }
/** /**
* Sends an email as a both HTML and plaintext * Sends an email as a both HTML and plaintext
* *
* $attachedFiles should be an array of file names * $attachedFiles should be an array of file names
* - if you pass the entire $_FILES entry, the user-uploaded filename will be preserved * - if you pass the entire $_FILES entry, the user-uploaded filename will be preserved
* use $plainContent to override default plain-content generation * use $plainContent to override default plain-content generation
* *
* @return bool * @return bool
*/ */
public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false, public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false,
$plainContent = false) { $plainContent = false) {
if ($customheaders && is_array($customheaders) == false) { if ($customheaders && is_array($customheaders) == false) {
echo "htmlEmail($to, $from, $subject, ...) could not send mail: improper \$customheaders passed:<BR>"; echo "htmlEmail($to, $from, $subject, ...) could not send mail: improper \$customheaders passed:<BR>";
dieprintr($customheaders); dieprintr($customheaders);
} }
$bodyIsUnicode = (strpos($htmlContent,"&#") !== false);
$plainEncoding = "";
$bodyIsUnicode = (strpos($htmlContent,"&#") !== false); // We generate plaintext content by default, but you can pass custom stuff
$plainEncoding = ""; $plainEncoding = '';
if(!$plainContent) {
$plainContent = Convert::xml2raw($htmlContent);
if(isset($bodyIsUnicode) && $bodyIsUnicode) $plainEncoding = "base64";
}
// We generate plaintext content by default, but you can pass custom stuff // If the subject line contains extended characters, we must encode the
$plainEncoding = ''; $subject = Convert::xml2raw($subject);
if(!$plainContent) { $subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
$plainContent = Convert::xml2raw($htmlContent);
if(isset($bodyIsUnicode) && $bodyIsUnicode) $plainEncoding = "base64";
}
// Make the plain text part
// If the subject line contains extended characters, we must encode the $headers["Content-Type"] = "text/plain; charset=utf-8";
$subject = Convert::xml2raw($subject); $headers["Content-Transfer-Encoding"] = $plainEncoding ? $plainEncoding : "quoted-printable";
$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";
$plainPart = $this->processHeaders($headers, ($plainEncoding == "base64") $plainPart = $this->processHeaders($headers, ($plainEncoding == "base64")
? chunk_split(base64_encode($plainContent),60) ? chunk_split(base64_encode($plainContent),60)
: wordwrap($this->QuotedPrintable_encode($plainContent),75)); : wordwrap($this->QuotedPrintable_encode($plainContent),75));
// Make the HTML part // Make the HTML part
$headers["Content-Type"] = "text/html; charset=utf-8"; $headers["Content-Type"] = "text/html; charset=utf-8";
// Add basic wrapper tags if the body tag hasn't been given
// Add basic wrapper tags if the body tag hasn't been given if(stripos($htmlContent, '<body') === false) {
if(stripos($htmlContent, '<body') === false) { $htmlContent =
$htmlContent = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" .
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" . "<HTML><HEAD>\n" .
"<HTML><HEAD>\n" . "<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" .
"<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" . "<STYLE type=\"text/css\"></STYLE>\n\n".
"<STYLE type=\"text/css\"></STYLE>\n\n". "</HEAD>\n" .
"</HEAD>\n" . "<BODY bgColor=\"#ffffff\">\n" .
"<BODY bgColor=\"#ffffff\">\n" . $htmlContent .
$htmlContent . "\n</BODY>\n" .
"\n</BODY>\n" . "</HTML>";
"</HTML>"; }
}
$headers["Content-Transfer-Encoding"] = "quoted-printable"; $headers["Content-Transfer-Encoding"] = "quoted-printable";
$htmlPart = $this->processHeaders($headers, wordwrap($this->QuotedPrintable_encode($htmlContent),75)); $htmlPart = $this->processHeaders($headers, wordwrap($this->QuotedPrintable_encode($htmlContent),75));
@ -171,203 +167,205 @@ class Mailer {
"multipart/alternative" "multipart/alternative"
); );
// Messages with attachments are handled differently // Messages with attachments are handled differently
if($attachedFiles && is_array($attachedFiles)) { if($attachedFiles && is_array($attachedFiles)) {
// The first part is the message itself // The first part is the message itself
$fullMessage = $this->processHeaders($messageHeaders, $messageBody); $fullMessage = $this->processHeaders($messageHeaders, $messageBody);
$messageParts = array($fullMessage); $messageParts = array($fullMessage);
// Include any specified attachments as additional parts // Include any specified attachments as additional parts
foreach($attachedFiles as $file) { foreach($attachedFiles as $file) {
if(isset($file['tmp_name']) && isset($file['name'])) { if(isset($file['tmp_name']) && isset($file['name'])) {
$messageParts[] = $this->encodeFileForEmail($file['tmp_name'], $file['name']); $messageParts[] = $this->encodeFileForEmail($file['tmp_name'], $file['name']);
} else { } else {
$messageParts[] = $this->encodeFileForEmail($file); $messageParts[] = $this->encodeFileForEmail($file);
}
} }
// 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 {
$headers = $messageHeaders;
$fullBody = $messageBody;
} }
// We further wrap all of this into another multipart block // Email headers
list($fullBody, $headers) = $this->encodeMultipart($messageParts, "multipart/mixed");
// Messages without attachments do not require such treatment
} else {
$headers = $messageHeaders;
$fullBody = $messageBody;
}
// Email headers
$headers["From"] = $this->validEmailAddr($from); $headers["From"] = $this->validEmailAddr($from);
// Messages with the X-SilverStripeMessageID header can be tracked // Messages with the X-SilverStripeMessageID header can be tracked
if(isset($customheaders["X-SilverStripeMessageID"]) && defined('BOUNCE_EMAIL')) { if(isset($customheaders["X-SilverStripeMessageID"]) && defined('BOUNCE_EMAIL')) {
$bounceAddress = BOUNCE_EMAIL; $bounceAddress = BOUNCE_EMAIL;
} else { } else {
$bounceAddress = $from; $bounceAddress = $from;
} }
// Strip the human name from the bounce address // Strip the human name from the bounce address
if(preg_match('/^([^<>]*)<([^<>]+)> *$/', $bounceAddress, $parts)) $bounceAddress = $parts[2]; if(preg_match('/^([^<>]*)<([^<>]+)> *$/', $bounceAddress, $parts)) $bounceAddress = $parts[2];
// $headers["Sender"] = $from; // $headers["Sender"] = $from;
$headers["X-Mailer"] = X_MAILER; $headers["X-Mailer"] = X_MAILER;
if (!isset($customheaders["X-Priority"])) $headers["X-Priority"] = 3; if (!isset($customheaders["X-Priority"])) $headers["X-Priority"] = 3;
$headers = array_merge((array)$headers, (array)$customheaders); $headers = array_merge((array)$headers, (array)$customheaders);
// the carbon copy header has to be 'Cc', not 'CC' or 'cc' -- ensure this. // 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']); }
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. // 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']); }
if (isset($headers['bcc'])) {$headers['Bcc']=$headers['bcc']; unset($headers['bcc']); } if (isset($headers['bcc'])) {$headers['Bcc']=$headers['bcc']; unset($headers['bcc']); }
// Send the email
// Send the email
$headers = $this->processHeaders($headers); $headers = $this->processHeaders($headers);
$to = $this->validEmailAddr($to); $to = $this->validEmailAddr($to);
// Try it without the -f option if it fails // Try it without the -f option if it fails
if(!($result = @mail($to, $subject, $fullBody, $headers, escapeshellarg("-f$bounceAddress")))) { if(!($result = @mail($to, $subject, $fullBody, $headers, escapeshellarg("-f$bounceAddress")))) {
$result = mail($to, $subject, $fullBody, $headers); $result = mail($to, $subject, $fullBody, $headers);
}
return $result;
} }
return $result; /**
} * @todo Make visibility protected in 3.2
*/
function encodeMultipart($parts, $contentType, $headers = false) {
$separator = "----=_NextPart_" . preg_replace('/[^0-9]/', '', rand() * 10000000000);
/** $headers["MIME-Version"] = "1.0";
* @todo Make visibility protected in 3.2 $headers["Content-Type"] = "$contentType; boundary=\"$separator\"";
*/ $headers["Content-Transfer-Encoding"] = "7bit";
function encodeMultipart($parts, $contentType, $headers = false) {
$separator = "----=_NextPart_" . preg_replace('/[^0-9]/', '', rand() * 10000000000);
$headers["MIME-Version"] = "1.0"; if($contentType == "multipart/alternative") {
$headers["Content-Type"] = "$contentType; boundary=\"$separator\""; // $baseMessage = "This is an encoded HTML message. There are two parts: a plain text and an HTML message,
$headers["Content-Transfer-Encoding"] = "7bit"; // open whatever suits you better.";
$baseMessage = "\nThis is a multi-part message in MIME format.";
} else {
// $baseMessage = "This is a message containing attachments. The e-mail body is contained in the first
// attachment";
$baseMessage = "\nThis is a multi-part message in MIME format.";
}
if($contentType == "multipart/alternative") { $separator = "\n--$separator\n";
// $baseMessage = "This is an encoded HTML message. There are two parts: a plain text and an HTML message, $body = "$baseMessage\n" .
// open whatever suits you better."; $separator . implode("\n".$separator, $parts) . "\n" . trim($separator) . "--";
$baseMessage = "\nThis is a multi-part message in MIME format.";
} else { return array($body, $headers);
// $baseMessage = "This is a message containing attachments. The e-mail body is contained in the first
// attachment";
$baseMessage = "\nThis is a multi-part message in MIME format.";
} }
/**
* @todo Make visibility protected in 3.2
*/
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";
$separator = "\n--$separator\n"; return $res;
$body = "$baseMessage\n" . }
$separator . implode("\n".$separator, $parts) . "\n" . trim($separator) . "--";
return array($body, $headers);
}
/** /**
* @todo Make visibility protected in 3.2 * Encode the contents of a file for emailing, including headers
*/ *
function processHeaders($headers, $body = false) { * $file can be an array, in which case it expects these members:
$res = ''; * 'filename' - the filename of the file
if(is_array($headers)) while(list($k, $v) = each($headers)) * 'contents' - the raw binary contents of the file as a string
$res .= "$k: $v\n"; * and can optionally include these members:
if($body) $res .= "\n$body"; * 'mimetype' - the mimetype of the file (calculated from filename if missing)
return $res; * '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
/** *
* Encode the contents of a file for emailing, including headers * h5. contentLocation
* *
* $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
*
* Content Location is one of the two methods allowed for embedding images into an html email. * Content Location is one of the two methods allowed for embedding images into an html email.
* It's also the simplest, and best supported. * It's also the simplest, and best supported.
* *
* Assume we have an email with this in the body: * Assume we have an email with this in the body:
* *
* <img src="http://example.com/image.gif" /> * <img src="http://example.com/image.gif" />
* *
* To display the image, an email viewer would have to download the image from the web every time * 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 * 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. * the user clicks 'Show images in this email'. Not optimal.
* *
* However, we can also include a copy of this image as an attached file in the email. * 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 * By giving it a contentLocation of "http://example.com/image.gif" most email viewers
* will use this attached copy instead of downloading it. Better, * will use this attached copy instead of downloading it. Better,
* most viewers will show it without a 'Show images in this email' conformation. * most viewers will show it without a 'Show images in this email' conformation.
* *
* Here is an example of passing this information through Email.php: * Here is an example of passing this information through Email.php:
* *
* $email = new Email(); * $email = new Email();
* $email->attachments[] = array( * $email->attachments[] = array(
* 'filename' => BASE_PATH . "/themes/mytheme/images/header.gif", * 'filename' => BASE_PATH . "/themes/mytheme/images/header.gif",
* 'contents' => file_get_contents(BASE_PATH . "/themes/mytheme/images/header.gif"), * 'contents' => file_get_contents(BASE_PATH . "/themes/mytheme/images/header.gif"),
* 'mimetype' => 'image/gif', * 'mimetype' => 'image/gif',
* 'contentLocation' => Director::absoluteBaseURL() . "/themes/mytheme/images/header.gif" * 'contentLocation' => Director::absoluteBaseURL() . "/themes/mytheme/images/header.gif"
* ); * );
* *
* @todo Make visibility protected in 3.2 * @todo Make visibility protected in 3.2
*/ */
function encodeFileForEmail($file, $destFileName = false, $disposition = NULL, $extraHeaders = "") { function encodeFileForEmail($file, $destFileName = false, $disposition = NULL, $extraHeaders = "") {
if(!$file) { if(!$file) {
user_error("encodeFileForEmail: not passed a filename and/or data", E_USER_WARNING); user_error("encodeFileForEmail: not passed a filename and/or data", E_USER_WARNING);
return; return;
}
if (is_string($file)) {
$file = array('filename' => $file);
$fh = fopen($file['filename'], "rb");
if ($fh) {
$file['contents'] = "";
while(!feof($fh)) $file['contents'] .= fread($fh, 10000);
fclose($fh);
} }
if (is_string($file)) {
$file = array('filename' => $file);
$fh = fopen($file['filename'], "rb");
if ($fh) {
$file['contents'] = "";
while(!feof($fh)) $file['contents'] .= fread($fh, 10000);
fclose($fh);
}
}
// Build headers, including content type
if(!$destFileName) $base = basename($file['filename']);
else $base = $destFileName;
$mimeType = !empty($file['mimetype']) ? $file['mimetype'] : HTTP::get_mime_type($file['filename']);
if(!$mimeType) $mimeType = "application/unknown";
if (empty($disposition)) $disposition = isset($file['contentLocation']) ? 'inline' : 'attachment';
// Encode for emailing
if (substr($mimeType, 0, 4) != 'text') {
$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";
$file['contents'] = $this->QuotedPrintable_encode($file['contents']);
}
$headers = "Content-type: $mimeType;\n\tname=\"$base\"\n".
"Content-Transfer-Encoding: $encoding\n".
"Content-Disposition: $disposition;\n\tfilename=\"$base\"\n";
if ( isset($file['contentLocation']) ) $headers .= 'Content-Location: ' . $file['contentLocation'] . "\n" ;
$headers .= $extraHeaders . "\n";
// Return completed packet
return $headers . $file['contents'];
} }
// Build headers, including content type
if(!$destFileName) $base = basename($file['filename']);
else $base = $destFileName;
$mimeType = !empty($file['mimetype']) ? $file['mimetype'] : HTTP::get_mime_type($file['filename']);
if(!$mimeType) $mimeType = "application/unknown";
if (empty($disposition)) $disposition = isset($file['contentLocation']) ? 'inline' : 'attachment';
// Encode for emailing
if (substr($mimeType, 0, 4) != 'text') {
$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";
$file['contents'] = $this->QuotedPrintable_encode($file['contents']);
}
$headers = "Content-type: $mimeType;\n\tname=\"$base\"\n".
"Content-Transfer-Encoding: $encoding\n".
"Content-Disposition: $disposition;\n\tfilename=\"$base\"\n";
if ( isset($file['contentLocation']) ) $headers .= 'Content-Location: ' . $file['contentLocation'] . "\n" ;
$headers .= $extraHeaders . "\n";
// Return completed packet
return $headers . $file['contents'];
}
/** /**
* @todo Make visibility protected in 3.2 * @todo Make visibility protected in 3.2
*/ */
function QuotedPrintable_encode($quotprint) { function QuotedPrintable_encode($quotprint) {
$quotprint = (string)str_replace('\r\n',chr(13).chr(10),$quotprint); $quotprint = (string)str_replace('\r\n',chr(13).chr(10),$quotprint);
$quotprint = (string)str_replace('\n', chr(13).chr(10),$quotprint); $quotprint = (string)str_replace('\n', chr(13).chr(10),$quotprint);
$quotprint = (string)preg_replace_callback("~([\x01-\x1F\x3D\x7F-\xFF])~", function($matches) { $quotprint = (string)preg_replace_callback("~([\x01-\x1F\x3D\x7F-\xFF])~", function($matches) {
@ -379,26 +377,26 @@ function QuotedPrintable_encode($quotprint) {
$quotprint = (string)str_replace('=0D',"\n",$quotprint); $quotprint = (string)str_replace('=0D',"\n",$quotprint);
$quotprint = (string)str_replace('=0A',"\n",$quotprint); $quotprint = (string)str_replace('=0A',"\n",$quotprint);
return (string) $quotprint; return (string) $quotprint;
} }
/** /**
* @todo Make visibility protected in 3.2 * @todo Make visibility protected in 3.2
*/ */
function validEmailAddr($emailAddress) { function validEmailAddr($emailAddress) {
$emailAddress = trim($emailAddress); $emailAddress = trim($emailAddress);
$angBrack = strpos($emailAddress, '<'); $angBrack = strpos($emailAddress, '<');
if($angBrack === 0) { if($angBrack === 0) {
$emailAddress = substr($emailAddress, 1, strpos($emailAddress,'>')-1); $emailAddress = substr($emailAddress, 1, strpos($emailAddress,'>')-1);
} else if($angBrack) { } else if($angBrack) {
$emailAddress = str_replace('@', '', substr($emailAddress, 0, $angBrack)) $emailAddress = str_replace('@', '', substr($emailAddress, 0, $angBrack))
.substr($emailAddress, $angBrack); . substr($emailAddress, $angBrack);
}
return $emailAddress;
} }
return $emailAddress;
}
/* /*
* Return a multipart/related e-mail chunk for the given HTML message and its linked images * Return a multipart/related e-mail chunk for the given HTML message and its linked images
* Decodes absolute URLs, accessing the appropriate local images * Decodes absolute URLs, accessing the appropriate local images
@ -409,7 +407,6 @@ function validEmailAddr($emailAddress) {
$replacedContent = imageRewriter($htmlContent, 'wrapImagesInline_rewriter($URL)'); $replacedContent = imageRewriter($htmlContent, 'wrapImagesInline_rewriter($URL)');
// Make the HTML part // Make the HTML part
$headers["Content-Type"] = "text/html; charset=utf-8"; $headers["Content-Type"] = "text/html; charset=utf-8";
$headers["Content-Transfer-Encoding"] = "quoted-printable"; $headers["Content-Transfer-Encoding"] = "quoted-printable";