Skip to content
Snippets Groups Projects
Commit 5ffc332b authored by Olivier Perez's avatar Olivier Perez
Browse files

Merge branch 'Add_nonHTML_version_to_mails' into 'develop'

Fix #87 Use PHPMailer to add non-HTML body to mails

Utilisation de PHPMailer

See merge request !70
parents 242b2ca0 882203fa
No related branches found
No related tags found
No related merge requests found
<?php
namespace Framadate\Services;
use PHPMailer;
class MailService {
private $smtp_allowed;
......@@ -22,39 +24,40 @@ class MailService {
function send($to, $subject, $body, $msgKey = null) {
if ($this->smtp_allowed == true && $this->canSendMsg($msgKey)) {
mb_internal_encoding('UTF-8');
// Build headers
$subject = mb_encode_mimeheader(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'), 'UTF-8', 'B', "\n", 9);
$mail = new PHPMailer(true);
$mail->isSMTP();
$encoded_app = mb_encode_mimeheader(NOMAPPLICATION, 'UTF-8', 'B', "\n", 6);
$size_encoded_app = (6 + strlen($encoded_app)) % 75;
$size_admin_email = strlen(ADRESSEMAILADMIN);
// From
$mail->FromName = NOMAPPLICATION;
$mail->From = ADRESSEMAILADMIN;
if ($this->isValidEmail(ADRESSEMAILREPONSEAUTO)) {
$mail->addReplyTo(ADRESSEMAILREPONSEAUTO);
}
if (($size_encoded_app + $size_admin_email + 9) > 74) {
$folding = "\n";
} else {
$folding = '';
};
// To
$mail->addAddress($to);
$from = sprintf("From: %s%s <%s>\n", $encoded_app, $folding, ADRESSEMAILADMIN);
// Subject
$mail->Subject = $subject;
$headers = $from;
$headers .= 'Reply-To: ' . ADRESSEMAILREPONSEAUTO . "\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "Auto-Submitted:auto-generated\n";
$headers .= 'Return-Path: <>';
// Bodies
$body = $body . ' <br/><br/>' . __('Mail', 'Thanks for your trust.') . ' <br/>' . NOMAPPLICATION . ' <hr/>' . __('Mail', 'FOOTER');
$mail->isHTML(true);
$mail->msgHTML($body, ROOT_DIR, true);
// Build body
$body = $body . '<br/><br/>' . __('Mail', 'Thanks for your trust.') . '<br/>' . NOMAPPLICATION . '<hr/>' . __('Mail', 'FOOTER');
// Build headers
$mail->CharSet = 'UTF-8';
$mail->addCustomHeader('Auto-Submitted', 'auto-generated');
$mail->addCustomHeader('Return-Path', '<>');
// Send mail
$mail->send();
// Log
$this->logService->log('MAIL', 'Mail sent to: ' . $to . ', key: ' . $msgKey);
$this->sendMail($to, $subject, $body, $msgKey, $headers);
// Store the mail sending date
$_SESSION[self::MAILSERVICE_KEY][$msgKey] = time();
}
}
......@@ -69,17 +72,5 @@ class MailService {
return !isset($_SESSION[self::MAILSERVICE_KEY][$msgKey]) || time() - $_SESSION[self::MAILSERVICE_KEY][$msgKey] > self::DELAY_BEFORE_RESEND;
}
private function sendMail($to, $subject, $body, $msgKey, $headers) {
mail($to, $subject, $body, $headers, '');
// Log
$this->logService->log('MAIL', 'Mail sent to: ' . $to . ', key: ' . $msgKey);
// Store the mail sending date
$_SESSION[self::MAILSERVICE_KEY][$msgKey] = time();
}
}
\ No newline at end of file
......@@ -11,7 +11,8 @@
"require": {
"smarty/smarty": "3.1.21",
"o80/i18n": "dev-develop"
"o80/i18n": "dev-develop",
"phpmailer/phpmailer": "~5.2"
},
"require-dev": {
......
This diff is collapsed.
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