PluginProbe
SendWP / 1.2.8
SendWP v1.2.8
trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1 1.2.10 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.4.4 1.4.5 1.4.6 1.4.8
sendwp / includes / class.mailer.php

class.mailer.php in SendWP 1.2.8, at includes/class.mailer.php

167 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // @codingStandardsIgnoreLine
2
3 namespace SendWP;
4
5 use SendWP\API\Request;
6
7 /**
8 * A decorater for sending API based transactional email.
9 *
10 * @TODO Maybe extract formatting methods.
11 */
12 class Mailer implements MailerInterface {
13 protected $phpmailer;
14 protected $request;
15
16 /**
17 * Create a factory
18 *
19 * @param Object $phpmailer
20 *
21 * @return Object
22 */
23 public static function factory( &$phpmailer ) {
24 $request = \SendWP\API\Request::create( 'postmaster' );
25 $phpmailer = new self( $phpmailer, $request );
26
27 return $phpmailer;
28 }
29
30 /**
31 * Constructor
32 *
33 * @param Object $phpmailer
34 * @param Request $request
35 *
36 * @return Void
37 */
38 public function __construct( $phpmailer, Request $request ) {
39 $this->phpmailer = $phpmailer;
40 $this->request = $request;
41 }
42
43 /**
44 * Check for property/method in $phpmailer.
45 *
46 * @param String $name
47 *
48 * @return String
49 */
50 public function __get( $name ) {
51 if ( property_exists( $this->phpmailer, $name ) ) {
52 return $this->phpmailer->$name;
53 }
54
55 return '';
56 }
57
58 /**
59 * Caller method
60 *
61 * @param String $name
62 * @param Array $arguments
63 *
64 * @return Self|Null
65 */
66 public function __call( $name, $arguments ) {
67 if ( method_exists( $this->phpmailer, $name ) ) {
68 return call_user_func( array( $this->phpmailer, $name ), $arguments );
69 }
70
71 return null;
72 }
73
74 /**
75 * Get attachments
76 *
77 * @return Array
78 */
79 public function get_attachments() {
80 $attachments = $this->phpmailer->get_attachments();
81 $attachments = array_map( array( $this, 'formatAttachment' ), $attachments );
82
83 return $attachments;
84 }
85
86 /**
87 * Send email
88 *
89 * @return Bool
90 */
91 public function send() {
92 $to_emails = array_map( array( $this, 'formatEmails' ), $this->getToAddresses() );
93 $cc_emails = array_map( array( $this, 'formatEmails' ), $this->getCcAddresses() );
94 $bcc_emails = array_map( array( $this, 'formatEmails' ), $this->getBccAddresses() );
95
96 $args = array(
97 'body' => array(
98 'to' => json_encode( (array) $to_emails ),
99 'from' => $this->From, // @codingStandardsIgnoreLine
100 'from_name' => $this->FromName, // @codingStandardsIgnoreLine
101 'subject' => $this->Subject, // @codingStandardsIgnoreLine
102 ),
103 );
104
105 if ( 'text/html' === $this->ContentType ) { // @codingStandardsIgnoreLine
106 $args['body']['body'] = $this->Body; // @codingStandardsIgnoreLine
107 $args['body']['altbody'] = $this->AltBody; // @codingStandardsIgnoreLine
108 } else {
109 $args['body']['altbody'] = $this->Body; // @codingStandardsIgnoreLine
110 }
111
112 if ( $reply_to = $this->getReplyToAddresses() ) { // @codingStandardsIgnoreLine
113 /**
114 * $reply_to is an array of arrays.
115 * [ [ $address, $name ], [...] ]
116 * To get a string value for the address, we need to reset it twice.
117 */
118 $reply_to = reset( $reply_to );
119 $args['body']['reply_to'] = reset( $reply_to );
120 }
121
122 if ( ! empty( $cc_emails ) ) {
123 $args['body']['cc'] = json_encode( (array) $cc_emails );
124 }
125
126 if ( ! empty( $bcc_emails ) ) {
127 $args['body']['bcc'] = json_encode( (array) $bcc_emails );
128 }
129
130 $attachments = $this->get_attachments();
131
132 if ( $attachments ) {
133 $args['body']['attachments'] = $attachments;
134 }
135
136 // Response is empty...
137 $response = $this->request->post( $args );
138
139 return true; // Sent by the Service.
140 }
141
142 /**
143 * Format email addresses
144 *
145 * @param Array $emails
146 *
147 * @return Array
148 */
149 protected function formatEmails( $emails ) {
150 return reset( $emails );
151 }
152
153 /**
154 * Format attachments
155 *
156 * @param Array $attachments
157 *
158 * @return Array
159 */
160 protected function formatAttachment( $attachment ) {
161 return array(
162 'filename' => $attachment[1], // $filename per PHPMailer docs.
163 'filedata' => file_get_contents( $attachment[0] ), // $path per PHPMailer docs.
164 );
165 }
166 }
167