AWS_SES_WP_Mail\SES::send_wp_mail PHP Method

send_wp_mail() public method

Override WordPress' default wp_mail function with one that sends email using the AWS SDK.
Since: 0.0.1
public send_wp_mail ( string $to, string $subject, string $message, mixed $headers = [], array $attachments = [] ) : boolean
$to string
$subject string
$message string
$headers mixed
$attachments array
return boolean true if mail has been sent, false if it failed
    public function send_wp_mail($to, $subject, $message, $headers = array(), $attachments = array())
    {
        // Compact the input, apply the filters, and extract them back out
        extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')));
        // Get headers as array
        if (empty($headers)) {
            $headers = array();
        }
        if (!is_array($headers)) {
            // Explode the headers out, so this function can take both
            // string headers and an array of headers.
            $headers = explode("\n", str_replace("\r\n", "\n", $headers));
        }
        // transform headers array into a key => value map
        $headers = array_reduce($headers, function ($headers, $header) {
            $header = array_map('trim', explode(':', $header));
            $headers[$header[0]] = $header[1];
            return $headers;
        }, array());
        // Get the site domain and get rid of www.
        $sitename = strtolower(parse_url(site_url(), PHP_URL_HOST));
        if ('www.' === substr($sitename, 0, 4)) {
            $sitename = substr($sitename, 4);
        }
        $from_email = 'wordpress@' . $sitename;
        $message_args = array('subject' => $subject, 'to' => $to, 'headers' => array('Content-Type' => apply_filters('wp_mail_content_type', 'text/plain'), 'From' => sprintf('%s <%s>', apply_filters('wp_mail_from_name', get_bloginfo('name')), apply_filters('wp_mail_from', $from_email))));
        $message_args['headers'] = array_merge($message_args['headers'], $headers);
        $message_args = apply_filters('aws_ses_wp_mail_pre_message_args', $message_args);
        // Make sure our to value is an array so we can manipulate it for the API.
        if (!is_array($message_args['to'])) {
            $message_args['to'] = explode(',', $message_args['to']);
        }
        if ($message_args['headers']['Content-Type'] === 'text/plain') {
            $message_args['text'] = $message;
        } else {
            $message_args['html'] = $message;
        }
        // Allow user to override message args before they're sent to Mandrill.
        $message_args = apply_filters('aws_ses_wp_mail_message_args', $message_args);
        $ses = $this->get_client();
        if (is_wp_error($ses)) {
            return $ses;
        }
        try {
            $args = array('Source' => $message_args['headers']['From'], 'Destination' => array('ToAddresses' => $message_args['to']), 'Message' => array('Subject' => array('Data' => $message_args['subject'], 'Charset' => get_bloginfo('charset')), 'Body' => array()));
            if (isset($message_args['text'])) {
                $args['Message']['Body']['Text'] = array('Data' => $message_args['text'], 'Charset' => get_bloginfo('charset'));
            }
            if (isset($message_args['html'])) {
                $args['Message']['Body']['Html'] = array('Data' => $message_args['html'], 'Charset' => get_bloginfo('charset'));
            }
            $args = apply_filters('aws_ses_wp_mail_ses_send_message_args', $args, $message_args);
            $ses->sendEmail($args);
        } catch (\Exception $e) {
            return new WP_Error(get_class($e), $e->getMessage());
        }
        return true;
    }