Switched to PHPMailer

This commit is contained in:
rr- 2015-06-28 12:24:46 +02:00
parent 027b98ce76
commit fd448bac87
4 changed files with 45 additions and 30 deletions

View file

@ -1,6 +1,7 @@
{ {
"require": { "require": {
"mnapoli/php-di": "~4.4" "mnapoli/php-di": "~4.4",
"phpmailer/phpmailer": "~5.2"
}, },
"require-dev": { "require-dev": {

View file

@ -3,8 +3,12 @@ serviceName = szurubooru
serviceBaseUrl = http://localhost/ serviceBaseUrl = http://localhost/
[mail] [mail]
botName = szurubooru bot smtpHost = localhost
botAddress = noreply@localhost smtpPort = 25
smtpUserName = bot
smtpUserPass = groovy123
smtpFrom = noreply@szurubooru
smtpFromName = szurubooru bot
passwordResetSubject = szurubooru - password reset passwordResetSubject = szurubooru - password reset
passwordResetBodyPath = mail/password-reset.txt passwordResetBodyPath = mail/password-reset.txt
activationSubject = szurubooru - account activation activationSubject = szurubooru - account activation

19
scripts/test-email.php Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/php
<?php
require_once(__DIR__
. DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . 'src'
. DIRECTORY_SEPARATOR . 'Bootstrap.php');
use Szurubooru\Injector;
use Szurubooru\Services\EmailService;
if (!isset($argv[1]))
{
echo "No recipient email specified.";
return;
}
$address = $argv[1];
$emailService = Injector::get(EmailService::class);
$emailService->sendEmail($address, 'test', "test\nąćęłóńśźż\n←↑→↓");

View file

@ -43,36 +43,27 @@ class EmailService
$this->sendEmail($user->getEmailUnconfirmed(), $mailSubject, $mailBody); $this->sendEmail($user->getEmailUnconfirmed(), $mailSubject, $mailBody);
} }
private function sendEmail($recipientEmail, $subject, $body) public function sendEmail($recipientEmail, $subject, $body)
{ {
$domain = substr($this->config->mail->botEmail, strpos($this->config->mail->botEmail, '@') + 1); $mail = new \PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$clientIp = isset($_SERVER['SERVER_ADDR']) $mail->SMTPDebug = 0;
? $_SERVER['SERVER_ADDR'] $mail->SMTPAuth = true;
: ''; $mail->Host = $this->config->mail->smtpHost;
$mail->Port = $this->config->mail->smtpPort;
$mail->Username = $this->config->mail->smtpUserName;
$mail->Password = $this->config->mail->smtpUserPass;
$mail->From = $this->config->mail->smtpFrom;
$mail->FromName = $this->config->mail->smtpFromName;
$mail->Subject = $subject;
$mail->Body = str_replace("\n", '<br>', $body);
$mail->AltBody = $body;
$mail->addAddress($recipientEmail);
$body = wordwrap($body, 70); if (!$mail->send())
if (empty($recipientEmail)) throw new \Exception('Couldn\'t send mail to ' . $recipientEmail . ': ' . $mail->ErrorInfo);
throw new \InvalidArgumentException('Destination e-mail address was not found');
$messageId = sha1(date('r') . uniqid()) . '@' . $domain;
$headers = [];
$headers[] = sprintf('MIME-Version: 1.0');
$headers[] = sprintf('Content-Transfer-Encoding: 7bit');
$headers[] = sprintf('Date: %s', date('r'));
$headers[] = sprintf('Message-ID: <%s>', $messageId);
$headers[] = sprintf('From: %s <%s>', $this->config->mail->botName, $this->config->mail->botEmail);
$headers[] = sprintf('Reply-To: %s', $this->config->mail->botEmail);
$headers[] = sprintf('Return-Path: %s', $this->config->mail->botEmail);
$headers[] = sprintf('Subject: %s', $subject);
$headers[] = sprintf('Content-Type: text/plain; charset=utf-8');
$headers[] = sprintf('X-Mailer: PHP/%s', phpversion());
$headers[] = sprintf('X-Originating-IP: %s', $clientIp);
$encodedSubject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
mail($recipientEmail, $encodedSubject, $body, implode("\r\n", $headers), '-f' . $this->config->mail->botEmail);
} }
private function tokenizeFile($templatePath, $tokens = []) private function tokenizeFile($templatePath, $tokens = [])