2

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.

2
  • Are normal emails from WordPress working? 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

3 Answers 3

8

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.org/Function_Reference/wp_mail
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail_content_type

7
  • Hi I have tried your function code in and check but now I am not able to get mail.
    – Manan
    Commented Apr 29, 2014 at 18:49
  • @Manan I have made a slight change to the code. See what the function returns - var_dump( my_custom_email() );
    – A.Jesin
    Commented Apr 29, 2014 at 18:55
  • No mail and getting bool(true)
    – Manan
    Commented Apr 29, 2014 at 19:00
  • @Manan Were you previously receiving mails using the code in your question?
    – A.Jesin
    Commented Apr 29, 2014 at 19:01
  • Hey now I got the mail but not found attachment.
    – Manan
    Commented Apr 29, 2014 at 19:03
1

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.org/Function_Reference/wp_mail

8
  • Yes I have made code from that page but not working.
    – Manan
    Commented Apr 29, 2014 at 18:09
  • You can remove $headers[] = 'MIME-Version: 1.0' . "\r\n"; $headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    – Maidul
    Commented Apr 29, 2014 at 18:10
  • I have uploads folder inside my-plugin so how to retrieve from that folder? Is it use for WP_CONTENT_DIR?
    – Manan
    Commented Apr 29, 2014 at 18:11
  • Any special reason for that. I have no idea.
    – Manan
    Commented Apr 29, 2014 at 18:12
  • WP_CONTENT_DIR is a constant get wp-content folder path only.For plugin you can get the plugin folder path by plugin_dir_url() codex.wordpress.org/Function_Reference/plugin_dir_url
    – Maidul
    Commented Apr 29, 2014 at 18:17
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.