I have been looking at multiple examples including this one.

I get the email no problem but there are no attachments. Am I missing the content/type of file type? All the examples I've seen uses only text/html as the content type.

Here's what I have (added upon Stephen's request)

if( isset( $_POST['to'] ) && isset( $_POST['from'] ) ) {
global $wpdb;

$to = $_POST['to'];
$from = $_POST['from']; 
$name = get_bloginfo('name');
$attachment = $_POST['file'];
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: multipart/mixed; charset=iso-8859-1' . "\r\n";

$headers .= 'From: ' . $name . ' <' . $from . '>' . "\r\n";   
$subject = 'Send to Kindle';
$msg = 'Yay! Your book has <a href="http://yahoo.com">arrived</a>';

$mail_attachment = array( $attachment );
wp_mail($to, $subject, $msg, $headers, $mail_attachment);
echo 'Email sent';
} else {
echo 'Email not sent';
  • Could you post some code regarding what you've tried? – Stephen Harris Apr 26 '12 at 7:49
  • Hi Stephen, just updated the post with the code. Thank you! – tbm Apr 26 '12 at 18:31

The $attachment argument for wp_mail takes a file (or array of files) - but the file path has to be fully specified. For example:

   $attachments = array(WP_CONTENT_DIR . '/uploads/file_to_attach.zip');
   $headers = 'From: My Name <myname@mydomain.com>' . "\r\n";
   wp_mail('test@test.com', 'subject', 'message', $headers, $attachments);

(see Codex). It seems that your $_POST['file'] is probably not specifying the full path.

The attachment has to a file path, not an url. The following worked for me:

$to = $_POST['to'];
$from = $_POST['from']; 
$name = get_bloginfo('name');

$headers = 'From: My Name <myname@mydomain.com>' . "\r\n";

$subject = 'Send to Kindle';

$msg = 'Yay! Your book has <a href="http://yahoo.com">arrived</a>';

$mail_attachment = array(WP_CONTENT_DIR . '/uploads/2012/03/image.png');   

wp_mail($to, $subject, $msg, $headers, $mail_attachment);

Note: I changed the headers attribute too. I'm not entirely sure what you're example was trying to do, but it meant the message of the email was not visible on some email clients.

  • Thank you Stephen for following up... ok $_Post['file'] is actually a URL to the file and I have confirmed the URL is correct. Is that acceptable? – tbm Apr 27 '12 at 3:40
  • Nope, see updated answer :) – Stephen Harris Apr 27 '12 at 10:46
  • That means I can't have them send an attachment that is hosted elsewhere e.g. Amazon S3? I was afraid of that. I'm just trying to give people the option to email a PDF to Kindle directly from a web page. Anyhow I'll try this later and report back. – tbm Apr 27 '12 at 12:02
  • You could read the remote file, then attach it? There's an example here. Exactly how you can do it / best practise would be a question for SO. – Stephen Harris Apr 27 '12 at 12:17
  • Thank you so much Stephen I tried your solution and it did work. Now... I have to figure out how to send attachment hosted elsewhere. Thanks for the pointer where to look! – tbm Apr 28 '12 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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