Sending email with attachment from localhost/WAMP Server using sendmail

PHPMailer and PEAR Mail supports sending attachment with email using their functions or extensions; but PHP’s own mail() function hasn’t any function separately to achieve this. Developers have to use their own code to attach file within email’s body to do so.

PHP’s mail() function can be used with sendmail.exe to send email from localhost/WAMP Server and I have already posted an article about it; so to configure it, refer to that article. This article will cover only the PHP code for sending email with attachment(s).

Solution
<?php
$random_hash = md5(time());
$file1 = "ebook.pdf";
$file2 = "image.jpg";
$to = "[recepient's_email_address]";
$subject = 'the subject';
$headers = 'From: [your_gmail_id]@gmail.com' . "\r\n" .
  'Reply-To: [your_gmail_id]@gmail.com' . "\r\n" .
  'X-Mailer: PHP/' . phpversion() . "\r\n" .
  'Content-Type: multipart/mixed; boundary="PHP-mixed-'.$random_hash.'"';
$attachment1 = chunk_split(base64_encode(file_get_contents($file1)));
$attachment2 = chunk_split(base64_encode(file_get_contents($file2)));

$message = <<<EMAIL_MESSAGE
--PHP-mixed-$random_hash
Content-Type: text/html; charset="iso-8859-1"; boundary="PHP-alt-$random_hash

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-mixed-$random_hash
Content-Type: application/pdf; name="$file1" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment1
--PHP-mixed-$random_hash
Content-Type: image/jpg; name="$file2" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment2
--PHP-mixed-$random_hash--
EMAIL_MESSAGE;

if(mail($to, $subject, $message, $headers))
	echo "Email sent";
else
	echo "Email sending failed";
?>

The “else” part in the above code might never get executed because mail() function will not return false when used with sendmail.exe; so might not get the error/reason if you don’t receive email.

Don’t try to send email from Yahoo!’s free email account, because it is not allowing users to send email using its SMTP server from other email clients or other such facilities (like this, specified in this article), only premium/paid members can use external resources than Yahoo! Mail to send emails.

Leave a comment ?

24 Comments.

  1. hello sir,,

    thanks for u r information

    i want to know how to send upload file as an attachment with php

    • This is similar to shown here, you just need to change the filename to the filename which user has uploaded.
      So it could be as following:
      move_uploaded_file($_FILES['file']['tmp_name'],'uploaded-file/'.$_FILES['file']['name']);
      $file1 = 'uploaded-file/'.$_FILES['file']['name'];

      (Here, in $_FILES['file'], “file” is the name of the HTML’s file upload box’ name. and in $file1 = 'uploaded-file/'., “uploaded-file” is the name of the folder where the uploaded file will be moved to.)

      Just replace the "$file1 = "ebook.pdf"; line with the above 2 lines and remove $file2‘s line and its related lines from the PHP code.

  2. when i run this program , it is adding some characters in the email message part. no attachement is taking place. 🙁

  3. such a nice article..
    very helpfull
    thanx a lot

  4. It run successfully bt attachment file like pdf contains nothing. Pdf shows it is a 0 kb file… Hlp Me…

  5. thankx a lot man…..u have done a great job

  6. really a nice article posted by you. i m a biggner and it it wonder for me and i have done it easily….. thanks dud…. 😛

  7. I used Sending email from localhost/WAMP Server using sendmail is worked for text format not it anyattachment files… what I have to do to rectify it

  8. Thanks..nice tutorial…..

  9. hii nikunj, Thank you so much for checking my code and for the instructions provided…
    🙂

  10. i configured but not working reporting comming for “Email Sending Failed”
    here is my code please help me.

    sendmail.ini

    smtp_server=smtp.gmail.com
    smtp_port=587
    smtp_ssl=ssl
    default_domain=localhost
    error_logfile=error.log
    debug_logfile=debug.log
    auth_username=hclveerakumar@gmail.com
    auth_password=something
    pop3_server=
    pop3_username=
    pop3_password=
    force_sender=
    force_recipient=
    hostname=localhost

    php.ini

    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    ; SMTP = smtp.gmail.com
    ; http://php.net/smtp-port
    ; smtp_port = 587

    ; For Win32 only.
    ; http://php.net/sendmail-from
    ; sendmail_from = hclveerakumar@gmail.com

    ; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).
    ; http://php.net/sendmail-path
    sendmail_path = “C:\wamp\sendmail\sendmail.exe”

  11. not working resulting comming email not send.
    please help me
    //sendmail.ini
    smtp_server=smtp.gmail.com
    smtp_port=587
    smtp_ssl=tls
    default_domain=localhost
    error_logfile=error.log
    debug_logfile=debug.log
    auth_username=hclveerakumar@gmail.com
    auth_password=something
    pop3_server=
    pop3_username=
    pop3_password=
    force_sender=
    force_recipient=
    hostname=

  12. Norman Berger

    I’ve been looking for a routine tot send eMail with attachment and this code on its own works well.

    When I try to incorporate in my own program the <<<EMAIL_MESSAGE causes some strange errors.

    Not familiar with it? Can you explain or point me to a place to learn how this is used? Thanks//

  13. Norman Berger

    Yes HERDOC – figured it out and it is working perfectly now! Thanks//

  14. Hi,
    i copy the code but and they send the PDF but with 0kb, so the PDF is empty.
    Can you help me with this?

    thanks a lot!

Leave a Comment