2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage email
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
if(isset($_SERVER['SERVER_NAME'])) {
|
|
|
|
/**
|
|
|
|
* X-Mailer header value on emails sent
|
|
|
|
*/
|
|
|
|
define('X_MAILER', 'SilverStripe Mailer - version 2006.06.21 (Sent from "'.$_SERVER['SERVER_NAME'].'")');
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
define('X_MAILER', 'SilverStripe Mailer - version 2006.06.21');
|
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
// Note: The constant 'BOUNCE_EMAIL' should be defined as a valid email address for where bounces should be returned
|
|
|
|
// to.
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Class to support sending emails.
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage email
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Email extends ViewableData {
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* @param string $from Email-Address
|
|
|
|
*/
|
|
|
|
protected $from;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $to Email-Address. Use comma-separation to pass multiple email-addresses.
|
|
|
|
*/
|
|
|
|
protected $to;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $subject Subject of the email
|
|
|
|
*/
|
|
|
|
protected $subject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $body HTML content of the email.
|
|
|
|
* Passed straight into {@link $ss_template} as $Body variable.
|
|
|
|
*/
|
|
|
|
protected $body;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $plaintext_body Optional string for plaintext emails.
|
|
|
|
* If not set, defaults to converting the HTML-body with {@link Convert::xml2raw()}.
|
|
|
|
*/
|
|
|
|
protected $plaintext_body;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $cc
|
|
|
|
*/
|
|
|
|
protected $cc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $bcc
|
|
|
|
*/
|
|
|
|
protected $bcc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Mailer $mailer Instance of a {@link Mailer} class.
|
|
|
|
*/
|
2008-04-26 08:32:31 +02:00
|
|
|
protected static $mailer;
|
|
|
|
|
|
|
|
/**
|
2008-12-04 23:38:32 +01:00
|
|
|
* This can be used to provide a mailer class other than the default, e.g. for testing.
|
|
|
|
*
|
|
|
|
* @param Mailer $mailer
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_mailer(Mailer $mailer) {
|
2008-04-26 08:32:31 +02:00
|
|
|
self::$mailer = $mailer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mailer.
|
2008-12-04 23:38:32 +01:00
|
|
|
*
|
|
|
|
* @return Mailer
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function mailer() {
|
2008-04-26 08:32:31 +02:00
|
|
|
if(!self::$mailer) self::$mailer = new Mailer();
|
|
|
|
return self::$mailer;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-12-04 23:38:32 +01:00
|
|
|
* @param array $customHeaders A map of header-name -> header-value
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-25 22:56:25 +01:00
|
|
|
protected $customHeaders = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* @param array $attachements Internal, use {@link attachFileFromString()} or {@link attachFile()}
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $attachments = array();
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param boolean $
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $parseVariables_done = false;
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* @param string $ss_template The name of the used template (without *.ss extension)
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $ss_template = "GenericEmail";
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $template_data Additional data available in a template.
|
|
|
|
* Used in the same way than {@link ViewableData->customize()}.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $template_data = null;
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $bounceHandlerURL
|
|
|
|
*/
|
2009-03-25 22:27:01 +01:00
|
|
|
protected $bounceHandlerURL = null;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-03-25 22:27:01 +01:00
|
|
|
/**
|
2008-12-04 23:38:32 +01:00
|
|
|
* @param sring $admin_email_address The default administrator email address.
|
|
|
|
* This will be set in the config on a site-by-site basis
|
2009-03-25 22:27:01 +01:00
|
|
|
*/
|
|
|
|
static $admin_email_address = '';
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $send_all_emails_to Email-Address
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected static $send_all_emails_to = null;
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $bcc_all_emails_to Email-Address
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected static $bcc_all_emails_to = null;
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $cc_all_emails_to Email-Address
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected static $cc_all_emails_to = null;
|
2009-03-17 10:12:54 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new email.
|
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public function __construct($from = null, $to = null, $subject = null, $body = null, $bounceHandlerURL = null,
|
|
|
|
$cc = null, $bcc = null) {
|
|
|
|
|
2009-05-20 01:23:45 +02:00
|
|
|
if($from != null) $this->from = $from;
|
|
|
|
if($to != null) $this->to = $to;
|
|
|
|
if($subject != null) $this->subject = $subject;
|
|
|
|
if($body != null) $this->body = $body;
|
|
|
|
if($cc != null) $this->cc = $cc;
|
|
|
|
if($bcc != null) $this->bcc = $bcc;
|
|
|
|
if($bounceHandlerURL != null) $this->setBounceHandlerURL($bounceHandlerURL);
|
2009-09-18 05:02:19 +02:00
|
|
|
parent::__construct();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-06-12 11:29:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function attachFileFromString($data, $filename, $mimetype = null) {
|
|
|
|
$this->attachments[] = array(
|
|
|
|
'contents' => $data,
|
|
|
|
'filename' => $filename,
|
|
|
|
'mimetype' => $mimetype,
|
|
|
|
);
|
|
|
|
}
|
2009-05-01 05:49:34 +02:00
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
public function setBounceHandlerURL( $bounceHandlerURL ) {
|
2009-03-25 22:27:01 +01:00
|
|
|
if($bounceHandlerURL) {
|
2009-03-17 10:12:54 +01:00
|
|
|
$this->bounceHandlerURL = $bounceHandlerURL;
|
2009-03-25 22:27:01 +01:00
|
|
|
} else {
|
|
|
|
$this->bounceHandlerURL = $_SERVER['HTTP_HOST'] . Director::baseURL() . 'Email_BounceHandler';
|
|
|
|
}
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
2009-05-01 05:49:34 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function attachFile($filename, $attachedFilename = null, $mimetype = null) {
|
2011-03-16 10:31:00 +01:00
|
|
|
if(!$attachedFilename) $attachedFilename = basename($filename);
|
2008-10-08 04:00:12 +02:00
|
|
|
$absoluteFileName = Director::getAbsFile($filename);
|
|
|
|
if(file_exists($absoluteFileName)) {
|
|
|
|
$this->attachFileFromString(file_get_contents($absoluteFileName), $attachedFilename, $mimetype);
|
|
|
|
} else {
|
|
|
|
user_error("Could not attach '$absoluteFileName' to email. File does not exist.", E_USER_NOTICE);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function Subject() {
|
|
|
|
return $this->subject;
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Body() {
|
|
|
|
return $this->body;
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function To() {
|
|
|
|
return $this->to;
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function From() {
|
|
|
|
return $this->from;
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Cc() {
|
|
|
|
return $this->cc;
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Bcc() {
|
|
|
|
return $this->bcc;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setSubject($val) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$this->subject = $val;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setBody($val) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$this->body = $val;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setTo($val) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$this->to = $val;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setFrom($val) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$this->from = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setCc($val) {
|
|
|
|
$this->cc = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setBcc($val) {
|
|
|
|
$this->bcc = $val;
|
|
|
|
}
|
|
|
|
|
2009-03-25 23:02:30 +01:00
|
|
|
/**
|
|
|
|
* Set the "Reply-To" header with an email address.
|
|
|
|
* @param string $email The email address of the "Reply-To" header
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function replyTo($email) {
|
2009-03-25 23:02:30 +01:00
|
|
|
$this->addCustomHeader('Reply-To', $email);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Add a custom header to this value.
|
|
|
|
* Useful for implementing all those cool features that we didn't think of.
|
2009-03-17 21:20:08 +01:00
|
|
|
*
|
|
|
|
* @param string $headerName
|
|
|
|
* @param string $headerValue
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function addCustomHeader($headerName, $headerValue) {
|
|
|
|
if($headerName == 'Cc') $this->cc = $headerValue;
|
|
|
|
else if($headerName == 'Bcc') $this->bcc = $headerValue;
|
|
|
|
else {
|
2009-03-04 04:44:11 +01:00
|
|
|
if(isset($this->customHeaders[$headerName])) $this->customHeaders[$headerName] .= ", " . $headerValue;
|
|
|
|
else $this->customHeaders[$headerName] = $headerValue;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-06-12 11:29:05 +02:00
|
|
|
|
2008-04-09 13:25:06 +02:00
|
|
|
public function BaseURL() {
|
|
|
|
return Director::absoluteBaseURL();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-06-12 11:29:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Debugging help
|
|
|
|
*/
|
|
|
|
public function debug() {
|
|
|
|
$this->parseVariables();
|
|
|
|
|
|
|
|
return "<h2>Email template $this->class</h2>\n" .
|
|
|
|
"<p><b>From:</b> $this->from\n" .
|
|
|
|
"<b>To:</b> $this->to\n" .
|
|
|
|
"<b>Cc:</b> $this->cc\n" .
|
|
|
|
"<b>Bcc:</b> $this->bcc\n" .
|
|
|
|
"<b>Subject:</b> $this->subject</p>" .
|
|
|
|
$this->body;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* Set template name (without *.ss extension).
|
|
|
|
*
|
|
|
|
* @param string $template
|
|
|
|
*/
|
|
|
|
public function setTemplate($template) {
|
|
|
|
$this->ss_template = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTemplate() {
|
|
|
|
return $this->ss_template;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected function templateData() {
|
|
|
|
if($this->template_data) {
|
|
|
|
return $this->template_data->customise(array(
|
|
|
|
"To" => $this->to,
|
|
|
|
"Cc" => $this->cc,
|
|
|
|
"Bcc" => $this->bcc,
|
|
|
|
"From" => $this->from,
|
|
|
|
"Subject" => $this->subject,
|
|
|
|
"Body" => $this->body,
|
2008-02-25 03:10:37 +01:00
|
|
|
"BaseURL" => $this->BaseURL(),
|
|
|
|
"IsEmail" => true,
|
2007-07-19 12:40:28 +02:00
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2008-12-04 23:38:32 +01:00
|
|
|
* Used by {@link SSViewer} templates to detect if we're rendering an email template rather than a page template
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
|
|
|
public function IsEmail() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-09 13:25:06 +02:00
|
|
|
/**
|
|
|
|
* Populate this email template with values.
|
|
|
|
* This may be called many times.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function populateTemplate($data) {
|
2009-03-17 10:12:54 +01:00
|
|
|
if($this->template_data) {
|
2008-04-09 13:25:06 +02:00
|
|
|
$this->template_data = $this->template_data->customise($data);
|
|
|
|
} else {
|
|
|
|
if(is_array($data)) $data = new ArrayData($data);
|
|
|
|
$this->template_data = $this->customise($data);
|
|
|
|
}
|
|
|
|
$this->parseVariables_done = false;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Load all the template variables into the internal variables, including
|
2009-03-17 10:12:54 +01:00
|
|
|
* the template into body. Called before send() or debugSend()
|
2008-03-12 05:19:39 +01:00
|
|
|
* $isPlain=true will cause the template to be ignored, otherwise the GenericEmail template will be used
|
|
|
|
* and it won't be plain email :)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-03-12 05:19:39 +01:00
|
|
|
protected function parseVariables($isPlain = false) {
|
2009-05-06 08:36:16 +02:00
|
|
|
SSViewer::set_source_file_comments(false);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->parseVariables_done) {
|
|
|
|
$this->parseVariables_done = true;
|
|
|
|
|
|
|
|
// Parse $ variables in the base parameters
|
|
|
|
$data = $this->templateData();
|
|
|
|
|
|
|
|
// Process a .SS template file
|
|
|
|
$fullBody = $this->body;
|
2008-03-12 05:19:39 +01:00
|
|
|
if($this->ss_template && !$isPlain) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Requery data so that updated versions of To, From, Subject, etc are included
|
|
|
|
$data = $this->templateData();
|
|
|
|
|
|
|
|
$template = new SSViewer($this->ss_template);
|
|
|
|
|
|
|
|
if($template->exists()) {
|
|
|
|
$fullBody = $template->process($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rewrite relative URLs
|
|
|
|
$this->body = HTTP::absoluteURLs($fullBody);
|
|
|
|
}
|
|
|
|
}
|
2008-04-09 13:25:06 +02:00
|
|
|
|
|
|
|
/**
|
2012-12-03 00:02:47 +01:00
|
|
|
* Validates the email address. Returns true of false
|
2009-03-17 10:12:54 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function validEmailAddress($address) {
|
2012-02-27 22:14:02 +01:00
|
|
|
if (function_exists('filter_var')) {
|
|
|
|
return filter_var($address, FILTER_VALIDATE_EMAIL);
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
return preg_match('#^([a-zA-Z0-9_+\.\-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)'
|
|
|
|
. '|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$#', $address);
|
2012-02-27 22:14:02 +01:00
|
|
|
}
|
2008-04-09 13:25:06 +02:00
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
/**
|
|
|
|
* Send the email in plaintext.
|
|
|
|
*
|
|
|
|
* @see send() for sending emails with HTML content.
|
|
|
|
* @uses Mailer->sendPlain()
|
|
|
|
*
|
|
|
|
* @param string $messageID Optional message ID so the message can be identified in bounces etc.
|
|
|
|
* @return bool Success of the sending operation from an MTA perspective.
|
|
|
|
* Doesn't actually give any indication if the mail has been delivered to the recipient properly)
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function sendPlain($messageID = null) {
|
2009-03-17 10:12:54 +01:00
|
|
|
Requirements::clear();
|
|
|
|
|
|
|
|
$this->parseVariables(true);
|
|
|
|
|
|
|
|
if(empty($this->from)) $this->from = Email::getAdminEmail();
|
|
|
|
|
|
|
|
$this->setBounceHandlerURL($this->bounceHandlerURL);
|
2009-03-25 22:56:25 +01:00
|
|
|
|
|
|
|
$headers = $this->customHeaders;
|
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
$headers['X-SilverStripeBounceURL'] = $this->bounceHandlerURL;
|
|
|
|
|
2009-03-25 22:46:48 +01:00
|
|
|
if($messageID) $headers['X-SilverStripeMessageID'] = project() . '.' . $messageID;
|
2009-03-17 10:12:54 +01:00
|
|
|
|
2009-03-25 22:46:48 +01:00
|
|
|
if(project()) $headers['X-SilverStripeSite'] = project();
|
2009-03-17 10:12:54 +01:00
|
|
|
|
|
|
|
$to = $this->to;
|
|
|
|
$subject = $this->subject;
|
|
|
|
if(self::$send_all_emails_to) {
|
|
|
|
$subject .= " [addressed to $to";
|
|
|
|
$to = self::$send_all_emails_to;
|
2009-03-25 22:27:01 +01:00
|
|
|
if($this->cc) $subject .= ", cc to $this->cc";
|
|
|
|
if($this->bcc) $subject .= ", bcc to $this->bcc";
|
2009-03-17 10:12:54 +01:00
|
|
|
$subject .= ']';
|
|
|
|
} else {
|
2009-03-25 22:43:01 +01:00
|
|
|
if($this->cc) $headers['Cc'] = $this->cc;
|
|
|
|
if($this->bcc) $headers['Bcc'] = $this->bcc;
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(self::$cc_all_emails_to) {
|
2009-03-25 22:43:01 +01:00
|
|
|
if(!empty($headers['Cc']) && trim($headers['Cc'])) {
|
|
|
|
$headers['Cc'] .= ', ' . self::$cc_all_emails_to;
|
|
|
|
} else {
|
|
|
|
$headers['Cc'] = self::$cc_all_emails_to;
|
|
|
|
}
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
2009-03-25 22:27:01 +01:00
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
if(self::$bcc_all_emails_to) {
|
2009-03-25 22:43:01 +01:00
|
|
|
if(!empty($headers['Bcc']) && trim($headers['Bcc'])) {
|
|
|
|
$headers['Bcc'] .= ', ' . self::$bcc_all_emails_to;
|
|
|
|
} else {
|
|
|
|
$headers['Bcc'] = self::$bcc_all_emails_to;
|
|
|
|
}
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Requirements::restore();
|
|
|
|
|
|
|
|
return self::mailer()->sendPlain($to, $this->from, $subject, $this->body, $this->attachments, $headers);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2009-03-17 10:07:33 +01:00
|
|
|
* Send an email with HTML content.
|
|
|
|
*
|
|
|
|
* @see sendPlain() for sending plaintext emails only.
|
|
|
|
* @uses Mailer->sendHTML()
|
|
|
|
*
|
|
|
|
* @param string $messageID Optional message ID so the message can be identified in bounces etc.
|
|
|
|
* @return bool Success of the sending operation from an MTA perspective.
|
|
|
|
* Doesn't actually give any indication if the mail has been delivered to the recipient properly)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-25 22:56:25 +01:00
|
|
|
public function send($messageID = null) {
|
2009-03-17 10:12:54 +01:00
|
|
|
Requirements::clear();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$this->parseVariables();
|
|
|
|
|
2009-03-25 22:27:01 +01:00
|
|
|
if(empty($this->from)) $this->from = Email::getAdminEmail();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$this->setBounceHandlerURL( $this->bounceHandlerURL );
|
|
|
|
|
|
|
|
$headers = $this->customHeaders;
|
|
|
|
|
|
|
|
$headers['X-SilverStripeBounceURL'] = $this->bounceHandlerURL;
|
|
|
|
|
2009-03-25 22:27:01 +01:00
|
|
|
if($messageID) $headers['X-SilverStripeMessageID'] = project() . '.' . $messageID;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-03-25 22:27:01 +01:00
|
|
|
if(project()) $headers['X-SilverStripeSite'] = project();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$to = $this->to;
|
|
|
|
$subject = $this->subject;
|
|
|
|
if(self::$send_all_emails_to) {
|
|
|
|
$subject .= " [addressed to $to";
|
|
|
|
$to = self::$send_all_emails_to;
|
2009-03-17 10:12:54 +01:00
|
|
|
if($this->cc) $subject .= ", cc to $this->cc";
|
|
|
|
if($this->bcc) $subject .= ", bcc to $this->bcc";
|
2007-07-19 12:40:28 +02:00
|
|
|
$subject .= ']';
|
|
|
|
unset($headers['Cc']);
|
|
|
|
unset($headers['Bcc']);
|
|
|
|
} else {
|
2009-03-25 22:43:01 +01:00
|
|
|
if($this->cc) $headers['Cc'] = $this->cc;
|
|
|
|
if($this->bcc) $headers['Bcc'] = $this->bcc;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(self::$cc_all_emails_to) {
|
2008-07-15 07:06:16 +02:00
|
|
|
if(!empty($headers['Cc']) && trim($headers['Cc'])) {
|
|
|
|
$headers['Cc'] .= ', ' . self::$cc_all_emails_to;
|
|
|
|
} else {
|
|
|
|
$headers['Cc'] = self::$cc_all_emails_to;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-07-15 07:06:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(self::$bcc_all_emails_to) {
|
2008-07-15 07:06:16 +02:00
|
|
|
if(!empty($headers['Bcc']) && trim($headers['Bcc'])) {
|
|
|
|
$headers['Bcc'] .= ', ' . self::$bcc_all_emails_to;
|
|
|
|
} else {
|
2008-08-20 08:31:12 +02:00
|
|
|
$headers['Bcc'] = self::$bcc_all_emails_to;
|
2008-07-15 07:06:16 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-03-25 22:43:01 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
Requirements::restore();
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
return self::mailer()->sendHTML($to, $this->from, $subject, $this->body, $this->attachments, $headers,
|
|
|
|
$this->plaintext_body);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-10-01 16:40:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used as a default sender address in the {@link Email} class
|
|
|
|
* unless overwritten. Also shown to users on live environments
|
|
|
|
* as a contact address on system error pages.
|
|
|
|
*
|
2009-03-22 23:59:14 +01:00
|
|
|
* Used by {@link Email->send()}, {@link Email->sendPlain()}, {@link Debug->friendlyError()}.
|
2008-10-01 16:40:01 +02:00
|
|
|
*
|
|
|
|
* @param string $newEmail
|
|
|
|
*/
|
2009-03-25 22:27:01 +01:00
|
|
|
public static function setAdminEmail($newEmail) {
|
2007-07-19 12:40:28 +02:00
|
|
|
self::$admin_email_address = $newEmail;
|
|
|
|
}
|
2009-03-17 10:12:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public static function getAdminEmail() {
|
|
|
|
return self::$admin_email_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send every email generated by the Email class to the given address.
|
|
|
|
* It will also add " [addressed to (email), cc to (email), bcc to (email)]" to the end of the subject line
|
|
|
|
* This can be used when testing, by putting a command like this in your _config.php file
|
|
|
|
*
|
|
|
|
* if(!Director::isLive()) Email::send_all_emails_to("someone@example.com")
|
|
|
|
*/
|
|
|
|
public static function send_all_emails_to($emailAddress) {
|
|
|
|
self::$send_all_emails_to = $emailAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CC every email generated by the Email class to the given address.
|
2009-03-17 10:12:54 +01:00
|
|
|
* It won't affect the original delivery in the same way that send_all_emails_to does. It just adds a CC header
|
2012-09-26 23:34:00 +02:00
|
|
|
* with the given email address. Note that you can only call this once - subsequent calls will overwrite the
|
|
|
|
* configuration variable.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* This can be used when you have a system that relies heavily on email and you want someone to be checking all
|
|
|
|
* correspondence.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
|
|
|
* if(Director::isLive()) Email::cc_all_emails_to("supportperson@example.com")
|
|
|
|
*/
|
|
|
|
public static function cc_all_emails_to($emailAddress) {
|
|
|
|
self::$cc_all_emails_to = $emailAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BCC every email generated by the Email class to the given address.
|
2009-03-17 10:12:54 +01:00
|
|
|
* It won't affect the original delivery in the same way that send_all_emails_to does. It just adds a BCC header
|
2012-09-26 23:34:00 +02:00
|
|
|
* with the given email address. Note that you can only call this once - subsequent calls will overwrite the
|
|
|
|
* configuration variable.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* This can be used when you have a system that relies heavily on email and you want someone to be checking all
|
|
|
|
* correspondence.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
|
|
|
* if(Director::isLive()) Email::cc_all_emails_to("supportperson@example.com")
|
|
|
|
*/
|
|
|
|
public static function bcc_all_emails_to($emailAddress) {
|
|
|
|
self::$bcc_all_emails_to = $emailAddress;
|
|
|
|
}
|
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
/**
|
|
|
|
* Checks for RFC822-valid email format.
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
* @copyright Cal Henderson <cal@iamcal.com>
|
|
|
|
* This code is licensed under a Creative Commons Attribution-ShareAlike 2.5 License
|
|
|
|
* http://creativecommons.org/licenses/by-sa/2.5/
|
|
|
|
*/
|
2012-09-06 03:37:44 +02:00
|
|
|
public static function is_valid_address($email){
|
2009-03-17 10:12:54 +01:00
|
|
|
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
|
|
|
|
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
|
|
|
|
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
|
|
|
|
'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
|
|
|
|
$quoted_pair = '\\x5c[\\x00-\\x7f]';
|
|
|
|
$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
|
|
|
|
$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
|
|
|
|
$domain_ref = $atom;
|
|
|
|
$sub_domain = "($domain_ref|$domain_literal)";
|
|
|
|
$word = "($atom|$quoted_string)";
|
|
|
|
$domain = "$sub_domain(\\x2e$sub_domain)*";
|
|
|
|
$local_part = "$word(\\x2e$word)*";
|
|
|
|
$addr_spec = "$local_part\\x40$domain";
|
|
|
|
|
|
|
|
return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
|
|
|
|
}
|
2008-10-01 16:40:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode an email-address to protect it from spambots.
|
|
|
|
* At the moment only simple string substitutions,
|
|
|
|
* which are not 100% safe from email harvesting.
|
|
|
|
*
|
|
|
|
* @todo Integrate javascript-based solution
|
|
|
|
*
|
|
|
|
* @param string $email Email-address
|
|
|
|
* @param string $method Method for obfuscating/encoding the address
|
2009-03-17 10:12:54 +01:00
|
|
|
* - 'direction': Reverse the text and then use CSS to put the text direction back to normal
|
|
|
|
* - 'visible': Simple string substitution ('@' to '[at]', '.' to '[dot], '-' to [dash])
|
|
|
|
* - 'hex': Hexadecimal URL-Encoding - useful for mailto: links
|
2008-10-01 16:40:01 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function obfuscate($email, $method = 'visible') {
|
|
|
|
switch($method) {
|
2009-02-02 00:49:53 +01:00
|
|
|
case 'direction' :
|
|
|
|
Requirements::customCSS(
|
|
|
|
'span.codedirection { unicode-bidi: bidi-override; direction: rtl; }',
|
|
|
|
'codedirectionCSS'
|
|
|
|
);
|
|
|
|
return '<span class="codedirection">' . strrev($email) . '</span>';
|
2008-10-01 16:40:01 +02:00
|
|
|
case 'visible' :
|
|
|
|
$obfuscated = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
|
|
|
|
return strtr($email, $obfuscated);
|
|
|
|
case 'hex' :
|
|
|
|
$encoded = '';
|
|
|
|
for ($x=0; $x < strlen($email); $x++) $encoded .= '&#x' . bin2hex($email{$x}).';';
|
|
|
|
return $encoded;
|
|
|
|
default:
|
|
|
|
user_error('Email::obfuscate(): Unknown obfuscation method', E_USER_NOTICE);
|
|
|
|
return $email;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Base class that email bounce handlers extend
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage email
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Email_BounceHandler extends Controller {
|
|
|
|
|
2011-02-13 23:14:51 +01:00
|
|
|
static $allowed_actions = array(
|
|
|
|
'index'
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function init() {
|
2009-11-16 00:43:30 +01:00
|
|
|
BasicAuth::protect_entire_site(false);
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::init();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function index() {
|
2009-03-17 10:12:54 +01:00
|
|
|
$subclasses = ClassInfo::subclassesFor( $this->class );
|
|
|
|
unset($subclasses[$this->class]);
|
|
|
|
|
|
|
|
if( $subclasses ) {
|
|
|
|
$subclass = array_pop( $subclasses );
|
|
|
|
$task = new $subclass();
|
|
|
|
$task->index();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if access key exists
|
|
|
|
if( !isset($_REQUEST['Key']) ) {
|
|
|
|
echo 'Error: Access validation failed. No "Key" specified.';
|
|
|
|
return;
|
|
|
|
}
|
2007-09-16 04:00:28 +02:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
// Check against access key defined in framework/_config.php
|
2009-03-17 10:12:54 +01:00
|
|
|
if( $_REQUEST['Key'] != EMAIL_BOUNCEHANDLER_KEY) {
|
|
|
|
echo 'Error: Access validation failed. Invalid "Key" specified.';
|
|
|
|
return;
|
2007-09-16 02:40:29 +02:00
|
|
|
}
|
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
if( !$_REQUEST['Email'] ) {
|
|
|
|
echo "No email address";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->recordBounce( $_REQUEST['Email'], $_REQUEST['Date'], $_REQUEST['Time'], $_REQUEST['Message'] );
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function recordBounce( $email, $date = null, $time = null, $error = null ) {
|
2012-02-27 22:14:02 +01:00
|
|
|
if(preg_match('/<(.*)>/', $email, $parts)) $email = $parts[1];
|
2009-03-17 10:12:54 +01:00
|
|
|
|
|
|
|
$SQL_email = Convert::raw2sql($email);
|
|
|
|
$SQL_bounceTime = Convert::raw2sql("$date $time");
|
2007-09-16 02:40:29 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$duplicateBounce = DataObject::get_one("Email_BounceRecord",
|
|
|
|
"\"BounceEmail\" = '$SQL_email' AND (\"BounceTime\"+INTERVAL 1 MINUTE) > '$SQL_bounceTime'");
|
2009-03-17 10:12:54 +01:00
|
|
|
|
|
|
|
if(!$duplicateBounce) {
|
|
|
|
$record = new Email_BounceRecord();
|
|
|
|
|
|
|
|
$member = DataObject::get_one( 'Member', "\"Email\"='$SQL_email'" );
|
|
|
|
|
|
|
|
if( $member ) {
|
|
|
|
$record->MemberID = $member->ID;
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// If the SilverStripeMessageID (taken from the X-SilverStripeMessageID header embedded in the email)
|
|
|
|
// is sent, then log this bounce in a Newsletter_SentRecipient record so it will show up on the 'Sent
|
|
|
|
// Status Report' tab of the Newsletter
|
2009-03-17 10:12:54 +01:00
|
|
|
if( isset($_REQUEST['SilverStripeMessageID'])) {
|
|
|
|
// Note: was sent out with: $project . '.' . $messageID;
|
|
|
|
$message_id_parts = explode('.', $_REQUEST['SilverStripeMessageID']);
|
|
|
|
// Note: was encoded with: base64_encode( $newsletter->ID . '_' . date( 'd-m-Y H:i:s' ) );
|
|
|
|
$newsletter_id_date_parts = explode ('_', base64_decode($message_id_parts[1]) );
|
|
|
|
|
|
|
|
// Escape just in case
|
|
|
|
$SQL_memberID = Convert::raw2sql($member->ID);
|
|
|
|
$SQL_newsletterID = Convert::raw2sql($newsletter_id_date_parts[0]);
|
|
|
|
|
|
|
|
// Log the bounce
|
2012-09-26 23:34:00 +02:00
|
|
|
$oldNewsletterSentRecipient = DataObject::get_one("Newsletter_SentRecipient",
|
|
|
|
"\"MemberID\" = '$SQL_memberID' AND \"ParentID\" = '$SQL_newsletterID'"
|
|
|
|
. " AND \"Email\" = '$SQL_email'");
|
2009-03-17 10:12:54 +01:00
|
|
|
|
|
|
|
// Update the Newsletter_SentRecipient record if it exists
|
|
|
|
if($oldNewsletterSentRecipient) {
|
|
|
|
$oldNewsletterSentRecipient->Result = 'Bounced';
|
|
|
|
$oldNewsletterSentRecipient->write();
|
|
|
|
} else {
|
|
|
|
// For some reason it didn't exist, create a new record
|
|
|
|
$newNewsletterSentRecipient = new Newsletter_SentRecipient();
|
|
|
|
$newNewsletterSentRecipient->Email = $SQL_email;
|
|
|
|
$newNewsletterSentRecipient->MemberID = $member->ID;
|
|
|
|
$newNewsletterSentRecipient->Result = 'Bounced';
|
|
|
|
$newNewsletterSentRecipient->ParentID = $newsletter_id_date_parts[0];
|
|
|
|
$newNewsletterSentRecipient->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we are going to Blacklist this member so that email will not be sent to them in the future.
|
2012-09-26 23:34:00 +02:00
|
|
|
// Note: Sending can be re-enabled by going to 'Mailing List' 'Bounced' tab and unchecking the box
|
|
|
|
// under 'Blacklisted'
|
2009-03-17 10:12:54 +01:00
|
|
|
$member->setBlacklistedEmail(TRUE);
|
2012-09-26 23:34:00 +02:00
|
|
|
echo '<p><b>Member: '.$member->FirstName.' '.$member->Surname
|
|
|
|
.' <'.$member->Email.'> was added to the Email Blacklist!</b></p>';
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !$date )
|
|
|
|
$date = date( 'd-m-Y' );
|
|
|
|
/*else
|
|
|
|
$date = date( 'd-m-Y', strtotime( $date ) );*/
|
|
|
|
|
|
|
|
if( !$time )
|
|
|
|
$time = date( 'H:i:s' );
|
|
|
|
/*else
|
|
|
|
$time = date( 'H:i:s', strtotime( $time ) );*/
|
|
|
|
|
|
|
|
$record->BounceEmail = $email;
|
|
|
|
$record->BounceTime = $date . ' ' . $time;
|
|
|
|
$record->BounceMessage = $error;
|
|
|
|
$record->write();
|
|
|
|
|
|
|
|
echo "Handled bounced email to address: $email";
|
|
|
|
} else {
|
|
|
|
echo 'Sorry, this bounce report has already been logged, not logging this duplicate bounce.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Database record for recording a bounced email
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage email
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Email_BounceRecord extends DataObject {
|
2009-03-17 10:12:54 +01:00
|
|
|
static $db = array(
|
|
|
|
'BounceEmail' => 'Varchar',
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
'BounceTime' => 'SS_Datetime',
|
2009-03-17 10:12:54 +01:00
|
|
|
'BounceMessage' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
'Member' => 'Member'
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
static $has_many = array();
|
|
|
|
|
|
|
|
static $many_many = array();
|
|
|
|
|
|
|
|
static $defaults = array();
|
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
static $singular_name = 'Email Bounce Record';
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
/**
|
2009-03-17 10:12:54 +01:00
|
|
|
* a record of Email_BounceRecord can't be created manually. Instead, it should be
|
2009-02-02 00:49:53 +01:00
|
|
|
* created though system.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canCreate($member = null) {
|
2009-02-02 00:49:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|