PHP mail HTML format with attachment

In this Post We Will Explain About is PHP Send HTML Email File Attachment Using PHPs mail With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to create PHP based email form with file attachmentExample

In this post we will show you Best way to implement How to Send a Confirmation Email with a File Attachment, hear for Php Send Email with Attachment – Sending Mailwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

PHP – send email with attachment file

PHP simple send mail with attachment script. This simple or custom function or source code PHP mail attachment script is simple able to send a text or plain text email send message as well as with a single data path attachment file.Below is an simple example on how we use this function to as send an simple email message with one some attached live24u.pdf file:Include the simple main script file — require_once(‘path/to/file/class.phpmailer.php’); as well as Download the PHPMailer libs or script source code from here: http://github.com/PHPMailer/PHPMailer

Source code : PHP mail attachment script
[php]
$my_file = “live24u.pdf”; // Pakainfo.com attachment file name
$path = “Your folder name”; //Pakainfo.com path of attachment file
$user_name = “Your Usrename”; //Pakainfo.com user name
$mail_to = “[email protected]”; //Pakainfo.com to eamil
$mail_sub = “Notification of attachment for user”; //Pakainfo.com subject of eamil
$body_text = “Hello “.$user_name.”,\nThis demo message with file attachment. You will be notified when their review has been completed.\n Regard \n live24u Team”;

$attachment_file = $path . “/” . $my_file; // path of file
$attachment_file_size = filesize($attachment_file);
$email_handle = fopen($attachment_file, “r”);
$content = fread($email_handle, $attachment_file_size);
fclose($email_handle);// file close of email handle
$content = chunk_split(base64_encode($content));
$myLivetime = md5(time());// a random hash will be necessary to send mixed content

Also Read This 👉   Send Simple Mail Function using PHP

// carriage return type (we use a PHP end of line constant)
$mail_seol = PHP_EOL;
$body_message = $body_text;
// main header (multipart mandatory)
$mail_header = “From: Pakainfo.com Team <[email protected]>” . $mail_seol;
//Pakainfo.com sender eamil detail
$mail_header .= “MIME-Version: 1.0” . $mail_seol;//MIME-Version
$mail_header .= “Content-Type: multipart/mixed; boundary=\”” . $myLivetime . “\”” . $mail_seol;//Content-Type
$mail_header .= “Content-Transfer-Encoding: 7bit” . $mail_seol;//Content-Transfer-Encoding
$mail_header .= “This is a MIME encoded message.” . $mail_seol;

//Pakainfo.com eamil message
$mail_header .= “–” . $myLivetime . $mail_seol;
$mail_header .= “Content-Type: text/plain; charset=\”iso-8859-1\”” . $mail_seol;
$mail_header .= “Content-Transfer-Encoding: 8bit” . $mail_seol;
$mail_header .= $body_message . $mail_seol;

//Pakainfo.com attachment
$mail_header .= “–” . $myLivetime . $mail_seol;
$mail_header .= “Content-Type: application/octet-stream; name=\”” . $my_file . “\”” . $mail_seol;
$mail_header .= “Content-Transfer-Encoding: base64” . $mail_seol;
$mail_header .= “Content-Disposition: attachment” . $mail_seol;
$mail_header .= $content . $mail_seol;
$mail_header .= “–” . $myLivetime . “–“;
mail($mail_to, $mail_sub, “, $mail_header); //SEND Mail TO USER
[/php]
About PHP’s native mail() function

The attachment simple file has to be PHP uploaded first or we can use a simple file which already here exists on your any linux file or folder web server

Example

I hope you have Got What is Sending HTML-email with Attachment Using PHP’s mail() Function And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Our goal is to create an HTML form with a file field, when someone clicks the submit button we want to receive the data with attachment in our email.

Before we started to this everyone for the big support my HTML form to PHP mail post. As per my subscriber’s request, I made this new tutorial. to send any attachment via HTML form to the email we can use phpmail function. here is am giving an example of how to do it.

Create form for html to email with attachments 

First create an HTML form with enctype=”multipart/form-data” because we are transferring files .

     



this is just a HTML boilerplate. you can add your class and modify with your website design.

Now we have to create our PHP file which will process the data when the form submit.

Here we are getting the data from the form and making a message string and encoding the uploaded attachment into base64 format and send it to your email address,

When the email hit your inbox it will decode the base64 object and display the attachment


mail.php file

[email protected]';  //if u dont have an email create one on your cpanel

    $mailto = '[email protected]';  //the email which u want to recv this email




    $content = file_get_contents($fileName);
    $content = chunk_split(base64_encode($content));

    // a random hash will be necessary to send mixed content
    $separator = md5(time());

    // carriage return type (RFC)
    $eol = "\r\n";

    // main header (multipart mandatory)
    $headers = "From: ".$fromname." <".$fromemail.">" . $eol;
    $headers .= "MIME-Version: 1.0" . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
    $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
    $headers .= "This is a MIME encoded message." . $eol;

    // message
    $body = "--" . $separator . $eol;
    $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    $body .= "Content-Transfer-Encoding: 8bit" . $eol;
    $body .= $message . $eol;

    // attachment
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= "Content-Disposition: attachment" . $eol;
    $body .= $content . $eol;
    $body .= "--" . $separator . "--";

    //SEND Mail
    if (mail($mailto, $subject, $body, $headers)) {
        echo "mail send ... OK"; // do what you want after sending the email
        
        
    } else {
        echo "mail send ... ERROR!";
        print_r( error_get_last() );
    }

      

Important Notes :

  • This will not work in your desktop or Localhost
  • You have to host it in a server to make it work.
  • Some free hosting providers not allow you to send emails from your website (eg : 000webhost)
  • Sometimes you have to add a valid from email address to work with this
  • Maximum file size is 25 , if you are attaching bigger files, it may not work. anyway its depend on your server provider.

Check out the video for more details and to know how to implement html to email with attachments

Download Source Code  : 

PHP mail HTML format with attachment

If you are still having doubts about how to work with HTML to email with attachments forms you can check out my Youtube video explaining everything in detail.

Can i send emails with attachments in PHP

Yes, you can send emails with attachments in PHP

How many emails i can send a day

Depends on your hosting provider. As PHP mail() worked based on your hosting.

What is the attachment size Limit

Normally its 25MB. and its depend on hosting provider

can i send emails from my localhost ?

By using this PHP mail() function. You cant send emails from your desktop or local server. But you can if you config a mail server in your localhost.

How to send mail in PHP with attachment file?

The PHP mail() function with some MIME type headers can be used to send email with attachment in PHP. In the following example code, MIME and Content-Type headers are used with mail() function to send email with attachment using PHP. $to – Recipient email address. $from – Sender email address.

How to send HTML form data to an email with attachments using PHP?

Check and validate the file extension to allow certain file formats (PDF, Image, and MS Word files). Upload file to the server using PHP move_uploaded_file() function. Add form data and uploaded file to the email content. Send an email to the specified recipient with an attachment using the PHP mail() function.

How do I add an email attachment in HTML?

Follow the step-by-step tutorial:.
Open the email template editor and access the template's HTML code. ... .
Wherever you want the animated image or GIF to be displayed, paste the following code into the HTML editor: . ... .
To create an attachment field, just click on Dynamic Field and select it..

How to add attachment in PHPMailer?

The command to attach a file can be placed anywhere between $mail = new PHPMailer(); and !$ mail->Send(); and it's called AddAttachment($path); . This single line will add the attachment to your mail. $path is the path of the filename.