Move mail sending to class \Framadate\Service\MailService

This commit is contained in:
Olivier PEREZ 2014-12-23 00:58:00 +01:00
parent d942f82b0a
commit 8109b11b70
3 changed files with 87 additions and 8 deletions

View File

@ -0,0 +1,49 @@
<?php
namespace Framadate\Services;
class MailService {
private $smtp_allowed;
function __construct($smtp_allowed) {
$this->smtp_allowed = $smtp_allowed;
}
public function isValidEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
function send($to, $subject, $body, $param = '') {
if($this->smtp_allowed == true) {
mb_internal_encoding('UTF-8');
$subject = mb_encode_mimeheader(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'), 'UTF-8', 'B', "\n", 9);
$encoded_app = mb_encode_mimeheader(NOMAPPLICATION, 'UTF-8', 'B', "\n", 6);
$size_encoded_app = (6 + strlen($encoded_app)) % 75;
$size_admin_email = strlen(ADRESSEMAILADMIN);
if (($size_encoded_app + $size_admin_email + 9) > 74) {
$folding = "\n";
} else {
$folding = '';
};
$from = sprintf("From: %s%s <%s>\n", $encoded_app, $folding, ADRESSEMAILADMIN);
$headers = $from;
$headers .= 'Reply-To: ' . ADRESSEMAILREPONSEAUTO . "\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "Auto-Submitted:auto-generated\n";
$headers .= 'Return-Path: <>';
$body = html_entity_decode($body, ENT_QUOTES, 'UTF-8') . _('\n--\n\n« La route est longue, mais la voie est libre… »\nFramasoft ne vit que par vos dons (déductibles des impôts).\nMerci d\'avance pour votre soutien http://soutenir.framasoft.org.');
mail($to, $subject, $body, $headers, $param);
}
}
}

View File

@ -45,6 +45,10 @@ class Utils
return (USE_REMOTE_USER && isset($_SERVER['REMOTE_USER'])) || isset($_SESSION['nom']);
}
/**
* @param string $title
* @deprecated
*/
public static function print_header($title = '')
{
global $lang;
@ -87,6 +91,7 @@ class Utils
*
* @param string $email Email address to check
* @return bool True if valid. False if not valid.
* @deprecated
*/
public static function isValidEmail($email)
{
@ -96,7 +101,7 @@ class Utils
/**
* Envoi un courrier avec un codage correct de To et Subject
* Les en-têtes complémentaires ne sont pas gérés
*
* @deprecated
*/
public static function sendEmail( $to, $subject, $body, $headers='', $param='')
{
@ -175,8 +180,7 @@ class Utils
* Completly delete data about the given poll
* TODO Move this function to FramaDB
*/
public static function removeSondage($poll_id)
{
public static function removeSondage($poll_id) {
global $connect;
$prepared = $connect->prepare('DELETE FROM sujet_studs WHERE id_sondage = ?');
@ -195,7 +199,7 @@ class Utils
/**
* Clean old poll (end_date < now).
* TODO Move this function to FramaDB
* TODO Move this function to PurgePollService
*/
public static function cleaningOldPolls($log_txt) {
global $connect;
@ -216,8 +220,7 @@ class Utils
* This method pretty prints an object to the page framed by pre tags.
* @param mixed $object The object to print.
*/
public static function debug($object)
{
public static function debug($object) {
echo '<pre>';
print_r($object);
echo '</pre>';

View File

@ -18,6 +18,7 @@
*/
use Framadate\Services\PollService;
use Framadate\Services\InputService;
use Framadate\Services\MailService;
use Framadate\Message;
use Framadate\Utils;
@ -25,6 +26,7 @@ include_once __DIR__ . '/app/inc/init.php';
/* Variables */
/* --------- */
$poll_id = null;
$poll = null;
$message = null;
@ -35,6 +37,31 @@ $editingVoteId = 0;
$pollService = new PollService($connect);
$inputService = new InputService();
$mailService = new MailService($config['use_smtp']);
/* Functions */
/*-----------*/
/**
* Send a notification to the poll admin to notify him about an update.
*
* @param $poll Object The poll
* @param $mailService MailService The mail service
*/
function sendUpdateNotification($poll, $mailService) {
if ($poll->receiveNewVotes && !isset($_SESSION['mail_sent'][$poll->poll_id])) {
$subject = '[' . NOMAPPLICATION . '] ' . _('Poll\'s participation') . ' : ' . html_entity_decode($poll->title, ENT_QUOTES, 'UTF-8');
$message = html_entity_decode('"$nom" ', ENT_QUOTES, 'UTF-8') .
_('has filled a line.\nYou can find your poll at the link') . " :\n\n" .
Utils::getUrlSondage($poll->admin_poll_id, true) . " \n\n" .
_('Thanks for your confidence.') . "\n" . NOMAPPLICATION;
$mailService->send($poll->admin_mail, $subject, $message);
$_SESSION["mail_sent"][$poll->poll_id] = true;
}
}
/* PAGE */
/* ---- */
@ -79,7 +106,7 @@ if (!empty($_POST['save'])) { // Save edition of an old vote
$result = $pollService->updateVote($poll_id, $editedVote, $choices);
if ($result) {
$message = new Message('success', _('Update vote successfully.'));
// TODO Send mail to notify the poll admin
sendUpdateNotification($poll, $mailService);
} else {
$message = new Message('danger', _('Update vote failed.'));
}
@ -100,7 +127,7 @@ if (!empty($_POST['save'])) { // Save edition of an old vote
$result = $pollService->addVote($poll_id, $name, $choices);
if ($result) {
$message = new Message('success', _('Update vote successfully.'));
// TODO Send mail to notify the poll admin
sendUpdateNotification($poll, $mailService);
} else {
$message = new Message('danger', _('Update vote failed.'));
}