SimpleEmailServiceMessage::addAttachmentFromFile PHP Method

addAttachmentFromFile() public method

Add email attachment by passing file path
public addAttachmentFromFile ( string $name, string $path, string $mimeType = 'application/octet-stream', string $contentId = null, string $attachmentType = 'attachment' ) : boolean
$name string The name of the file attachment as it will appear in the email
$path string Path to the attachment file
$mimeType string Specify custom MIME type
$contentId string Content ID of the attachment for inclusion in the mail message
$attachmentType string Attachment type: attachment or inline
return boolean Status of the operation
    public function addAttachmentFromFile($name, $path, $mimeType = 'application/octet-stream', $contentId = null, $attachmentType = 'attachment')
    {
        if (file_exists($path) && is_file($path) && is_readable($path)) {
            $this->addAttachmentFromData($name, file_get_contents($path), $mimeType, $contentId, $attachmentType);
            return true;
        }
        return false;
    }

Usage Example

Exemplo n.º 1
0
function wpses_mail($to, $subject, $message, $headers = '', $attachments = '')
{
    global $SES;
    global $wpses_options;
    global $wp_better_emails;
    // headers can be sent as array, too. convert them to string to avoid further complications.
    if (is_array($headers)) {
        $headers = implode("\r\n", $headers);
    }
    extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers')));
    wpses_check_SES();
    if (isset($wp_better_emails)) {
        // From wpbe plugin, not efficient nor elegant - Will do better next time.
        // Could just call the php filter on a adhoc object, to be less dependant on the implementation of wpbe code.
        $txt = wp_specialchars_decode($message, ENT_QUOTES);
        $txt = $wp_better_emails->set_email_template($txt, 'plaintext_template');
        $txt = apply_filters('wpbe_plaintext_body', $wp_better_emails->template_vars_replacement($txt));
        /** HTML ******************************************************* */
        $html = $wp_better_emails->esc_textlinks($message);
        $html = nl2br(make_clickable($html));
        $html = $wp_better_emails->set_email_template($html);
        $html = apply_filters('wpbe_html_body', $wp_better_emails->template_vars_replacement($html));
    } else {
        $message = preg_replace('/<(http:.*)>/', '$1', $message);
        $html = $message;
        $txt = strip_tags($html);
        if (strlen($html) == strlen($txt)) {
            $html = '';
            // que msg text
        }
        // no html entity in txt.
        $txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
    }
    // TODO: option pour que TXT si msg html, ou les deux comme ici par défaut.
    $m = new SimpleEmailServiceMessage();
    $m->addTo($to);
    $m->setReturnPath($wpses_options['return_path']);
    $from = '"' . $wpses_options['from_name'] . '" <' . $wpses_options['from_email'] . ">";
    if ('' != $wpses_options['reply_to']) {
        if ('headers' == strtolower($wpses_options['reply_to'])) {
            // extract replyto from headers
            $rto = array();
            if (preg_match('/^Reply-To: ([a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4})\\b/imsU', $headers, $rto)) {
                // does only support one email for now.
                $m->addReplyTo($rto[1]);
            }
            if (preg_match('/^From: (.*)/isU', $headers, $rto)) {
                // Uses "From:" header
                $from = $rto[1];
            }
        } else {
            $m->addReplyTo($wpses_options['reply_to']);
        }
    }
    $m->setFrom($from);
    $m->setSubject($subject);
    if ($html == '') {
        // que texte
        $m->setMessageFromString($txt);
    } else {
        $m->setMessageFromString($txt, $html);
    }
    // Attachments
    if ('' != $attachments) {
        if (!is_array($attachments)) {
            $attachments = explode("\n", $attachments);
        }
        // Now we got an array
        foreach ($attachments as $afile) {
            $m->addAttachmentFromFile(basename($afile), $afile);
        }
    }
    $res = $SES->sendEmail($m);
    if (is_array($res)) {
        return $res['MessageId'];
    } else {
        return NULL;
    }
}
All Usage Examples Of SimpleEmailServiceMessage::addAttachmentFromFile