Skip to content
Snippets Groups Projects
Commit 17b81111 authored by Artem Konenko's avatar Artem Konenko
Browse files

#207 Export SendMail class to work with SMTP project wide

parent 82de2925
Branches
Tags
No related merge requests found
<?php defined('SYSPATH') OR die('No direct access allowed.');
return array
(
'default' => array(
'host' => 'localhost', // Specify SMTP server
'port' => '25', // TCP port to connect to
'username' => NULL, // SMTP username
'password' => NULL, // SMTP password
'SMTPAuth' => false, // Enable SMTP authentication
'SMTPSecure' => NULL, // Enable TLS encryption, `ssl` also accepted
'from' => 'no_reply@grade.sfedu.ru',
'fromName' => 'Grade System (Сервис БРС)',
'enableDebug' => false // Enable verbose debug output
),
);
<?php
require(DOCROOT.'/modules/mail/vendor/PHPMailer/PHPMailerAutoload.php');
function gradeSendMail($subject, $body, $sendToEmail, $sendToName) {
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'class.mmcs.sfedu.ru'; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Username = NULL; // SMTP username
$mail->Password = NULL; // SMTP password
$mail->SMTPSecure = NULL; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->From = 'no_reply@grade.sfedu.ru';
$mail->FromName = 'Grade System (Сервис БРС)';
$mail->addAddress($sendToEmail, $sendToName); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->ContentType = 'text/plain';
$mail->CharSet = 'utf-8';
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
Log::instance()->add(Log::NOTICE, 'Message could not be sent: '.$mail->ErrorInfo);
//echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
Log::instance()->add(Log::NOTICE, 'Message has been sent');
}
}
class Account
{
private static function checkTokenLifetime($creationDate) {
......@@ -81,15 +40,7 @@ class Account
$twig->EMail = $email;
$twig->Subject = $subject;
/* Kohana mailer is not working
$status = Mailer::factory()
->headers('Content-Type', 'text/html; charset=utf-8')
->subject($subject)
->in_reply_to(Mailer::message_id())
->from('no-reply@rating.mmcs.sfedu.ru')
->body($twig->render())
->send($email);*/
gradeSendMail($subject, $twig->render(), $email, $UserFullName);
SendMail::send($subject, $twig->render(), $email, $UserFullName);
}
// remind password
......
......@@ -49,13 +49,7 @@ class Controller_Handler_RequestsProcessing extends Controller_Handler {
'User' => $this->user,
])->render();
$headers = [
'From: RatingSystem@no-reply.mmcs.sfedu.ru',
'Reply-To: ' . $this->user->EMail,
// 'Content-Type: text/html; charset=UTF-8',
];
mail($to, $subject, $message, implode("\n", $headers));
SendMail::send($subject, $message, $to, $to);
$data['success'] = ($ticket > 0);
$this->response->body(json_encode($data));
......
<?php defined('SYSPATH') or die('No direct script access.');
require(DOCROOT.'/modules/mail/vendor/PHPMailer/PHPMailerAutoload.php');
class SendMail {
public static function send($subject, $body, $sendToEmail, $sendToName) {
$config = Kohana::$config->load('email.default');
$mail = new PHPMailer;
if ( $config['enableDebug'] ) {
$mail->SMTPDebug = 3;
}
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $config['host'];
$mail->SMTPAuth = $config['SMTPAuth'];
$mail->Username = $config['username'];
$mail->Password = $config['password'];
$mail->SMTPSecure = $config['SMTPSecure'];
$mail->Port = $config['port'];
$mail->From = $config['from'];
$mail->FromName = $config['fromName'];
$mail->addAddress($sendToEmail, $sendToName); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->ContentType = 'text/plain';
$mail->CharSet = 'utf-8';
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
Log::instance()->add(Log::NOTICE, 'Message could not be sent: '.$mail->ErrorInfo);
} else {
Log::instance()->add(Log::NOTICE, 'Message has been sent');
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment