Skip to content
Snippets Groups Projects
SendMail.php 2.53 KiB
Newer Older
<?php defined('SYSPATH') || die('No direct script access.');

require(DOCROOT.'/modules/mail/vendor/PHPMailer/PHPMailerAutoload.php');

class SendMail {
    /**
     * @param string $subject
     * @param string $body
     * @param string $sendToEmail
     * @param string $sendToName
     * @param string $replyToEmail (optional)
     * @param string $replayToName (optional)
     */
    public static function send($subject, $body
                                , $sendToEmail, $sendToName
                                , $replyToEmail = null, $replyToNameShort = null, $replayToName = null, $hasImage = false, $imgFileName = null) {
        $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->FromName = $replyToNameShort;
        $mail->addAddress($sendToEmail, $sendToName);     // Add a recipient
        $mail->addCC($replyToEmail, $replayToName);
        //$mail->addAddress('ellen@example.com');               // Name is optional

        if ($replyToEmail != null)
        {
            $mail->addReplyTo($replyToEmail, $replayToName);
        }
        //$mail->addCC('cc@example.com');
        //$mail->addBCC('bcc@example.com');

        if ($hasImage) {
            $mail->addAttachment(DOCROOT . '/support/img/' . $imgFileName . '.jpg');         // Add attachments
        }
        //$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');
        }
    }
}