Site icon Learn Web Development & Programming, Artificial Intelligence, WordPress, Shopify Articles

How to send attachment in an email on form submission using PHP

send mail with attachment in php

Contact or feedback form is utilized for online communication and the submitted form data is sent vie email instantly. At the point when the contact form has a document upload field, the document should be sent with email as a attachment. Using the php mail() function, you can send email with attachment and form data in PHP without much of a stretch. In this instructional tutorials, we will guide you how to send email with attachment on form submission using PHP.

The following code gives a moment capacity to make a contact form with document attachment option and use code on the site. Additionally, an email will be sent to a particular email address with a document file attachment. For the better understanding, we will break the PHP contact form with email and document file attachment code in two sections, HTML (Web Form) and PHP (Form Submission). You can put the two sections of the code together in the site page where you need to use contact form with a document file attachment.

Contact Form HTML with File Upload Field

Most importantly we require a HTML Form through which we will upload the file. The following is the HTML Form code with some basic fields (Name, Email, Subject, and Message) and a file input field.

Note the “enctype” attribute in form tag. This is a attribute that determines the encoding type utilized while sending data to server. At whatever point we utilize any file input tag, we have to determine this attribute in form tag with value “multipart/form-data”. This encoding type “multipart/form-data” tells that no character will be encoded while sending. It helps in saving the data of uploaded document file. One more thing to take note that the enctype attribute is used with post method.

Presently let’s take a look at the php code that does the real task of sending uploaded document file to email address.

Complete Code:

Conclusion

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Exit mobile version