# sendwp/1.2.9/includes/class.mailer.php

SendWP, version 1.2.9. 167 lines.

- Page: https://pluginprobe.com/plugins/sendwp/1.2.9/code/includes/class.mailer.php
- Raw: https://pluginprobe.com/plugins/sendwp/1.2.9/raw/includes/class.mailer.php
- Modified: 2022-05-30T19:40:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sendwp/1.2.9/code/includes/class.mailer.php#L10-L20`.

```php
<?php // @codingStandardsIgnoreLine

namespace SendWP;

use SendWP\API\Request;

/**
 * A decorater for sending API based transactional email.
 *
 * @TODO Maybe extract formatting methods.
 */
class Mailer implements MailerInterface {
	protected $phpmailer;
	protected $request;

	/**
	 * Create a factory
	 *
	 * @param Object $phpmailer
	 *
	 * @return Object
	 */
	public static function factory( &$phpmailer ) {
		$request   = \SendWP\API\Request::create( 'postmaster' );
		$phpmailer = new self( $phpmailer, $request );

		return $phpmailer;
	}

	/**
	 * Constructor
	 *
	 * @param Object $phpmailer
	 * @param Request $request
	 *
	 * @return Void
	 */
	public function __construct( $phpmailer, Request $request ) {
		$this->phpmailer = $phpmailer;
		$this->request   = $request;
	}

	/**
	 * Check for property/method in $phpmailer.
	 *
	 * @param String $name
	 *
	 * @return String
	 */
	public function __get( $name ) {
		if ( property_exists( $this->phpmailer, $name ) ) {
			return $this->phpmailer->$name;
		}

		return '';
	}

	/**
	 * Caller method
	 *
	 * @param String $name
	 * @param Array $arguments
	 *
	 * @return Self|Null
	 */
	public function __call( $name, $arguments ) {
		if ( method_exists( $this->phpmailer, $name ) ) {
			return call_user_func( array( $this->phpmailer, $name ), $arguments );
		}

		return null;
	}

	/**
	 * Get attachments
	 *
	 * @return Array
	 */
	public function get_attachments() {
		$attachments = $this->phpmailer->get_attachments();
		$attachments = array_map( array( $this, 'formatAttachment' ), $attachments );

		return $attachments;
	}

	/**
	 * Send email
	 *
	 * @return Bool
	 */
	public function send() {
		$to_emails  = array_map( array( $this, 'formatEmails' ), $this->getToAddresses() );
		$cc_emails  = array_map( array( $this, 'formatEmails' ), $this->getCcAddresses() );
		$bcc_emails = array_map( array( $this, 'formatEmails' ), $this->getBccAddresses() );

		$args = array(
			'body' => array(
				'to'        => json_encode( (array) $to_emails ),
				'from'      => $this->From, // @codingStandardsIgnoreLine
				'from_name' => $this->FromName, // @codingStandardsIgnoreLine
				'subject'   => $this->Subject, // @codingStandardsIgnoreLine
			),
		);

		if ( 'text/html' === $this->ContentType ) { // @codingStandardsIgnoreLine
			$args['body']['body']    = $this->Body; // @codingStandardsIgnoreLine
			$args['body']['altbody'] = $this->AltBody; // @codingStandardsIgnoreLine
		} else {
			$args['body']['altbody'] = $this->Body; // @codingStandardsIgnoreLine
		}

		if ( $reply_to = $this->getReplyToAddresses() ) { // @codingStandardsIgnoreLine
			/**
			 * $reply_to is an array of arrays.
			 * [ [ $address, $name ], [...] ]
			 * To get a string value for the address, we need to reset it twice.
			 */
			$reply_to                 = reset( $reply_to );
			$args['body']['reply_to'] = reset( $reply_to );
		}

		if ( ! empty( $cc_emails ) ) {
			$args['body']['cc'] = json_encode( (array) $cc_emails );
		}

		if ( ! empty( $bcc_emails ) ) {
			$args['body']['bcc'] = json_encode( (array) $bcc_emails );
		}

		$attachments = $this->get_attachments();

		if ( $attachments ) {
			$args['body']['attachments'] = $attachments;
		}

		// Response is empty...
		$response = $this->request->post( $args );

		return true; // Sent by the Service.
	}

	/**
	 * Format email addresses
	 *
	 * @param Array $emails
	 *
	 * @return Array
	 */
	protected function formatEmails( $emails ) {
		return reset( $emails );
	}

	/**
	 * Format attachments
	 *
	 * @param Array $attachments
	 *
	 * @return Array
	 */
	protected function formatAttachment( $attachment ) {
		return array(
			'filename' => $attachment[1], // $filename per PHPMailer docs.
			'filedata' => file_get_contents( $attachment[0] ), // $path per PHPMailer docs.
		);
	}
}

```
