Send PHP HTML mail with attachments

前端 未结 4 2055
清歌不尽
清歌不尽 2020-11-30 02:41

I got a problem: Until today, I sent HTML mails with PHP using a header which contains

Content-type: text/html;

Now, I added functionality

4条回答
  •  死守一世寂寞
    2020-11-30 03:33

    To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email.Have a look at the example:

     
    --PHP-mixed-  
    Content-Type: multipart/alternative; boundary="PHP-alt-" 
    
    --PHP-alt-  
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit
    
    Hello World!!! 
    This is simple text email message. 
    
    --PHP-alt-  
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit
    
    

    Hello World!

    This is something with HTML formatting.

    --PHP-alt--- --PHP-mixed- Content-Type: application/zip; name="attachment.zip" Content-Transfer-Encoding: base64 Content-Disposition: attachment --PHP-mixed---

    As you can see, sending an email with attachment is easy to accomplish. In the preceding example we have multipart/mixed MIME type, and inside it we have multipart/alternative MIME type that specifies two versions of the email. To include an attachment to our message, we read the data from the specified file into a string, encode it with base64, split it in smaller chunks to make sure that it matches the MIME specifications and then include it as an attachment.

提交回复
热议问题