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. 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.
Here’s 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 email address in the code.
If you want bit advance Ajax version please check here.
That’s it, you can capture subject and message body from HTML form and use them in the code. I hope this code will be useful in your PHP projects. Good luck!