Send PHP mail with Attachment
This custom function or PHP mail attachment script is able to send a plain text email message together with a single attachment file.
You probably know how to send email with PHP, it’s just few lines of code, but it gets bit tricky when you want to send an attachment with PHP email and should be able to deliver emails to Gmail and Hotmail without regarding as SPAM..
The attachment file has to be uploaded first or you can use a file which already exists on your web server. There are much better and more advanced PHP scripts on the Internet, but I hope this example will help you to understand, how it’s possible to send an email message plus attachment by using some PHP code.
So, today let’s find-out how we can send email with an attachment using PHP mail. Create a HTML form with file input field similar to below, enctype attribute should be “multipart/form-data“, so that form-data is encoded as “multipart/form-data” when sent to server.
HTML Mailing Form for the example ( php mail function with image/documents attachment )
About PHP’s native mail() function
A workaround for sending attachment using PHP mail() is to construct a MIME header which contains the attachment information. The following piece of codes should help.
Below i provide the complete PHP code that sends PHP email with attachment. Just create a PHP file with code below and point your HTML form to the file, and don’t forget to change recipient address and domain email address in the code.
By default, the PHP mail() does not support sending email with attachment. In order to send an attachment, u can either use the PEAR package or PHPMailer. But in reality, the hosting server may not provide these kinds of third party libraries.