diff --git a/composer.json b/composer.json index 00299763..40b7eb80 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "require": { - "mnapoli/php-di": "~4.4" + "mnapoli/php-di": "~4.4", + "phpmailer/phpmailer": "~5.2" }, "require-dev": { diff --git a/data/config.ini b/data/config.ini index ebdbdda8..e32b34bd 100644 --- a/data/config.ini +++ b/data/config.ini @@ -3,8 +3,12 @@ serviceName = szurubooru serviceBaseUrl = http://localhost/ [mail] -botName = szurubooru bot -botAddress = noreply@localhost +smtpHost = localhost +smtpPort = 25 +smtpUserName = bot +smtpUserPass = groovy123 +smtpFrom = noreply@szurubooru +smtpFromName = szurubooru bot passwordResetSubject = szurubooru - password reset passwordResetBodyPath = mail/password-reset.txt activationSubject = szurubooru - account activation diff --git a/scripts/test-email.php b/scripts/test-email.php new file mode 100755 index 00000000..1e13eb80 --- /dev/null +++ b/scripts/test-email.php @@ -0,0 +1,19 @@ +#!/usr/bin/php +sendEmail($address, 'test', "test\nąćęłóńśźż\n←↑→↓"); diff --git a/src/Services/EmailService.php b/src/Services/EmailService.php index c4d7809e..752c4507 100644 --- a/src/Services/EmailService.php +++ b/src/Services/EmailService.php @@ -43,36 +43,27 @@ class EmailService $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']) - ? $_SERVER['SERVER_ADDR'] - : ''; + $mail->SMTPDebug = 0; + $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", '
', $body); + $mail->AltBody = $body; + $mail->addAddress($recipientEmail); - $body = wordwrap($body, 70); - if (empty($recipientEmail)) - 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); + if (!$mail->send()) + throw new \Exception('Couldn\'t send mail to ' . $recipientEmail . ': ' . $mail->ErrorInfo); } private function tokenizeFile($templatePath, $tokens = [])