Email, Messaging, & Video Calls > Email How to Send Email From a PHP Script Learn how to program a web page to send PHP mail By Heinz Tschabitscher Heinz Tschabitscher Writer University of Vienna A former freelance contributor who has reviewed hundreds of email programs and services since 1997. lifewire's editorial guidelines Updated on June 15, 2022 Tweet Share Email Tweet Share Email In This Article Expand Jump to a Section PHP Mail Script Example More PHP Email Options Protect Your Script From Spammers What to Know Script: Enter ini_set( 'display_errors', 1 ); error_reporting( E_ALL ); $to = "youremailrecipient@example.com"Next: $subject = "Your subject here"; $message = "Your message here"; echo "The email message was sent."; ?Fill in relevant details (email address, email subject, message, etc.) as needed. This article explains how to send an email from a PHP script running on a webpage by using a Sendmail program to send emails from your web server rather than your mail server. PHP Mail Script Example If you implement this example, make sure to change the elements that apply to you, such as your email address, subject, and message. ini_set( 'display_errors', 1 );error_reporting( E_ALL );$to = "youremailrecipient@example.com";$subject = "Your subject here";$message = "Your message here";echo "The email message was sent.";?> The first two lines allow you to see any errors that may occur with the script: ini_set( 'display_errors', 1 );error_reporting( E_ALL ); Next is the To line. This is the email address the email should be sent to. This email address could be yours or someone else's, depending on why you're using the PHP mail function. $to = "youremailrecipient@example.com"; In the Subject line, type whatever you want to be used as the subject of the emails sent through this PHP script. $subject = "Your subject here"; The Message line is where the body of the email goes. $message = "Your message here"; Use the \n parameter to add a new line to the message so that it doesn't all display on a single line to the recipient. Add more than one, if you need to. The echo message would be a success or error message that displays on the page if the other parameters aren't properly filled out. echo "The email message was sent."; You can also send email from your PHP script using SMTP authentication, specifying whether the PHP email script should use a local or remote SMTP server for sending messages. More PHP Email Options Here are a few more options to keep in mind: If you want to include a From header line, it's easy to add it to your PHP script. The mail() function included with stock PHP doesn't support SMTP authentication. If you need this function, send the email using SMTP authentication. To make sure users enter an actual email address, validate the text field to make sure it contains an email-like structure. If you want to specify the receiver's name in addition to the To address, add the name within the quotes and then put the email address in brackets. Learn more about PHP's send mail function at PHP.net. Protect Your Script From Spammers If you use the mail() function, especially in combination with a web form, make sure you check that it's called from the desired page and protect the form with something like a CAPTCHA. It's also important to check for suspicious strings, for example, a Bcc: followed by a number of email addresses. Was this page helpful? Thanks for letting us know! Get the Latest Tech News Delivered Every Day Subscribe Tell us why! Other Not enough details Hard to understand Submit