Skip to content
Browse files

Merge branch 'master' of github.com:leblanc-simon/OpenHarcelement

  • Loading branch information...
2 parents 5aafa48 + af315a2 commit 44845dab15680d7ae6d2b8c3b2fbb773330c1891 @leblanc-simon committed
Showing with 61 additions and 9 deletions.
  1. +3 −0 .gitmodules
  2. +50 −8 app/Task.php
  3. +3 −0 config/config.inc.php
  4. +4 −1 config/include.php
  5. +1 −0 vendor/swiftmailer
View
3 .gitmodules
@@ -0,0 +1,3 @@
+[submodule "vendor/swiftmailer"]
+ path = vendor/swiftmailer
+ url = git://github.com/swiftmailer/swiftmailer.git
View
58 app/Task.php
@@ -90,7 +90,12 @@ public static function alertCreator(Harcelement $harcelement)
);
$message = str_replace($search, $replace, $email_tpl);
- mail($harcelement->getEmail(), 'Confirmation de votre harcelement', $message, Task::getHeader($harcelement->getName().' <'.$harcelement->getEmail().'>'));
+ $to = array($harcelement->getEmail() => $harcelement->getName());
+ $from = Config::get('from');
+ $subject = 'Confirmation de votre harcèlement';
+ $replyto = null;
+
+ return Task::processMail($to, $from, $subject, $message, $replyto);
}
@@ -103,7 +108,13 @@ public static function alertCreator(Harcelement $harcelement)
*/
private static function sendMail(Harcelement $harcelement)
{
- if (mail($harcelement->getEmailVictim(), $harcelement->getSubject(), $harcelement->getMessage(), Task::getHeader($harcelement->getName().' <'.$harcelement->getEmail().'>'))) {
+ $to = $harcelement->getEmailVictim();
+ $from = Config::get('from');
+ $subject = $harcelement->getSubject();
+ $message = $harcelement->getMessage();
+ $replyto = array($harcelement->getEmail() => $harcelement->getName());
+
+ if (Task::processMail($to, $from, $subject, $message, $replyto)) {
$harcelement->setNumberSend($harcelement->getNumberSend() + 1);
if ($harcelement->getNext() === null) {
$next = new DateTime();
@@ -117,17 +128,48 @@ private static function sendMail(Harcelement $harcelement)
/**
- * Méthode permettant d'obtenir le bon header pour les mails envoyés
+ * Méthode permettant d'envoyer un mail via SwiftMailer
*
- * @param string $return_path L'adresse sur laquelle il faudra répondre
+ * @param string|array $to L'adresse mail vers laquelle on envoi le mail
+ * @param string|array $from L'adresse mail qui envoi le mail
+ * @param string $subject Le sujet du message
+ * @param string $message Le message
+ * @param string|array $replyto L'adresse mail où répondre
+ * @return bool Vrai si le mail a été envoyé, faux sinon
* @access private
* @static
*/
- private static function getHeader($return_path)
+ private static function processMail($to, $from, $subject, $message, $replyto = null)
{
- $header_mail = 'Return-Path: '.$return_path."\r\n".
- 'Content-Type: text/plain; charset=UTF-8;'."\r\n";
+ try {
+ $transport_bin = Config::get('sendmail_bin');
+ if ($transport_bin === null) {
+ $transport = Swift_MailTransport::newInstance();
+ } else {
+ $transport = Swift_SendmailTransport::newInstance($transport_bin);
+ }
+
+ $mailer = Swift_Mailer::newInstance($transport);
+
+ $message = Swift_Message::newInstance($subject)
+ ->setFrom($from)
+ ->setTo($to)
+ ->setBody($message);
+
+ if ($replyto !== null) {
+ $message->setReplyTo($replyto);
+ }
+
+ $result = $mailer->send($message);
+ } catch (Exception $e) {
+ // Erreur lors de l'envoi du mail
+ $result = 0;
+ }
+
+ if ($result > 0) {
+ return true;
+ }
- return $header_mail;
+ return false;
}
}
View
3 config/config.inc.php
@@ -37,6 +37,9 @@
'PT6H' => '6 heures',
'P1D' => '1 jour',
),
+ 'from' => array('openharcelement@leblanc-simon.fr' => 'OpenHarcelement'),
+ // Uncomment this line for don't use mail() function
+ //'sendmail_bin' => '/usr/sbin/sendmail',
'email_tpl' => <<<EOF
Bonjour %%name%%,
View
5 config/include.php
@@ -31,6 +31,7 @@
define('LIB_DIR', __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'lib');
define('CONFIG_DIR', __DIR__);
define('APP_DIR', __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'app');
+define('VENDOR_DIR', __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'vendor');
require_once LIB_DIR.DIRECTORY_SEPARATOR.'Config.php';
require_once LIB_DIR.DIRECTORY_SEPARATOR.'Connection.php';
@@ -40,4 +41,6 @@
require_once CONFIG_DIR.DIRECTORY_SEPARATOR.'config.inc.php';
require_once APP_DIR.DIRECTORY_SEPARATOR.'Api.php';
-require_once APP_DIR.DIRECTORY_SEPARATOR.'Task.php';
+require_once APP_DIR.DIRECTORY_SEPARATOR.'Task.php';
+
+require_once VENDOR_DIR.DIRECTORY_SEPARATOR.'swiftmailer'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'swift_required.php';
1 vendor/swiftmailer
@@ -0,0 +1 @@
+Subproject commit 3ff99a604dcfb13497e83ede3e62f1a14f1daf60

0 comments on commit 44845da

Please sign in to comment.
Something went wrong with that request. Please try again.