plugins - wp_mail attachment not working

Hello I am trying to send mail with some attachment using with wordpress wp_mail functionality but I am not getting atta

Hello I am trying to send mail with some attachment using with wordpress wp_mail functionality but I am not getting attachment in my mailbox.

Can you please check my bellow code and guide me where I am wrong. You can see I am seeding image in attachment.

<?php
    $to  = '[email protected]';
    $subject = 'WordPress wp_mail';
    $message = '
    <html>
    <body>
        <table rules="all" style="border-color: #666;" cellpadding="10">
          <tr>Hello WordPress</tr>
        </table>          
    </body>
    </html>
    ';

    $attachments = array( 'http://sitename/project/wp-content/plugins/my-plugin/uploads/sample_photo_01.jpg' );
    //$attachments = array(WP_CONTENT_DIR  . '/uploads/'.$_FILES["myfile"]["name"]);
    $headers[] = 'MIME-Version: 1.0' . "\r\n";
    $headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers[] = 'From: '.get_option( 'blogname' ).' <'.get_option( 'admin_email' ).'>';

    wp_mail( $to, $subject, $message, $headers, $attachments );
?>

Thanks.

Hello I am trying to send mail with some attachment using with wordpress wp_mail functionality but I am not getting attachment in my mailbox.

Can you please check my bellow code and guide me where I am wrong. You can see I am seeding image in attachment.

<?php
    $to  = '[email protected]';
    $subject = 'WordPress wp_mail';
    $message = '
    <html>
    <body>
        <table rules="all" style="border-color: #666;" cellpadding="10">
          <tr>Hello WordPress</tr>
        </table>          
    </body>
    </html>
    ';

    $attachments = array( 'http://sitename/project/wp-content/plugins/my-plugin/uploads/sample_photo_01.jpg' );
    //$attachments = array(WP_CONTENT_DIR  . '/uploads/'.$_FILES["myfile"]["name"]);
    $headers[] = 'MIME-Version: 1.0' . "\r\n";
    $headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers[] = 'From: '.get_option( 'blogname' ).' <'.get_option( 'admin_email' ).'>';

    wp_mail( $to, $subject, $message, $headers, $attachments );
?>

Thanks.

Share Improve this question edited Apr 29, 2014 at 18:04 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Apr 29, 2014 at 18:04 MananManan 1771 gold badge4 silver badges10 bronze badges 2
  • Are normal emails from WordPress working? – Steven Jones Commented Apr 29, 2014 at 18:14
  • My email functionality is working. Only thing is that I am not getting attachments. – Manan Commented Apr 29, 2014 at 18:15
Add a comment  | 

3 Answers 3

Reset to default 9

Attachments should always use the absolute filesystem path.

Also to change the Content-Type of the email you should use the wp_mail_content_type filter.

<?php
function my_custom_email() {
    $to  = '[email protected]';
    $subject = 'WordPress wp_mail';
    $message = '
    <html>
    <body>
        <table rules="all" style="border-color: #666;" cellpadding="10">
          <tr>Hello WordPress</tr>
        </table>          
    </body>
    </html>
    ';

    $attachments = array(  WP_PLUGIN_DIR . '/my-plugin/uploads/sample_photo_01.jpg' );
    $headers[] = 'From: '.get_option( 'blogname' ).' <'.get_option( 'admin_email' ).'>';
    add_filter( 'wp_mail_content_type', 'my_custom_email_content_type' );
    return wp_mail( $to, $subject, $message, $headers, $attachments );
}

function my_custom_email_content_type() {
    return 'text/html';
}

I have placed the entire code in a function so that the wp_mail_content_type filter applies only to this email.

Sources:
http://codex.wordpress/Function_Reference/wp_mail
http://codex.wordpress/Plugin_API/Filter_Reference/wp_mail_content_type

You can try this code

  $attachments = array( WP_CONTENT_DIR . '/uploads/file_to_attach.zip' );
   $headers = 'From: My Name <[email protected]>' . "\r\n";
   wp_mail('[email protected]', 'subject', 'message', $headers, $attachments );

I get it from http://codex.wordpress/Function_Reference/wp_mail

Better to use get_home_path(); - WordPress Documentation

$attachments = array( get_home_path() . '/wp-content/themes/your-theme/uploads/sample_photo_01.jpg');

This gets the absolute filesystem path to the root of the WordPress installation, all you need to do from there is navigate to where you image / file is located.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745107755a4611660.html

相关推荐

  • plugins - wp_mail attachment not working

    Hello I am trying to send mail with some attachment using with wordpress wp_mail functionality but I am not getting atta

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信