PHP mail() Function Code to Send Emails from a Form Updated on February 11, 2025 by Carrie Smaha 2 Minutes, 39 Seconds to Read You can use the PHP mail() function to send an email with PHP and the simplest way to do this is to send a text email when a visitor to your website fills out a form. NOTE: Mail() is not available on shared hosting. (Since you’re interested in sending email via PHP, we’re assuming you have a live website. Is that not the case? Let’s start by getting you hooked up with the perfect web hosting package.) Table of Contents Basic PHP email() function code How PHP mail() works Leveraging PHPMailer for Advanced Email Functionality Conclusion Basic PHP email() function code Below is the code for the basic email function. We can take the script and use a form on our website to set the variables in the script above to send an email. <?php // if "email" variable is filled out, send email if (isset($_REQUEST['email'])) { //Email information $to = "[email protected]"; $email = $_REQUEST['email']; $subject = $_REQUEST['subject']; $message = $_REQUEST['message']; //Send email mail($to, $subject, $message, "From:" . $email); //Email response echo "Thank you for contacting us!"; } //if "email" variable is not filled out, display the form else { ?> <form method="post"> Email: <input name="email" type="text" /> Subject: <input name="subject" type="text" /> Message: <textarea name="message" rows="15" cols="40"></textarea> <input type="submit" value="Submit" /> </form> <?php } ?> If you want to include multiple recipients on the email, you can update this line as follows: $to = "[email protected], [email protected]"; How PHP mail() works The first part of the form checks to make sure the email input field is filled out. If it isn’t, displays the HTML form on the page. If the email is set (after the visitor fills out the form), it is ready to send. When the submit button is pressed after the form is filled out, the page reloads, reads that the email input is set and it sends the email. Leveraging PHPMailer for Advanced Email Functionality In addition to utilizing the native PHP mail() function for sending emails from your web forms, you have the option to explore more advanced email capabilities through a third-party library known as PHPMailer. While this article has primarily focused on demonstrating the usage of the mail() function, it’s worth noting that PHPMailer offers an array of features that can greatly enhance your email sending process. If you find yourself in need of secure SMTP authentication, streamlined attachment handling, or comprehensive error reporting, considering PHPMailer as an alternative solution could provide you with the tools to address these requirements seamlessly. Conclusion Keep in mind, this is a basic tutorial explaining how to use the mail() function in PHP but it can be insecure and you should generally avoid using it. The purpose of this article is to provide you the basics of how to use phpmail() but if you want to do more with it, you may want to look into securing your code to possible hacks. To learn more about the PHP email function, please see the article on How to create a custom PHP contact form with more information on validation and error checking. If you need further assistance, feel free to ask a question on our support center. Share this Article Carrie Smaha Senior Manager Marketing Operations Carrie Smaha is a Senior Marketing Operations leader with over 20 years of experience in digital strategy, web development, and IT project management. She specializes in go-to-market programs and SaaS solutions for WordPress and VPS Hosting, working closely with technical teams and customers to deliver high-performance, scalable platforms. At InMotion Hosting, she drives product marketing initiatives that blend strategic insight with technical depth. More Articles by Carrie Related Articles Transferring Emails from Your Old Host to InMotion Hosting How to Setup Office 365 DNS Records How to fix OLMAPI32.dll and WWLIB.dll error in Outlook 2013 How to Setup Outlook 2016 for Mac Issues with Outlook 2007 Running on Windows 10 Outlook not working after installing Windows 10 IMAP Issues Affecting Outlook 2013 and Office 365 Outlook 2013/Office 365 – Subscribing to your Inbox How to create an email signature in Outlook 2003 How to create an email signature for Mac in Outlook 2011