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).
<?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.
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.when i run this program , it is adding some characters in the email message part. no attachement is taking place. 🙁
Please ensure that you have written proper code for
$headers
and$message
variables. Each and every character for them is very important.such a nice article..
very helpfull
thanx a lot
It run successfully bt attachment file like pdf contains nothing. Pdf shows it is a 0 kb file… Hlp Me…
You can send your code to nikunjbhatt84@gmail.com. I will check the code and reply with suggestion if any.
thankx a lot man…..u have done a great job
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…. 😛
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
You can send your code to nikunjbhatt84@gmail.com. I will check the code and reply with suggestion if any.
Thanks..nice tutorial…..
hii nikunj, Thank you so much for checking my code and for the instructions provided…
🙂
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”
There is a mistake in the sendmail.ini’s setting. You should specify
smtp_port=465
when specifyingsmtp_ssl=ssl
. Ifsmtp_port=587
is specified thensmtp_ssl=tls
should be set.i set smtp_ssl=tsl now result comming Email sent. but not received mail. please give the solution.
Thanks,
P.Veera.
Have you specified
tsl
ortls
? Correct value istls
.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=
I am seeing that now
localhost
is removed fromhostname
setting. And please copy-paste here, in comment, the contents of debug.log file of sendmail.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//
It seems that you are trying using heredoc string. Please check the manual regarding heredoc string on php.net.
Yes HERDOC – figured it out and it is working perfectly now! Thanks//
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!
Be sure that the file path and name are correct. You can also use the
file_exists()
function to check for the existence of the file before callingfile_get_contents()
function.