Skip to content
Snippets Groups Projects
Commit cd8da302 authored by Bryce Lowe's avatar Bryce Lowe
Browse files

added ability to select different mail transport options

parent 50522556
No related branches found
No related tags found
No related merge requests found
......@@ -75,6 +75,37 @@ $config = [
'technicalcontact_name' => 'Administrator',
'technicalcontact_email' => 'na@example.org',
/*
* (Optional) The method by which email is delivered. Defaults to mail which utilizes the
* PHP mail() function.
*
* Valid options are: mail, sendmail and smtp.
*/
//'mail.transport.method' => 'smtp',
/*
* Set the transport options for the transport method specified. The valid settings are relative to the
* selected transport method.
*/
//// smtp mail transport options
// 'mail.transport.options' => [
// 'host' => 'mail.example.org',
// 'port' => 25,
// 'username' => 'user@example.org', 'password' => '',
// 'security' => 'tls',
// ],
//// sendmail mail transport options
// 'mail.transport.options' => [
// 'path' => '/usr/sbin/sendmail'
// ],
/*
* (Optional) When using the Sendmail transport method, provide an alternate path to the Sendmail
* executable.
*/
//'mail.sendmail.path' => '',
/*
* The envelope from address for outgoing emails.
* This should be in a domain that has your application's IP addresses in its SPF record
......
......@@ -47,6 +47,8 @@ class EMail
$this->mail->Subject = $subject;
$this->mail->setFrom($from ?: static::getDefaultMailAddress());
$this->mail->addAddress($to ?: static::getDefaultMailAddress());
static::initFromConfig($this);
}
......@@ -143,6 +145,103 @@ class EMail
$this->mail->send();
}
/**
* Sets the method by which the email will be sent. Currently supports what
* PHPMailer supports: sendmail, mail and smtp.
*
* @param string $transportMethod the transport method
* @param array $transportOptions options for the transport method
*
* @return void
*
* @throws \InvalidArgumentException
*/
public function setTransportMethod($transportMethod, array $transportOptions = [])
{
assert(is_string($transportMethod));
assert(is_array($transportOptions));
switch (strtolower($transportMethod)) {
// smtp transport method
case 'smtp':
$this->mail->isSMTP();
// set the host (required)
if (isset($transportOptions['host'])) {
$this->mail->Host = $transportOptions['host'];
}
// throw an exception otherwise
else {
throw new \InvalidArgumentException("Missing Required Email Transport Parameter 'host'");
}
// set the port (optional, assume standard SMTP port 25 if not provided)
$this->mail->Port = (isset($transportOptions['port'])) ? (int)$transportOptions['port'] : 25;
// smtp auth: enabled if username or password is set
if (isset($transportOptions['username']) || isset($transportOptions['password'])) {
$this->mail->SMTPAuth = true;
}
// smtp auth: username
if (isset($transportOptions['username'])) {
$this->mail->Username = $transportOptions['username'];
}
// smtp auth: password
if (isset($transportOptions['password'])) {
$this->mail->Password = $transportOptions['password'];
}
// smtp security: encryption type
if (isset($transportOptions['secure'])) {
$this->mail->SMTPSecure = $transportOptions['secure'];
}
// smtp security: enable or disable smtp auto tls
if (isset($transportOptions['autotls'])) {
$this->mail->SMTPAutoTLS = (bool)$transportOptions['autotls'];
}
break;
//mail transport method
case 'mail':
$this->mail->isMail();
break;
// sendmail transport method
case 'sendmail':
$this->mail->isSendmail();
// override the default path of the sendmail executable
if (isset($transportOptions['path'])) {
$this->mail->Sendmail = $transportOptions['path'];
}
break;
default:
throw new \InvalidArgumentException("Invalid Mail Transport Method - Check 'mail.transport.method' Configuration Option");
}
}
/**
* Initializes the provided EMail object with the configuration provided from the SimpleSAMLphp configuration.
*
* @param EMail $EMail
* @return EMail
* @throws \Exception
*/
public static function initFromConfig(EMail $EMail)
{
assert($EMail instanceof EMail);
$config = Configuration::getInstance();
$EMail->setTransportMethod(
$config->getString('mail.transport.method', ''),
$config->getArrayize('mail.transport.options', [])
);
return $EMail;
}
/**
* Generate the body of the e-mail
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment