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  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 
<?php
/* --------------------------------------------------------------
   MailerAdapter.inc.php 2015-07-20 gm
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2015 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

MainFactory::load_class('MailerAdapterInterface');

/**
 * Class MailerAdapter
 *
 * This class provides a communication layer with the external mailing library
 * in order to isolate the library-specific code.
 *
 * @category System
 * @package  Email
 */
class MailerAdapter implements MailerAdapterInterface
{
    /**
     * PHPMailer Instance
     *
     * @var PHPMailer
     */
    protected $mailer;
    
    /**
     * Used for punycode encoding.
     *
     * @var TrueBV\Punycode
     */
    protected $punycode;
    
    /**
     * @var bool
     */
    protected $useHtmlMail;
    
    
    /**
     * Class Constructor
     *
     * @param PHPMailer       $mailer   PHPMailer instance.
     * @param TrueBV\Punycode $punycode Punycode encoder instance.
     * @param \BoolType       $useHtmlMail
     */
    public function __construct(PHPMailer $mailer, TrueBV\Punycode $punycode, \BoolType $useHtmlMail = null)
    {
        $this->mailer      = $mailer;
        $this->punycode    = $punycode;
        $this->useHtmlMail = $useHtmlMail ? $useHtmlMail->asBool() : false;
    }
    
    
    /**
     * Sends a single email.
     *
     * @param EmailInterface $email Contains email information.
     *
     * @throws Exception If mailer library fails to send the email.
     */
    public function send(EmailInterface $email)
    {
        $mail = clone $this->mailer;
        
        // Set Email Sender Contact
        $mail->From     = $this->punycode->encode((string)$email->getSender()->getEmailAddress());
        $mail->FromName = (string)$email->getSender()->getContactName();
        
        // Set Email Recipient Contact
        $mail->addAddress($this->punycode->encode((string)$email->getRecipient()->getEmailAddress()),
                          (string)$email->getRecipient()->getContactName());
        
        // Set Email Reply To Contact
        if($email->getReplyTo() !== null)
        {
            $mail->addReplyTo($this->punycode->encode((string)$email->getReplyTo()->getEmailAddress()),
                              (string)$email->getReplyto()->getContactName());
        }
        
        // Set Email BCC Contacts
        foreach($email->getBcc()->getArray() as $contact)
        {
            $mail->addBCC($this->punycode->encode((string)$contact->getEmailAddress()),
                          (string)$contact->getContactName());
        }
        
        // Set Email CC Contacts
        foreach($email->getBcc()->getArray() as $contact)
        {
            $mail->addCC($this->punycode->encode((string)$contact->getEmailAddress()),
                         (string)$contact->getContactName());
        }
        
        // Set Email Attachments
        foreach($email->getAttachments()->getArray() as $attachment)
        {
            $attachmentPath = (string)$attachment->getPath();
            if(!file_exists($attachmentPath) || !is_file($attachmentPath))
            {
                throw new AttachmentNotFoundException('Attachment file does not exist or is not a file: '
                                                      . $attachmentPath, $attachmentPath);
            }
            
            $mail->addAttachment((string)$attachment->getPath(), (string)$attachment->getName());
        }
        
        // Set Email Subject and Content
        $mail->Subject = (string)$email->getSubject();
        
        if($this->useHtmlMail)
        {
            $mail->IsHTML(true);
            $mail->Body    = (string)$email->getContentHtml();
            $mail->AltBody = (string)$email->getContentPlain();
        }
        else
        {
            $mail->isHTML(false);
            $mail->Body = (string)$email->getContentPlain();
        }
        
        // Empty mail body validation check. PHPMailer will not send mails without content.
        if($mail->Body === '')
        {
            $mail->Body = PHP_EOL;
        }
        
        // Send Email
        if(!$mail->send())
        {
            throw new Exception('Mailer library could not send email: ' . $mail->ErrorInfo);
        }
    }
}