Home>Article>Backend Development> How to send email in php? PHP mail() function implements sending emails
How to send email in php? This article will introduce to you how to use the PHP mail() function to send emails. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
mail() is a built-in function of PHP that allows sending emails using the native sendmail program. Whenever the mail() function is called, it calls the local sendmail program, which is usually configured by the system administrator. If your web host is on Hostinger, you can enable/disable this feature in the Email - >Mail Service Control section.
By default, the sendmail service is self-starting (self-starting). But if not, you can do it in just a few clicks.
Let’s take a look at thePHP mail() function to implement the steps to send an email:
Step 1 - Create a test file for PHP email
First, we need to create a file for the PHP email script. We place it in the public_html directory so that it can be easily accessed via the domain name. You can use a file manager or FTP client to perform this task. We name the file testmail.php.
Step 2 - Understanding PHP Email
Let’s introduce ourselves to the components of a basic PHP email script
<?PHP ini_set( 'display_errors', 1 ); error_reporting( E_ALL ); $from = "test@hostinger-tutorials.com"; $to = "test@gmail.com"; $subject = "Checking PHP mail"; $message = "PHP mail works just fine"; $headers = "From:" . $from; mail($to,$subject,$message, $headers); echo “电子邮件已发送。” ; ?>
The first two lines enable error reporting. This will help if the script fails to execute. The remaining components are:
For more information about sendmail and its components, see the official PHP documentation (http://php.net/manual/en/ function.mail.php).
Step 3 - Run the PHP Mail Script
If you place the PHP script in the public_html directory, you can access it via YourDomain.com/testmail. php to run it. You should see the following message:
Congratulations! Now understand the basic syntax of PHP email and can use it to send emails.
The above is the detailed content of How to send email in php? PHP mail() function implements sending emails. For more information, please follow other related articles on the PHP Chinese website!