Projekt

Allgemein

Profil

GX-Bug #42563 » MailerAdapter.inc.php

/GXEngine/Classes/CoreServices/Email/ - Till Tepelmann, 17.07.2015 16:48

 
1
<?php
2
/* --------------------------------------------------------------
3
   MailerAdapter.inc.php 2015-02-05 gm
4
   Gambio GmbH
5
   http://www.gambio.de
6
   Copyright (c) 2015 Gambio GmbH
7
   Released under the GNU General Public License (Version 2)
8
   [http://www.gnu.org/licenses/gpl-2.0.html]
9
   --------------------------------------------------------------
10
*/
11

    
12
MainFactory::load_class('MailerAdapterInterface');
13

    
14
/**
15
 * Class MailerAdapter
16
 *
17
 * This class provides a communication layer with the external mailing library
18
 * in order to isolate the library-specific code.
19
 *
20
 * @category System
21
 * @package  Email
22
 */
23
class MailerAdapter implements MailerAdapterInterface
24
{
25
	/**
26
	 * @var PHPMailer
27
	 */
28
	protected $mailer;
29

    
30

    
31
	/**
32
	 * Class Constructor
33
	 *
34
	 * @param PHPMailer $mailer
35
	 */
36
	public function __construct(PHPMailer $mailer)
37
	{
38
		$this->mailer = $mailer;
39
	}
40

    
41

    
42
	/**
43
	 * Send a single email.
44
	 *
45
	 * @param EmailInterface $email Contains email information.
46
	 *
47
	 * @return EmailInterface Returns the email
48
	 * @throws Exception If mailer library fails to send the email.
49
	 */
50
	public function send(EmailInterface $email)
51
	{
52
		$mail = clone $this->mailer;
53

    
54
		// Set Email Sender Contact
55
		$mail->From     = (string)$email->getSender()->getEmailAddress();
56
		$mail->FromName = (string)$email->getSender()->getContactName();
57

    
58
		// Set Email Recipient Contact
59
		$mail->addAddress((string)$email->getRecipient()->getEmailAddress(),
60
		                  (string)$email->getRecipient()->getContactName());
61

    
62
		// Set Email Reply To Contact
63
		if($email->getReplyTo() !== null)
64
		{
65
			$mail->addReplyTo((string)$email->getReplyTo()->getEmailAddress(),
66
			                  (string)$email->getReplyto()->getContactName());
67
		}
68

    
69
		// Set Email BCC Contacts
70
		foreach($email->getBcc()->getArray() as $contact)
71
		{
72
			$mail->addBCC((string)$contact->getEmailAddress(), (string)$contact->getContactName());
73
		}
74

    
75
		// Set Email CC Contacts
76
		foreach($email->getBcc()->getArray() as $contact)
77
		{
78
			$mail->addCC((string)$contact->getEmailAddress(), (string)$contact->getContactName());
79
		}
80

    
81
		// Set Email Attachments
82
		foreach($email->getAttachments()->getArray() as $attachment)
83
		{
84
			$attachmentPath = (string)$attachment->getPath();
85
			if(!file_exists($attachmentPath) || !is_file($attachmentPath))
86
			{
87
				throw new AttachmentNotFoundException('Attachment file does not exist or is not a file: '
88
				                                      . $attachmentPath, $attachmentPath);
89
			}
90

    
91
			$mail->addAttachment((string)$attachment->getPath(), (string)$attachment->getName());
92
		}
93

    
94
		// Set Email Subject and Content
95
		$mail->Subject = (string)$email->getSubject();
96
        if(EMAIL_USE_HTML == 'true')
97
        {
98
            $mail->IsHTML(true);
99
            $mail->Body    = (string)$email->getContentHtml();
100
            $mail->AltBody = (string)$email->getContentPlain();
101
        }
102
        else 
103
        {
104
            $mail->IsHTML(false);
105
            $mail->Body    = (string)$email->getContentPlain();
106

    
107
        }
108
		
109

    
110
		// Empty mail body validation check. PHPMailer will not send mails without content.
111
		if($mail->Body === '')
112
		{
113
			$mail->Body = PHP_EOL;
114
		}
115

    
116
		// Send Email
117
		if(!$mail->send())
118
		{
119
			throw new Exception('Mailer library could not send email: ' . $mail->ErrorInfo);
120
		}
121
	}
122
}
(2-2/2)