1 <?php
2 3 4 5 6 7 8 9 10
11
12 MainFactory::load_class('MailerAdapterInterface');
13
14 15 16 17 18 19 20 21 22
23 class MailerAdapter implements MailerAdapterInterface
24 {
25 26 27 28
29 protected $mailer;
30
31
32 33 34 35 36
37 public function __construct(PHPMailer $mailer)
38 {
39 $this->mailer = $mailer;
40 }
41
42
43 44 45 46 47 48 49
50 public function send(EmailInterface $email)
51 {
52 $mail = clone $this->mailer;
53
54
55 $mail->From = (string)$email->getSender()->getEmailAddress();
56 $mail->FromName = (string)$email->getSender()->getContactName();
57
58
59 $mail->addAddress((string)$email->getRecipient()->getEmailAddress(),
60 (string)$email->getRecipient()->getContactName());
61
62
63 if($email->getReplyTo() !== null)
64 {
65 $mail->addReplyTo((string)$email->getReplyTo()->getEmailAddress(),
66 (string)$email->getReplyto()->getContactName());
67 }
68
69
70 foreach($email->getBcc()->getArray() as $contact)
71 {
72 $mail->addBCC((string)$contact->getEmailAddress(), (string)$contact->getContactName());
73 }
74
75
76 foreach($email->getBcc()->getArray() as $contact)
77 {
78 $mail->addCC((string)$contact->getEmailAddress(), (string)$contact->getContactName());
79 }
80
81
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
95 $mail->Subject = (string)$email->getSubject();
96
97 if(EMAIL_USE_HTML == 'true')
98 {
99 $mail->IsHTML(true);
100 $mail->Body = (string)$email->getContentHtml();
101 $mail->AltBody = (string)$email->getContentPlain();
102 }
103 else
104 {
105 $mail->isHTML(false);
106 $mail->Body = (string)$email->getContentPlain();
107 }
108
109
110 if($mail->Body === '')
111 {
112 $mail->Body = PHP_EOL;
113 }
114
115
116 if(!$mail->send())
117 {
118 throw new Exception('Mailer library could not send email: ' . $mail->ErrorInfo);
119 }
120 }
121 }