diff --git a/src/PHPCensor/Helper/Email.php b/src/PHPCensor/Helper/Email.php index f2a2ef11..c9db49c5 100644 --- a/src/PHPCensor/Helper/Email.php +++ b/src/PHPCensor/Helper/Email.php @@ -97,11 +97,26 @@ class Email return $this; } + /** + * Get the from address to use for the email. + * @return mixed|string + */ + protected function getFrom() + { + $email = $this->config->get('php-censor.email_settings.from_address', self::DEFAULT_FROM); + + if (empty($email)) { + $email = self::DEFAULT_FROM; + } + + return $email; + } + /** * Send the email. - * + * * @param Builder $builder - * + * * @return integer */ public function send(Builder $builder) @@ -109,50 +124,6 @@ class Email $smtpServer = $this->config->get('php-censor.email_settings.smtp_address'); $builder->logDebug(sprintf("SMTP: '%s'", !empty($smtpServer) ? 'true' : 'false')); - if (empty($smtpServer)) { - return (integer)$this->sendViaMail(); - } else { - return (integer)$this->sendViaSwiftMailer(); - } - } - - /** - * Sends the email via the built in PHP mail() function. - * @return bool - */ - protected function sendViaMail() - { - $headers = ''; - - if ($this->isHtml) { - $headers = 'Content-Type: text/html' . PHP_EOL; - } - - $headers .= 'From: ' . $this->getFrom() . PHP_EOL; - - $emailTo = []; - foreach ($this->emailTo as $email => $name) { - $thisTo = $email; - - if (!is_null($name)) { - $thisTo = '"' . $name . '" <' . $thisTo . '>'; - } - - $emailTo[] = $thisTo; - } - - $emailTo = implode(', ', $emailTo); - - return mail($emailTo, $this->subject, $this->body, $headers); - } - - /** - * Sends the email using SwiftMailer. - * - * @return int - */ - protected function sendViaSwiftMailer() - { $factory = new MailerFactory($this->config->get('php-censor')); $mailer = $factory->getSwiftMailerFromConfig(); @@ -171,19 +142,4 @@ class Email return $mailer->send($message); } - - /** - * Get the from address to use for the email. - * @return mixed|string - */ - protected function getFrom() - { - $email = $this->config->get('php-censor.email_settings.from_address', self::DEFAULT_FROM); - - if (empty($email)) { - $email = self::DEFAULT_FROM; - } - - return $email; - } } diff --git a/src/PHPCensor/Helper/MailerFactory.php b/src/PHPCensor/Helper/MailerFactory.php index 220e323a..ad0fc2b8 100644 --- a/src/PHPCensor/Helper/MailerFactory.php +++ b/src/PHPCensor/Helper/MailerFactory.php @@ -39,23 +39,28 @@ class MailerFactory */ public function getSwiftMailerFromConfig() { - $encryptionType = $this->getMailConfig('smtp_encryption'); + if ($this->getMailConfig('smtp_address')) { + $encryptionType = $this->getMailConfig('smtp_encryption'); - // Workaround issue where smtp_encryption could == 1 in the past by - // checking it is a valid transport - if ($encryptionType && !in_array($encryptionType, stream_get_transports())) { - $encryptionType = null; + // Workaround issue where smtp_encryption could == 1 in the past by + // checking it is a valid transport + if ($encryptionType && !in_array($encryptionType, stream_get_transports())) { + $encryptionType = null; + } + + /** @var \Swift_SmtpTransport $transport */ + $transport = \Swift_SmtpTransport::newInstance( + $this->getMailConfig('smtp_address'), + $this->getMailConfig('smtp_port'), + $encryptionType + ); + + $transport->setUsername($this->getMailConfig('smtp_username')); + $transport->setPassword($this->getMailConfig('smtp_password')); + } else { + $transport = \Swift_MailTransport::newInstance(); } - /** @var \Swift_SmtpTransport $transport */ - $transport = \Swift_SmtpTransport::newInstance( - $this->getMailConfig('smtp_address'), - $this->getMailConfig('smtp_port'), - $encryptionType - ); - $transport->setUsername($this->getMailConfig('smtp_username')); - $transport->setPassword($this->getMailConfig('smtp_password')); - return \Swift_Mailer::newInstance($transport); } @@ -73,7 +78,7 @@ class MailerFactory switch ($configName) { case 'smtp_address': - return "localhost"; + return ""; case 'default_mailto_address': return null; case 'smtp_port':