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');
|
|
|
|
}
|
|
|
|
|
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 {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string $from Email-Address
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
protected $from;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string $to Email-Address. Use comma-separation to pass multiple email-addresses.
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
protected $to;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string $subject Subject of the email
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
protected $subject;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Passed straight into {@link $ss_template} as $Body variable.
|
2016-07-08 22:03:30 +02:00
|
|
|
*
|
|
|
|
* @var string $body HTML content of the email.
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
protected $body;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* If not set, defaults to converting the HTML-body with {@link Convert::xml2raw()}.
|
2016-07-08 22:03:30 +02:00
|
|
|
*
|
|
|
|
* @var string $plaintext_body Optional string for plaintext emails.
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
protected $plaintext_body;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string $cc
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
protected $cc;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string $bcc
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
protected $bcc;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var 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
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var array $attachments Internal, use {@link attachFileFromString()} or {@link attachFile()}
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $attachments = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var boolean $parseVariables_done
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $parseVariables_done = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string $ss_template The name of the used template (without *.ss extension)
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2016-07-08 22:03:30 +02:00
|
|
|
protected $ss_template = 'GenericEmail';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Used in the same way than {@link ViewableData->customize()}.
|
2016-07-08 22:03:30 +02:00
|
|
|
*
|
|
|
|
* @var ViewableData_Customised $template_data Additional data available in a template.
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2016-07-08 22:03:30 +02:00
|
|
|
protected $template_data;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-25 22:27:01 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* This will be set in the config on a site-by-site basis
|
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2014-08-15 08:53:05 +02:00
|
|
|
* @var string The default administrator email address.
|
2009-03-25 22:27:01 +01:00
|
|
|
*/
|
2013-03-27 13:41:24 +01:00
|
|
|
private static $admin_email = '';
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
/**
|
2013-01-10 00:33:20 +01:00
|
|
|
* Send every email generated by the Email class to the given address.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-01-10 00:33:20 +01:00
|
|
|
* It will also add " [addressed to (email), cc to (email), bcc to (email)]" to the end of the subject line
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-01-10 00:33:20 +01:00
|
|
|
* To set this, set Email.send_all_emails_to in your yml config file.
|
|
|
|
* It can also be set in _ss_environment.php with SS_SEND_ALL_EMAILS_TO.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @config
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string $send_all_emails_to Email-Address
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2016-07-08 22:03:30 +02:00
|
|
|
private static $send_all_emails_to;
|
2011-03-02 06:23:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send every email generated by the Email class *from* the given address.
|
|
|
|
* It will also add " [, from to (email)]" to the end of the subject line
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-02 06:23:07 +01:00
|
|
|
* To set this, set Email.send_all_emails_from in your yml config file.
|
|
|
|
* It can also be set in _ss_environment.php with SS_SEND_ALL_EMAILS_FROM.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @config
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string $send_all_emails_from Email-Address
|
2011-03-02 06:23:07 +01:00
|
|
|
*/
|
2016-07-08 22:03:30 +02:00
|
|
|
private static $send_all_emails_from;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string BCC every email generated by the Email class to the given address.
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2016-07-08 22:03:30 +02:00
|
|
|
private static $bcc_all_emails_to;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2016-07-08 22:03:30 +02:00
|
|
|
* @var string CC every email generated by the Email class to the given address.
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2016-07-08 22:03:30 +02:00
|
|
|
private static $cc_all_emails_to;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new email.
|
2016-07-08 22:03:30 +02:00
|
|
|
*
|
|
|
|
* @param string|null $from
|
|
|
|
* @param string|null $to
|
|
|
|
* @param string|null $subject
|
|
|
|
* @param string|null $body
|
|
|
|
* @param string|null $bounceHandlerURL
|
|
|
|
* @param string|null $cc
|
|
|
|
* @param string|null $bcc
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
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) {
|
|
|
|
|
2016-07-08 22:03:30 +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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
if($bounceHandlerURL !== null) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use "emailbouncehandler" module');
|
2012-12-03 01:03:18 +01:00
|
|
|
}
|
|
|
|
|
2009-09-18 05:02:19 +02:00
|
|
|
parent::__construct();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* Get the mailer.
|
|
|
|
*
|
|
|
|
* @return Mailer
|
|
|
|
*/
|
|
|
|
public static function mailer() {
|
|
|
|
return Injector::inst()->get('Mailer');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated since version 4.0
|
|
|
|
*/
|
|
|
|
public static function set_mailer(Mailer $mailer) {
|
|
|
|
Deprecation::notice('4.0', 'Use Injector to override the Mailer service');
|
|
|
|
Injector::inst()->registerService($mailer, 'Mailer');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach a file based on provided raw data.
|
|
|
|
*
|
|
|
|
* @param string $data The raw file data (not encoded).
|
|
|
|
* @param string $attachedFilename Name of the file that should appear once it's sent as a separate attachment.
|
|
|
|
* @param string|null $mimeType MIME type to use when attaching file. If not provided, will attempt to infer via HTTP::get_mime_type().
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function attachFileFromString($data, $attachedFilename, $mimeType = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->attachments[] = array(
|
|
|
|
'contents' => $data,
|
2016-07-08 22:03:30 +02:00
|
|
|
'filename' => $attachedFilename,
|
|
|
|
'mimetype' => $mimeType,
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-19 01:59:27 +02:00
|
|
|
/**
|
|
|
|
* @deprecated since version 4.0
|
|
|
|
*/
|
2012-12-03 01:03:18 +01:00
|
|
|
public function setBounceHandlerURL($bounceHandlerURL) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use "emailbouncehandler" module');
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* Attach the specified file to this email message.
|
|
|
|
*
|
|
|
|
* @param string $filename Relative or full path to file you wish to attach to this email message.
|
|
|
|
* @param string|null $attachedFilename Name of the file that should appear once it's sent as a separate attachment.
|
|
|
|
* @param string|null $mimeType MIME type to use when attaching file. If not provided, will attempt to infer via HTTP::get_mime_type().
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
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)) {
|
2016-07-08 22:03:30 +02:00
|
|
|
$this->attachFileFromString(file_get_contents($absoluteFileName), $attachedFilename, $mimeType);
|
2008-10-08 04:00:12 +02:00
|
|
|
} else {
|
|
|
|
user_error("Could not attach '$absoluteFileName' to email. File does not exist.", E_USER_NOTICE);
|
|
|
|
}
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Subject() {
|
|
|
|
return $this->subject;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Body() {
|
|
|
|
return $this->body;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function To() {
|
|
|
|
return $this->to;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function From() {
|
|
|
|
return $this->from;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Cc() {
|
|
|
|
return $this->cc;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Bcc() {
|
|
|
|
return $this->bcc;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @param string $val
|
|
|
|
* @return $this
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setSubject($val) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->subject = $val;
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @param string $val
|
|
|
|
* @return $this
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setBody($val) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->body = $val;
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @param string $val
|
|
|
|
* @return $this
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setTo($val) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->to = $val;
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @param string $val
|
|
|
|
* @return $this
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setFrom($val) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->from = $val;
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @param string $val
|
|
|
|
* @return $this
|
|
|
|
*/
|
2008-12-04 23:38:32 +01:00
|
|
|
public function setCc($val) {
|
|
|
|
$this->cc = $val;
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @param string $val
|
|
|
|
* @return $this
|
|
|
|
*/
|
2008-12-04 23:38:32 +01:00
|
|
|
public function setBcc($val) {
|
|
|
|
$this->bcc = $val;
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-25 23:02:30 +01:00
|
|
|
/**
|
|
|
|
* Set the "Reply-To" header with an email address.
|
2016-07-08 22:03:30 +02:00
|
|
|
*
|
2017-01-17 12:37:13 +01:00
|
|
|
* @param string $val The email address of the "Reply-To" header
|
2016-07-08 22:03:30 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setReplyTo($val) {
|
|
|
|
$this->addCustomHeader('Reply-To', $val);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $email
|
|
|
|
* @return $this
|
|
|
|
* @deprecated 4.0 Use the "setReplyTo" method instead
|
2009-03-25 23:02:30 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function replyTo($email) {
|
2016-07-08 22:03:30 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "setReplyTo" method instead');
|
|
|
|
$this->setReplyTo($email);
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2009-03-25 23:02:30 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* Add a custom header to this email message. Useful for implementing all those cool features that we didn't think of.
|
|
|
|
*
|
|
|
|
* IMPORTANT: If the specified header already exists, the provided value will be appended!
|
|
|
|
*
|
|
|
|
* @todo Should there be an option to replace instead of append? Or maybe a new method ->setCustomHeader()?
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 21:20:08 +01:00
|
|
|
* @param string $headerName
|
|
|
|
* @param string $headerValue
|
2016-07-08 22:03:30 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function addCustomHeader($headerName, $headerValue) {
|
2016-07-08 22:03:30 +02:00
|
|
|
if ($headerName == 'Cc') {
|
|
|
|
$this->cc = $headerValue;
|
|
|
|
} elseif($headerName == 'Bcc') {
|
|
|
|
$this->bcc = $headerValue;
|
|
|
|
} else {
|
|
|
|
// Append value instead of replacing.
|
|
|
|
if(isset($this->customHeaders[$headerName])) {
|
|
|
|
$this->customHeaders[$headerName] .= ", " . $headerValue;
|
|
|
|
} else {
|
|
|
|
$this->customHeaders[$headerName] = $headerValue;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-06-12 11:29:05 +02:00
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-04-09 13:25:06 +02:00
|
|
|
public function BaseURL() {
|
|
|
|
return Director::absoluteBaseURL();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-06-12 11:29:05 +02:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* Get an HTML string for debugging purposes.
|
|
|
|
*
|
|
|
|
* @return string
|
2008-06-12 11:29:05 +02:00
|
|
|
*/
|
|
|
|
public function debug() {
|
|
|
|
$this->parseVariables();
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
return "<h2>Email template $this->class</h2>\n" .
|
2008-06-12 11:29:05 +02:00
|
|
|
"<p><b>From:</b> $this->from\n" .
|
2014-08-15 08:53:05 +02:00
|
|
|
"<b>To:</b> $this->to\n" .
|
|
|
|
"<b>Cc:</b> $this->cc\n" .
|
|
|
|
"<b>Bcc:</b> $this->bcc\n" .
|
|
|
|
"<b>Subject:</b> $this->subject</p>" .
|
2008-06-12 11:29:05 +02:00
|
|
|
$this->body;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* Set template name (without *.ss extension).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-02-02 00:49:53 +01:00
|
|
|
* @param string $template
|
2016-07-08 22:03:30 +02:00
|
|
|
* @return $this
|
2009-02-02 00:49:53 +01:00
|
|
|
*/
|
|
|
|
public function setTemplate($template) {
|
|
|
|
$this->ss_template = $template;
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTemplate() {
|
|
|
|
return $this->ss_template;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
2017-01-17 10:54:18 +01:00
|
|
|
* @return bool
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
|
|
|
public function IsEmail() {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-09 13:25:06 +02:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* Populate this email template with values. This may be called many times.
|
|
|
|
*
|
|
|
|
* @param array|ViewableData $data
|
|
|
|
* @return $this
|
2008-04-09 13:25:06 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function populateTemplate($data) {
|
2009-03-17 10:12:54 +01:00
|
|
|
if($this->template_data) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->template_data = $this->template_data->customise($data);
|
2008-04-09 13:25:06 +02:00
|
|
|
} else {
|
|
|
|
if(is_array($data)) $data = new ArrayData($data);
|
|
|
|
$this->template_data = $this->customise($data);
|
|
|
|
}
|
|
|
|
$this->parseVariables_done = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-21 10:58:45 +02:00
|
|
|
return $this;
|
2008-04-09 13:25:06 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
2014-08-15 08:53:05 +02:00
|
|
|
* and it won't be plain email :)
|
2016-07-08 22:03:30 +02:00
|
|
|
*
|
|
|
|
* @param bool $isPlain
|
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-03-12 05:19:39 +01:00
|
|
|
protected function parseVariables($isPlain = false) {
|
2013-06-21 04:16:07 +02:00
|
|
|
$origState = Config::inst()->get('SSViewer', 'source_file_comments');
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('SSViewer', 'source_file_comments', false);
|
2013-06-21 04:16:07 +02:00
|
|
|
|
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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$template = new SSViewer($this->ss_template);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($template->exists()) {
|
|
|
|
$fullBody = $template->process($data);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Rewrite relative URLs
|
|
|
|
$this->body = HTTP::absoluteURLs($fullBody);
|
|
|
|
}
|
2013-06-21 04:16:07 +02:00
|
|
|
Config::inst()->update('SSViewer', 'source_file_comments', $origState);
|
2013-10-21 10:58:45 +02:00
|
|
|
|
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-09 13:25:06 +02:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* @param string $address
|
|
|
|
* @return bool
|
|
|
|
* @deprecated 4.0 Use the "is_valid_address" method instead
|
2009-03-17 10:12:54 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function validEmailAddress($address) {
|
2016-07-08 22:03:30 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "is_valid_address" method instead');
|
|
|
|
return static::is_valid_address($address);
|
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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 10:12:54 +01:00
|
|
|
* @see send() for sending emails with HTML content.
|
|
|
|
* @uses Mailer->sendPlain()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 10:12:54 +01:00
|
|
|
* @param string $messageID Optional message ID so the message can be identified in bounces etc.
|
2016-07-08 22:03:30 +02:00
|
|
|
* @return mixed 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). See Mailer->sendPlain() for return type details.
|
2009-03-17 10:12:54 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function sendPlain($messageID = null) {
|
2009-03-17 10:12:54 +01:00
|
|
|
Requirements::clear();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
$this->parseVariables(true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
if(empty($this->from)) $this->from = Email::config()->admin_email;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-25 22:56:25 +01:00
|
|
|
$headers = $this->customHeaders;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-25 22:46:48 +01:00
|
|
|
if($messageID) $headers['X-SilverStripeMessageID'] = project() . '.' . $messageID;
|
2014-08-15 08:53:05 +02: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;
|
2011-03-02 06:23:07 +01:00
|
|
|
$from = $this->from;
|
2009-03-17 10:12:54 +01:00
|
|
|
$subject = $this->subject;
|
2013-01-10 00:33:20 +01:00
|
|
|
if($sendAllTo = $this->config()->send_all_emails_to) {
|
2009-03-17 10:12:54 +01:00
|
|
|
$subject .= " [addressed to $to";
|
2013-01-10 00:33:20 +01:00
|
|
|
$to = $sendAllTo;
|
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 .= ']';
|
2013-01-10 00:33:20 +01:00
|
|
|
unset($headers['Cc']);
|
|
|
|
unset($headers['Bcc']);
|
2009-03-17 10:12:54 +01:00
|
|
|
} 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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-10 00:33:20 +01:00
|
|
|
if($ccAllTo = $this->config()->cc_all_emails_to) {
|
2009-03-25 22:43:01 +01:00
|
|
|
if(!empty($headers['Cc']) && trim($headers['Cc'])) {
|
2013-01-10 00:33:20 +01:00
|
|
|
$headers['Cc'] .= ', ' . $ccAllTo;
|
2009-03-25 22:43:01 +01:00
|
|
|
} else {
|
2013-01-10 00:33:20 +01:00
|
|
|
$headers['Cc'] = $ccAllTo;
|
2009-03-25 22:43:01 +01:00
|
|
|
}
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
2009-03-25 22:27:01 +01:00
|
|
|
|
2013-01-10 00:33:20 +01:00
|
|
|
if($bccAllTo = $this->config()->bcc_all_emails_to) {
|
2009-03-25 22:43:01 +01:00
|
|
|
if(!empty($headers['Bcc']) && trim($headers['Bcc'])) {
|
2013-01-10 00:33:20 +01:00
|
|
|
$headers['Bcc'] .= ', ' . $bccAllTo;
|
2009-03-25 22:43:01 +01:00
|
|
|
} else {
|
2013-01-10 00:33:20 +01:00
|
|
|
$headers['Bcc'] = $bccAllTo;
|
2009-03-25 22:43:01 +01:00
|
|
|
}
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
|
|
|
|
2011-03-02 06:23:07 +01:00
|
|
|
if($sendAllfrom = $this->config()->send_all_emails_from) {
|
|
|
|
if($from) $subject .= " [from $from]";
|
|
|
|
$from = $sendAllfrom;
|
|
|
|
}
|
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
Requirements::restore();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-02 06:23:07 +01:00
|
|
|
return self::mailer()->sendPlain($to, $from, $subject, $this->body, $this->attachments, $headers);
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 10:07:33 +01:00
|
|
|
* @param string $messageID Optional message ID so the message can be identified in bounces etc.
|
2016-07-08 22:03:30 +02:00
|
|
|
* @return mixed 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). See Mailer->sendPlain() for return type details.
|
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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->parseVariables();
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
if(empty($this->from)) $this->from = Email::config()->admin_email;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$headers = $this->customHeaders;
|
|
|
|
|
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
|
|
|
|
2013-01-10 00:33:20 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$to = $this->to;
|
2011-03-02 06:23:07 +01:00
|
|
|
$from = $this->from;
|
2007-07-19 12:40:28 +02:00
|
|
|
$subject = $this->subject;
|
2013-01-10 00:33:20 +01:00
|
|
|
if($sendAllTo = $this->config()->send_all_emails_to) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$subject .= " [addressed to $to";
|
2013-01-10 00:33:20 +01:00
|
|
|
$to = $sendAllTo;
|
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']);
|
2013-01-10 00:33:20 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-10 00:33:20 +01:00
|
|
|
if($ccAllTo = $this->config()->cc_all_emails_to) {
|
2008-07-15 07:06:16 +02:00
|
|
|
if(!empty($headers['Cc']) && trim($headers['Cc'])) {
|
2013-01-10 00:33:20 +01:00
|
|
|
$headers['Cc'] .= ', ' . $ccAllTo;
|
2008-07-15 07:06:16 +02:00
|
|
|
} else {
|
2013-01-10 00:33:20 +01:00
|
|
|
$headers['Cc'] = $ccAllTo;
|
2008-07-15 07:06:16 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-01-10 00:33:20 +01:00
|
|
|
|
|
|
|
if($bccAllTo = $this->config()->bcc_all_emails_to) {
|
2008-07-15 07:06:16 +02:00
|
|
|
if(!empty($headers['Bcc']) && trim($headers['Bcc'])) {
|
2013-01-10 00:33:20 +01:00
|
|
|
$headers['Bcc'] .= ', ' . $bccAllTo;
|
2008-07-15 07:06:16 +02:00
|
|
|
} else {
|
2013-01-10 00:33:20 +01:00
|
|
|
$headers['Bcc'] = $bccAllTo;
|
2008-07-15 07:06:16 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-02 06:23:07 +01:00
|
|
|
|
|
|
|
if($sendAllfrom = $this->config()->send_all_emails_from) {
|
|
|
|
if($from) $subject .= " [from $from]";
|
|
|
|
$from = $sendAllfrom;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
Requirements::restore();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-02 06:23:07 +01:00
|
|
|
return self::mailer()->sendHTML($to, $from, $subject, $this->body, $this->attachments, $headers,
|
2012-09-26 23:34:00 +02:00
|
|
|
$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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-22 23:59:14 +01:00
|
|
|
* Used by {@link Email->send()}, {@link Email->sendPlain()}, {@link Debug->friendlyError()}.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Email.admin_email" config setting instead
|
2008-10-01 16:40:01 +02:00
|
|
|
* @param string $newEmail
|
|
|
|
*/
|
2009-03-25 22:27:01 +01:00
|
|
|
public static function setAdminEmail($newEmail) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Email.admin_email" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Email', 'admin_email', $newEmail);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
/**
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Email.admin_email" config setting instead
|
2009-03-17 10:12:54 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public static function getAdminEmail() {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Email.admin_email" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
return Config::inst()->get('Email', 'admin_email');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* if(!Director::isLive()) Email::send_all_emails_to("someone@example.com")
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Email.send_all_emails_to" config setting instead
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public static function send_all_emails_to($emailAddress) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Email.send_all_emails_to" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Email', 'send_all_emails_to', $emailAddress);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-02 06:23:07 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* CC every email generated by the Email class to the given address.
|
2014-08-15 08:53:05 +02: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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* if(Director::isLive()) Email::cc_all_emails_to("supportperson@example.com")
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Email.cc_all_emails_to" config setting instead
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public static function cc_all_emails_to($emailAddress) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Email.cc_all_emails_to" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Email', 'cc_all_emails_to', $emailAddress);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BCC every email generated by the Email class to the given address.
|
2014-08-15 08:53:05 +02: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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* if(Director::isLive()) Email::cc_all_emails_to("supportperson@example.com")
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Email.bcc_all_emails_to" config setting instead
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public static function bcc_all_emails_to($emailAddress) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Email.bcc_all_emails_to" config setting instead');
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Email', 'bcc_all_emails_to', $emailAddress);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-17 10:12:54 +01:00
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* Validates the email address to get as close to RFC 822 compliant as possible.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-07-08 22:03:30 +02:00
|
|
|
* @param string $email
|
|
|
|
* @return bool
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @copyright Cal Henderson <cal@iamcal.com>
|
|
|
|
* This code is licensed under a Creative Commons Attribution-ShareAlike 2.5 License
|
2009-03-17 10:12:54 +01:00
|
|
|
* 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";
|
|
|
|
|
2016-07-08 22:03:30 +02:00
|
|
|
return preg_match("!^$addr_spec$!", $email) === 1;
|
2009-03-17 10:12:54 +01:00
|
|
|
}
|
2008-10-01 16:40:01 +02:00
|
|
|
|
|
|
|
/**
|
2016-07-08 22:03:30 +02:00
|
|
|
* Encode an email-address to help protect it from spam bots. At the moment only simple string substitutions, which
|
|
|
|
* are not 100% safe from email harvesting.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-01 16:40:01 +02:00
|
|
|
* @todo Integrate javascript-based solution
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-01 16:40:01 +02:00
|
|
|
* @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 = '';
|
2020-04-01 14:32:31 +02:00
|
|
|
for ($x=0; $x < strlen($email); $x++) $encoded .= '&#x' . bin2hex($email[$x]).';';
|
2008-10-01 16:40:01 +02:00
|
|
|
return $encoded;
|
|
|
|
default:
|
|
|
|
user_error('Email::obfuscate(): Unknown obfuscation method', E_USER_NOTICE);
|
|
|
|
return $email;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|