Classes

File io/mail/MailToPHPMailerConverter.class.php

File io/mail/MailToPHPMailerConverter.class.php

 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 
<?php
/**
 * @package     IO
 * @subpackage  Mail
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Benoit SAUTEL <ben.popeye@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2019 03 27
 * @since       PHPBoost 3.0 - 2010 03 10
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

include_once PATH_TO_ROOT . '/kernel/lib/php/phpmailer/src/Exception.php';
include_once PATH_TO_ROOT . '/kernel/lib/php/phpmailer/src/PHPMailer.php';
include_once PATH_TO_ROOT . '/kernel/lib/php/phpmailer/src/SMTP.php';
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

class MailToPHPMailerConverter
{
    /**
     * @var PHPMailer
     */
    private $mailer;
    /**
     * @var Mail
     */
    private $mail_to_send;

    /**
     * @param Mail $mail
     * @return PHPMailer
     */
    public function convert(Mail $mail)
    {
        $this->mail_to_send = $mail;
        $this->mailer = new PHPMailer(true);
        $this->mailer->CharSet = 'utf-8';
        $this->mailer->SMTPAutoTLS = false;
        $this->convert_mail();
        return $this->mailer;
    }

    private function convert_mail()
    {
        foreach ($this->mail_to_send->get_recipients() as $recipient => $name)
        {
            $this->mailer->AddAddress($recipient, $name);
        }

        // cc
        foreach ($this->mail_to_send->get_cc_recipients() as $recipient => $name)
        {
            $this->mailer->AddCC($recipient, $name);
        }

        // bcc
        foreach ($this->mail_to_send->get_bcc_recipients() as $recipient => $name)
        {
            $this->mailer->AddBCC($recipient, $name);
        }

        // from
        $this->mailer->SetFrom($this->mail_to_send->get_sender_mail(), $this->mail_to_send->get_sender_name());
        $this->mailer->AddReplyTo($this->mail_to_send->get_reply_to_mail() ? $this->mail_to_send->get_reply_to_mail() : $this->mail_to_send->get_sender_mail(), $this->mail_to_send->get_reply_to_name() ? $this->mail_to_send->get_reply_to_name() : $this->mail_to_send->get_sender_name());

        $this->mailer->Subject = $this->mail_to_send->get_subject();

        // content
        $this->convert_content();
    }

    private function convert_content()
    {
        if ($this->mail_to_send->is_html())
        {
            $this->mailer->MsgHTML($this->mail_to_send->get_content());
        }
        else
        {
            $this->mailer->Body = $this->mail_to_send->get_content();
        }
    }
}
?>