Sending email with attachment using php’s mail() function
Hello Friends,
All we know how to send email in php. If you are begineer in php than i must say php provides inbuilt mail function named as mail(). Using this function we can implement email functionality very easily just by passing four parameters (to, subject, message and headers). But what if you want to send an attachement with an email ? Today i came across a functionality where i need to send an email with attachment(Sending resume – doc file). Here i am sharing the function using which you can send an email with attachment using php's mail function.
$to = "raxit4u2@gmail.com";
$subject = "Email with attachment";
$random_hash = md5(date('r', time()));
$headers = "From: raxit4u2@gmail.com\r\nReply-To: raxit4u2@gmail.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents("resume.doc")));
$output = "
–PHP-mixed-$random_hash;
Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
–PHP-alt-$random_hash
Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
Hello,
This is testing email with attachment. Please find attachment.
–PHP-alt-$random_hash
Content-Type: text/html; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
Hello,
This is testing email with attachment. Please find attachment.
–PHP-alt-$random_hash–
–PHP-mixed-$random_hash
Content-Type: application/msword; name=resume.doc
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
–PHP-mixed-$random_hash–";
if(mail($to, $subject, $output, $headers))
{
echo 'Mail Sent with an attachment.';
}
Copy the above code for sending attachment emails in php,attachment email using php script,send an email with attachment,php emails with attachment.
Comment below if you have any question.
To know more about programming,JavaScript issues,jQuery,Expression Engine,MYSQL database and Open-source, enter your email address below. We will send you free tutorials.
Posts Related to Sending email with attachment using php's mail() function
Sending email with multiple attachment using php’s mail() function
Hello Friends, All we know how to send email in php. You might not be aware with sending email with attachment in php. If so ...
POP3/IMAP Email with attachment-Read & Forwarding with PHP
Hello Friends, Do you know how we can read, forward or transfer the POP3/IMAP email to another email address ? I came across this situation. ...
How to send email from localhost(XAMPP or WAMP) in PHP in Windows
Hello Friends, Whenever you works in local server (XAMPP or WAMP) for developing any email functionality in website, than you might be wonder that why ...
Export MYSQL data into Excel/CSV via php
Hello Friends, Today i came across a functionality where i need to Export the MYSQL data into CSV/Excel file via PHP function/script. There are such ...
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
Hello Friends, I was working with SMTP (Simple Mail Transfer Protocol) for sending email across IP (Internet Protocol) networks. I have downloaded smtp mailer class, ...
You can leave a response or trackback from your own site.






I am
about 4 months ago
i want to mail via my localhost(xampp). i m using bsnl as my isp and use yahoo to send mails. setup made in php.ini -> smtp=smtp.bsnl.co.in and others as u detailed in your articles. but “SMTP server response:550 5.7.1 relaying denied” error comes. please guide me..
about 3 weeks ago
Hello.
Good work, but i use for this phpmailer class from http://phpmailer.worxware.com/. It is working with every mail clients i know, even with html messages…