How to Send Email with a File Attachment in Php-phpcodehub

How to Send  Email with a File Attachment in Php

  • Sending a Simple Text Email
  • Sending HTML Email
  • Sending Email with Attachments

Email is the most popular Internet service today. A plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP.

So, you want to send automated email messages from your PHP application. This can be in direct response to a user’s action, such as signing up for your site, or a recurring event at a set time, such as a monthly newsletter. Sometimes email contains file attachments, both plain text and HTML portions, and so on. To understand how to send each variation that may exist on an email, we will start with the simple example and move to the more complicated.

Now-a-days, we have forms on site, which has an UPLOAD button in it. We create form using PHP. And when user submit that form with some file, we should get an email with that file as Attachment. So we generally what do, we upload the file to server and send it with email. But what if you don’t want to save that file on server and directly get it in email. Following is the code for the same. It will attach that file with email and send it to user directly.




$to = $_POST['toEmail'];
$fromEmail = $_POST['fieldFormEmail']; 
$fromName = $_POST['fieldFormName']; 
$subject = $_POST['fieldSubject']; 
$message = $_POST['fieldDescription'];

$tmpName = $_FILES['attachment']['tmp_name']; 
$fileType = $_FILES['attachment']['type']; 
$fileName = $_FILES['attachment']['name']; 

"Content-Type: {$fileatt_type};\n" .
$htmlbody = "Name:{$fromName}\n"."Email:{$fromEmail}\n"."Subject:{$subject}\n"."Message:{$message}\n";
//$htmlbody = "Name:".$fromName."</h3><h3>Email:".$fromEmail."</h3><p>Subject:".$subject."</p><p>Message:".$message."</p>";
$to = $to; //Recipient Email Address

$subject = $subject; //Email Subject

$headers = "From: ".$fromEmail."\r\nReply-To: ".$fromEmail."";

$random_hash = md5(date('r', time()));

$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($tmpName))); 
//$attachment = chunk_split(base64_encode(file_get_contents('test.jpg'))); // Set your file path here

//define the body of the message.

$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";

//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; name=\"".$fileName."\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";
//echo $message; die;
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";

//send the email
$mail = mail( $to, $subject , $message, $headers );

echo $mail ? "Mail sent" : "Mail failed";
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Attachment Without Upload - Excellent Web World</title>
body{ font-family:Arial, Helvetica, sans-serif; font-size:13px;}
th{ background:#99CC33; text-align:right; vertical-align:top;}
input{ width:181px;}
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="mainform" enctype="multipart/form-data">
    <table width="500" border="0" cellpadding="5" cellspacing="5">
        <th>Your Name</th>
        <td><input name="fieldFormName" type="text"></td>
        <th>Your Email</th>
        <td><input name="fieldFormEmail" type="text"></td>
        <th>To Email</th>
        <td><input name="toEmail" type="text"></td>

        <td><input name="fieldSubject" type="text" id="fieldSubject"></td>
        <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td>
      <th>Attach Your File</th>
      <td><input name="attachment" type="file"></td>
        <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td>
Share Button

About admin

Leave a Reply

Your email address will not be published. Required fields are marked *


Author: pramod kumar kumawat Mob: 9269727062
Free WordPress Themes - Download High-quality Templates