Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

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.

Enter your email address:
 
Feel free to ask any question. Just leave your comment below and we will answer your comment with in 24 hours.
Share:

6 Comments

Leave a Reply

Your email address will not be published.