• Register
57,980 points
11 5 5

Want to solve programming problems and get paid for it? If that sounds interesting to you then contact us.

Sending an email through a script is a very useful feature in any application worth it's salt. The majority of websites usually use the sending of emails to convey various notifications to their users. If your web application uses PHP, it is very easy to send emails.

PHP provides you with an easy method to send emails from the web. Using PHP's mail () function, you can send both text and HTML. But there are times that sending an only text or HTML is a bit short and we need to attach files to these emails. In this tutorial, we will explain how to send emails with attachments with PHP.

How to send an email with attachments

PHP's mail () function can also be used to send emails with attachments, you just have to configure the email headers correctly. In addition, to send the mail you will have to specify the recipient email ($ to), the name of the person who sent it ($ fromName), the subject ($ subject), the email of the person who sent it ($ from), the file to attach ($ file) and the content or body of the email ($ htmlContent). The following script allows you to send both HTML and text emails with attached files.

<? php 
// recipient 
$ to = 'recipient@example.com' ; // sender $ from = 'sender@example.com' ; $ fromName = 'Schedule.net' ; // email subject $ subject = 'PHP Email with Attachment' ; // attachment file path $ file = "file.pdf" ; // email body content $ htmlContent = '<h1> PHP Email with Attachment </h1>     <p> This email has sent from PHP script with attachment. </p>' ; 

 . "<" . $ from. ">" ; // boundary $ semi_rand = md5 ( time ()); $ mime_boundary = "== Multipart_Boundary_x {$ semi_rand} x" ; // headers for attachment $ headers . = "nMIME-Version: 1.0n" . "Content-Type: multipart / mixed; n" . "boundary =" { $ mime_boundary } "" ; // multipart boundary $ message = "- {$ mime_boundary} n" . "Content-Type: text / html; charset ="

"Content-Transfer-Encoding: 7bitnn" . $ htmlContent . "nn" ; // preparing attachment if (! empty ( $ file ) > 0 ) { if ( is_file ( $ file )) {         $ message . = "- {$ mime_boundary} n" ;         $ fp =     @fopen ( $ file , "rb" );         $ data =   @fread ( $ fp , filesize ( $ file ));         @fclose (   

$ fp ); 
        $ data = chunk_split ( base64_encode ( $ data )); 
        $ message . = "Content-Type: application / octet-stream; name =" ". basename ($ file)." "n" . "Content-Description:" . basename ( $ files [ $ i ]) . "n" . "Content-Disposition: attachment; n" . "filename =" ". basename ($ file)." "; size =" . filesize ( $ file ) . "; n" . "Content-Transfer-Encoding:   
         
            
          "nn" ; } } $ message . = "- {$ mime_boundary} -" ; $ returnpath = "-f" . $ from ; // send email $ mail = @mail ( $ to , $ subject , $ message , $ headers , $ returnpath ); // email sending status echo $ mail ? "<h1> Mail sent. </h1>" : "<h1> Mail sending failed. </h1>" ;

Or there’s is another easy method to do it:

Send an email with attachment in PHP. The email theme is widely used in web systems. Most web applications to optimize the reach of their platform make use of this resource, either to send newsletters, notices and information about the platform. If the web application that I develop uses PHP, it is very easy to send the Email from PHP.

Send email with attachment in PHP:

The PHP language provides functions for sending email from the website. Using the mail () function in PHP, you can send text, image, or an attachment. In this short article, we will see how to send an email with attachment in PHP. Our script example makes it simple to send text or HTML mail including any type of file as an attachment using PHP.

 

Send HTML email with an attachment

The Email function of PHP () with some MIME type headers is used to send an email with attachment in PHP.

  1. You must specify the recipient's email ($ to).
  2. The name of the sender ($ fromName).
  3. The sender's email ($ from).
  4. The subject ($ Subject), the file to be attached ($ File)
  5. The body content to send ($ htmlContent).

The following code sequence allows you to send the type of message (text or HTML) with attachment to the desired email.

<? php
//Container
$ to = 'container@miexample.com';

// mail sender
$ from = 'sender@miexample.com';
$ fromName = 'BaulPHP';

//Email subject
$ subject = 'PHP email with BaulPHP attachments'; 

// Path of the attached file
$ file = "mipdf.pdf";

// Email content
$ htmlContent = '<h1> PHP email with BaulPHP attachments </h1>
    <p> This email has sent from PHP script with attachments. </p> ';

// Header for sender information
$ headers = "From: $ fromName". "<". $ from. ">";

// Limit Email
$ semi_rand = md5 (time ()); 
$ mime_boundary = "== Multipart_Boundary_x {$ semi_rand} x"; 

// Headers for attachment 
$ headers. = "\ nMIME-Version: 1.0 \ n". "Content-Type: multipart / mixed; \ n". "boundary = \" {$ mime_boundary} \ "";

// multipart limit
$ message = "- {$ mime_boundary} \ n". "Content-Type: text / html; charset = \" UTF-8 \ "\ n".
"Content-Transfer-Encoding: 7bit \ n \ n". $ htmlContent. "\ n \ n";

// file preparation
if (! empty ($ file)> 0) {
    if (is_file ($ file)) {
        $ message. = "- {$ mime_boundary} \ n";
        $ fp = @fopen ($ file, "rb");
        $ data = @fread ($ fp, filesize ($ file));

        @fclose ($ fp);
        $ data = chunk_split (base64_encode ($ data));
        $ message. = "Content-Type: application / octet-stream; name = \" ". basename ($ file)." \ "\ n". 
        "Content-Description:" .basename ($ files [$ i]). "\ N".
        "Content-Disposition: attachment; \ n". "filename = \" ". basename ($ file)." \ "; size =". filesize ($ file). "; \ n".
        "Content-Transfer-Encoding: base64 \ n \ n". $ data. "\ n \ n";
    }
}
$ message. = "- {$ mime_boundary} -";
$ returnpath = "-f". $ from;

//Send email
$ mail = @mail ($ to, $ subject, $ message, $ headers, $ returnpath); 

// Email delivery status
echo $ mail? "<h1> Mail sent. </h1>": "<h1> Sending of mail failed. </h1>";

Conclusion:

The brief explanation will get you out of a bind, though using the correct implementation of the script. Here we have provided the easy way to send email with attachment.
For this example it is not necessary to include any library to send HTML mail with attachment. Using the php () function you can easily send email with attachment to any recipient.

57,980 points
11 5 5