PostmanMessage.php 17 KB
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
<?php
if (! class_exists ( "PostmanMessage" )) {
	
	require_once 'PostmanEmailAddress.php';
	
	/**
	 * This class knows how to interface with Wordpress
	 * including loading/saving to the database.
	 *
	 * The various Transports available:
	 * http://framework.zend.com/manual/current/en/modules/zend.mail.smtp.options.html
	 *
	 * @author jasonhendriks
	 *        
	 */
	class PostmanMessage {
		const EOL = "\r\n";
		
		// logger for all concrete classes - populate with setLogger($logger)
		protected $logger;
		
		// set by the caller
		private $from;
		private $replyTo;
		private $toRecipients;
		private $ccRecipients;
		private $bccRecipients;
		private $subject;
		private $body;
		private $bodyTextPart;
		private $bodyHtmlPart;
		private $headers;
		private $attachments;
		private $date;
		private $messageId;
		
		// determined by the send() method
		private $isTextHtml;
		private $contentType;
		private $charset;
		
		//
		private $boundary;
		
		/**
		 * No-argument constructor
		 */
		function __construct() {
			$this->logger = new PostmanLogger ( get_class ( $this ) );
			$this->headers = array ();
			$this->toRecipients = array ();
			$this->ccRecipients = array ();
			$this->bccRecipients = array ();
		}
		
		/**
		 *
		 * @return boolean
		 */
		public function isBodyPartsEmpty() {
			return empty ( $this->bodyTextPart ) && empty ( $this->bodyHtmlPart );
		}
		
		/**
		 *
		 * @param PostmanModuleTransport $transport        	
		 */
		public function validate(PostmanModuleTransport $transport) {
			if ($transport->isEmailValidationSupported ()) {
				$this->internalValidate ();
			}
		}
		
		/**
		 * Create body parts based on content type
		 * MyMail creates its own body parts
		 */
		public function createBodyParts() {

			// modify the content-type to include the boundary
			if (false !== stripos ( $this->contentType, 'multipart' ) && ! empty ( $this->boundary )) {
				// Lines in email are terminated by CRLF ("\r\n") according to RFC2821
				$this->contentType = sprintf ( "%s;\r\n\t boundary=\"%s\"", $this->contentType, $this->getBoundary () );
			}
			
			//
			$body = $this->getBody ();
			$contentType = $this->getContentType ();
			// add the message content as either text or html
			if (empty ( $contentType ) || substr ( $contentType, 0, 10 ) === 'text/plain') {
				$this->logger->debug ( 'Creating text body part' );
				$this->setBodyTextPart ( $body );
			} else if (substr ( $contentType, 0, 9 ) === 'text/html') {
				$this->logger->debug ( 'Creating html body part' );
				$this->setBodyHtmlPart ( $body );
			} else if (substr ( $contentType, 0, 21 ) === 'multipart/alternative') {
				$this->logger->debug ( 'Adding body as multipart/alternative' );
				$arr = explode ( PHP_EOL, $body );
				$textBody = '';
				$htmlBody = '';
				$mode = '';
				foreach ( $arr as $s ) {
					$this->logger->trace ( 'mode: ' . $mode . ' bodyline: ' . $s );
					if (substr ( $s, 0, 25 ) === "Content-Type: text/plain;") {
						$mode = 'foundText';
					} else if (substr ( $s, 0, 24 ) === "Content-Type: text/html;") {
						$mode = 'foundHtml';
					} else if ($mode == 'textReading') {
						$textBody .= $s;
					} else if ($mode == 'htmlReading') {
						$htmlBody .= $s;
					} else if ($mode == 'foundText') {
						$trim = trim ( $s );
						if (empty ( $trim )) {
							$mode = 'textReading';
						}
					} else if ($mode == 'foundHtml') {
						$trim = trim ( $s );
						if (empty ( $trim )) {
							$mode = 'htmlReading';
						}
					}
				}
				$this->setBodyHtmlPart ( $htmlBody );
				$this->setBodyTextPart ( $textBody );
			} else {
				$this->logger->error ( 'Unknown content-type: ' . $contentType );
				$this->setBodyTextPart ( $body );
			}
		}
		
		/**
		 * Apply the WordPress filters to the email
		 */
		public function applyFilters() {
			if ($this->logger->isDebug ()) {
				$this->logger->debug ( 'Applying WordPress filters' );
			}
			
			/**
			 * Filter the email address to send from.
			 *
			 * @since 2.2.0
			 *       
			 * @param string $from_email
			 *        	Email address to send from.
			 */
			$filteredEmail = apply_filters ( 'wp_mail_from', $this->getFromAddress ()->getEmail () );
			if ($this->logger->isTrace ()) {
				$this->logger->trace ( 'wp_mail_from: ' . $filteredEmail );
			}
			if ($this->getFromAddress ()->getEmail () !== $filteredEmail) {
				$this->logger->debug ( sprintf ( 'Filtering From email address: before=%s after=%s', $this->getFromAddress ()->getEmail (), $filteredEmail ) );
				$this->getFromAddress ()->setEmail ( $filteredEmail );
			}
			
			/**
			 * Filter the name to associate with the "from" email address.
			 *
			 * @since 2.3.0
			 *       
			 * @param string $from_name
			 *        	Name associated with the "from" email address.
			 */
			$filteredName = apply_filters ( 'wp_mail_from_name', $this->getFromAddress ()->getName () );
			if ($this->logger->isTrace ()) {
				$this->logger->trace ( 'wp_mail_from_name: ' . $filteredName );
			}
			if ($this->getFromAddress ()->getName () !== $filteredName) {
				$this->logger->debug ( sprintf ( 'Filtering From email name: before=%s after=%s', $this->getFromAddress ()->getName (), $filteredName ) );
				$this->getFromAddress ()->setName ( $filteredName );
			}
			
			/**
			 * Filter the default wp_mail() charset.
			 *
			 * @since 2.3.0
			 *       
			 * @param string $charset
			 *        	Default email charset.
			 */
			$filteredCharset = apply_filters ( 'wp_mail_charset', $this->getCharset () );
			if ($this->logger->isTrace ()) {
				$this->logger->trace ( 'wp_mail_charset: ' . $filteredCharset );
			}
			if ($this->getCharset () !== $filteredCharset) {
				$this->logger->debug ( sprintf ( 'Filtering Charset: before=%s after=%s', $this->getCharset (), $filteredCharset ) );
				$this->setCharset ( $filteredCharset );
			}
			
			/**
			 * Filter the wp_mail() content type.
			 *
			 * @since 2.3.0
			 *       
			 * @param string $content_type
			 *        	Default wp_mail() content type.
			 */
			$filteredContentType = apply_filters ( 'wp_mail_content_type', $this->getContentType () );
			if ($this->logger->isTrace ()) {
				$this->logger->trace ( sprintf ( 'wp_mail_content_type: "%s"', $filteredContentType ) );
			}
			if ($this->getContentType () != $filteredContentType) {
				$this->logger->debug ( sprintf ( 'Filtering Content-Type: before=%s after=%s', $this->getContentType (), $filteredContentType ) );
				$this->setContentType ( $filteredContentType );
			}
			
			// Postman has it's own 'user override' filter
			$options = PostmanOptions::getInstance ();
			$forcedEmailAddress = $options->getMessageSenderEmail ();
			if ($options->isSenderEmailOverridePrevented () && $this->getFromAddress ()->getEmail () !== $forcedEmailAddress) {
				$this->logger->debug ( sprintf ( 'Forced From email address: before=%s after=%s', $this->getFromAddress ()->getEmail (), $forcedEmailAddress ) );
				$this->getFromAddress ()->setEmail ( $forcedEmailAddress );
			}
			$forcedEmailName = $options->getMessageSenderName ();
			if ($options->isSenderNameOverridePrevented () && $this->getFromAddress ()->getName () !== $forcedEmailName) {
				$this->logger->debug ( sprintf ( 'Forced From email name: before=%s after=%s', $this->getFromAddress ()->getName (), $forcedEmailName ) );
				$this->getFromAddress ()->setName ( $forcedEmailName );
			}
		}
		
		/**
		 * Check all email headers for errors
		 * Throw an exception if an error is found
		 */
		private function internalValidate() {
			// check the reply-to address for errors
			if (isset ( $this->replyTo )) {
				$this->getReplyTo ()->validate ( 'Reply-To' );
			}
			
			// check the from address for errors
			$this->getFromAddress ()->validate ( 'From' );
			
			// validate the To recipients
			foreach ( ( array ) $this->getToRecipients () as $toRecipient ) {
				$toRecipient->validate ( 'To' );
			}
			
			// validate the Cc recipients
			foreach ( ( array ) $this->getCcRecipients () as $ccRecipient ) {
				$ccRecipient->validate ( 'Cc' );
			}
			
			// validate the Bcc recipients
			foreach ( ( array ) $this->getBccRecipients () as $bccRecipient ) {
				$bccRecipient->validate ( 'Bcc' );
			}
		}
		
		/**
		 *
		 * @return PostmanEmailAddress
		 */
		public function getFromAddress() {
			return $this->from;
		}
		
		/**
		 * Get the charset, checking first the WordPress bloginfo, then the header, then the wp_mail_charset filter.
		 *
		 * @return string
		 */
		public function getCharset() {
			return $this->charset;
		}
		
		/**
		 * Set the charset
		 *
		 * @param unknown $charset        	
		 */
		public function setCharset($charset) {
			$this->charset = $charset;
		}
		
		/**
		 * Get the content type, checking first the header, then the wp_mail_content_type filter
		 *
		 * @return string
		 */
		public function getContentType() {
			return $this->contentType;
		}
		public function setContentType($contentType) {
			$this->contentType = $contentType;
		}
		/**
		 *
		 * @param unknown $recipients
		 *        	Array or comma-separated list of email addresses to send message.
		 * @throws Exception
		 */
		public function addTo($to) {
			$this->addRecipients ( $this->toRecipients, $to );
		}
		/**
		 *
		 * @param unknown $recipients
		 *        	Array or comma-separated list of email addresses to send message.
		 * @throws Exception
		 */
		public function addCc($cc) {
			$this->addRecipients ( $this->ccRecipients, $cc );
		}
		/**
		 *
		 * @param unknown $recipients
		 *        	Array or comma-separated list of email addresses to send message.
		 * @throws Exception
		 */
		public function addBcc($bcc) {
			$this->addRecipients ( $this->bccRecipients, $bcc );
		}
		/**
		 *
		 * @param unknown $recipients
		 *        	Array or comma-separated list of email addresses to send message.
		 * @throws Exception
		 */
		private function addRecipients(&$recipientList, $recipients) {
			if (! empty ( $recipients )) {
				$recipients = PostmanEmailAddress::convertToArray ( $recipients );
				foreach ( $recipients as $recipient ) {
					if (! empty ( $recipient )) {
						$this->logger->debug ( sprintf ( 'User added recipient: "%s"', $recipient ) );
						array_push ( $recipientList, new PostmanEmailAddress ( $recipient ) );
					}
				}
			}
		}
		
		/**
		 * For the string version, each header line (beginning with From:, Cc:, etc.) is delimited with a newline ("\r\n")
		 */
		public function addHeaders($headers) {
			if (! is_array ( $headers )) {
				// WordPress may send a string where "each header line (beginning with From:, Cc:, etc.) is delimited with a newline ("\r\n") (advanced)"
				// this converts that string to an array
				$headers = explode ( "\n", str_replace ( "\r\n", "\n", $headers ) );
				// $headers = explode ( PHP_EOL, $headers );
			}
			// otherwise WordPress sends an array
			foreach ( $headers as $header ) {
				if (! empty ( $header )) {
					// boundary may be in a header line, but it's not a header
					// eg. boundary="----=_NextPart_DC7E1BB5...
					if (strpos ( $header, ':' ) === false) {
						if (false !== stripos ( $header, 'boundary=' )) {
							$parts = preg_split ( '/boundary=/i', trim ( $header ) );
							$this->boundary = trim ( str_replace ( array (
									"'",
									'"' 
							), '', $parts [1] ) );
							$this->logger->debug ( sprintf ( 'Processing special boundary header \'%s\'', $this->getBoundary () ) );
						} else {
							$this->logger->debug ( sprintf ( 'Ignoring broken header \'%s\'', $header ) );
						}
						continue;
					}
					list ( $name, $content ) = explode ( ':', trim ( $header ), 2 );
					$this->processHeader ( $name, $content );
				}
			}
		}
		
		/**
		 * Add the headers that were processed in processHeaders()
		 * Zend requires that several headers are specially handled.
		 *
		 * @param unknown $name        	
		 * @param unknown $value        	
		 * @param Postman_Zend_Mail $mail        	
		 */
		private function processHeader($name, $content) {
			$name = trim ( $name );
			$content = trim ( $content );
			switch (strtolower ( $name )) {
				case 'content-type' :
					$this->logProcessHeader ( 'Content-Type', $name, $content );
					if (strpos ( $content, ';' ) !== false) {
						list ( $type, $charset ) = explode ( ';', $content );
						$this->setContentType ( trim ( $type ) );
						if (false !== stripos ( $charset, 'charset=' )) {
							$charset = trim ( str_replace ( array (
									'charset=',
									'"' 
							), '', $charset ) );
						} elseif (false !== stripos ( $charset, 'boundary=' )) {
							$this->boundary = trim ( str_replace ( array (
									'BOUNDARY=',
									'boundary=',
									'"' 
							), '', $charset ) );
							$charset = '';
						}
						if (! empty ( $charset )) {
							$this->setCharset ( $charset );
						}
					} else {
						$this->setContentType ( trim ( $content ) );
					}
					break;
				case 'to' :
					$this->logProcessHeader ( 'To', $name, $content );
					$this->addTo ( $content );
					break;
				case 'cc' :
					$this->logProcessHeader ( 'Cc', $name, $content );
					$this->addCc ( $content );
					break;
				case 'bcc' :
					$this->logProcessHeader ( 'Bcc', $name, $content );
					$this->addBcc ( $content );
					break;
				case 'from' :
					$this->logProcessHeader ( 'From', $name, $content );
					$this->setFrom ( $content );
					break;
				case 'subject' :
					$this->logProcessHeader ( 'Subject', $name, $content );
					$this->setSubject ( $content );
					break;
				case 'reply-to' :
					$this->logProcessHeader ( 'Reply-To', $name, $content );
					$this->setReplyTo ( $content );
					break;
				case 'sender' :
					$this->logProcessHeader ( 'Sender', $name, $content );
					$this->logger->warn ( sprintf ( 'Ignoring Sender header \'%s\'', $content ) );
					break;
				case 'return-path' :
					$this->logProcessHeader ( 'Return-Path', $name, $content );
					$this->logger->warn ( sprintf ( 'Ignoring Return-Path header \'%s\'', $content ) );
					break;
				case 'date' :
					$this->logProcessHeader ( 'Date', $name, $content );
					$this->setDate ( $content );
					break;
				case 'message-id' :
					$this->logProcessHeader ( 'Message-Id', $name, $content );
					$this->setMessageId ( $content );
					break;
				default :
					// Add it to our grand headers array
					$this->logProcessHeader ( 'other', $name, $content );
					array_push ( $this->headers, array (
							'name' => $name,
							'content' => $content 
					) );
					break;
			}
		}
		
		/**
		 *
		 * @param unknown $desc        	
		 * @param unknown $name        	
		 * @param unknown $content        	
		 */
		private function logProcessHeader($desc, $name, $content) {
			$this->logger->debug ( 'Processing ' . $desc . ' Header - ' . $name . ': ' . $content );
		}
		
		/**
		 * Add attachments to the message
		 *
		 * @param Postman_Zend_Mail $mail        	
		 */
		public function addAttachmentsToMail(Postman_Zend_Mail $mail) {
			$attachments = $this->attachments;
			if (! is_array ( $attachments )) {
				// WordPress may a single filename or a newline-delimited string list of multiple filenames
				$attArray = explode ( PHP_EOL, $attachments );
			} else {
				$attArray = $attachments;
			}
			// otherwise WordPress sends an array
			foreach ( $attArray as $file ) {
				if (! empty ( $file )) {
					$this->logger->debug ( "Adding attachment: " . $file );
					$at = new Postman_Zend_Mime_Part ( file_get_contents ( $file ) );
					// $at->type = 'image/gif';
					$at->disposition = Postman_Zend_Mime::DISPOSITION_ATTACHMENT;
					$at->encoding = Postman_Zend_Mime::ENCODING_BASE64;
					$at->filename = basename ( $file );
					$mail->addAttachment ( $at );
				}
			}
		}
		function setBody($body) {
			$this->body = $body;
		}
		function setBodyTextPart($bodyTextPart) {
			$this->bodyTextPart = $bodyTextPart;
		}
		function setBodyHtmlPart($bodyHtmlPart) {
			$this->bodyHtmlPart = $bodyHtmlPart;
		}
		function setSubject($subject) {
			$this->subject = $subject;
		}
		function setAttachments($attachments) {
			$this->attachments = $attachments;
		}
		function setFrom($email, $name = null) {
			if (! empty ( $email )) {
				$this->from = new PostmanEmailAddress ( $email, $name );
			}
		}
		function setReplyTo($replyTo) {
			if (! empty ( $replyTo )) {
				$this->replyTo = new PostmanEmailAddress ( $replyTo );
			}
		}
		function setMessageId($messageId) {
			$this->messageId = $messageId;
		}
		function setDate($date) {
			$this->date = $date;
		}
		
		// return the headers
		public function getHeaders() {
			return $this->headers;
		}
		public function getBoundary() {
			return $this->boundary;
		}
		public function getToRecipients() {
			return $this->toRecipients;
		}
		public function getCcRecipients() {
			return $this->ccRecipients;
		}
		public function getBccRecipients() {
			return $this->bccRecipients;
		}
		public function getReplyTo() {
			return $this->replyTo;
		}
		public function getDate() {
			return $this->date;
		}
		public function getMessageId() {
			return $this->messageId;
		}
		public function getSubject() {
			return $this->subject;
		}
		public function getBody() {
			return $this->body;
		}
		public function getBodyTextPart() {
			return $this->bodyTextPart;
		}
		public function getBodyHtmlPart() {
			return $this->bodyHtmlPart;
		}
		public function getAttachments() {
			return $this->attachments;
		}
	}
}