Php mail function config
The PHP mail function!
The PHP mail function is used to send emails from a PHP script. Here's a basic configuration guide to get you started:
1. Server Configuration
- Make sure your server has a working mail server (e.g., Sendmail, Postfix, or Exim) installed and configured.
- Ensure that the mail server is properly configured to send emails.
2. PHP Configuration
- In your PHP configuration file (
php.ini
), set the following settings:sendmail_path
: Set this to the path of your mail server's sendmail executable (e.g.,/usr/sbin/sendmail
).SMTP
: Set this to the hostname or IP address of your mail server (e.g.,mail.example.com
).smtp_port
: Set this to the port number used by your mail server (e.g.,25
).mail.add_x_header
: Set this toOn
to add aX-PHP-Originating-Script
header to the email.mail.log
: Set this to a log file path to log email sending errors.
Example:
[mail function]
sendmail_path = /usr/sbin/sendmail
SMTP = mail.example.com
smtp_port = 25
mail.add_x_header = On
mail.log = /var/log/phpmail.log
3. PHP Mail Function
- Use the PHP mail function (
mail()
) to send emails. The basic syntax is:mail($to, $subject, $message, $headers);
$to
: The recipient's email address.$subject
: The email subject.$message
: The email body.$headers
: An array of headers (e.g.,From
,To
,CC
,BCC
, etc.).
Example:
<?php
$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent from PHP.';
$headers = array(
'From' => '[email protected]',
'To' => $to,
'Subject' => $subject
);
mail($to, $subject, $message, $headers);
?>
4. Additional Tips
- Make sure to set the
From
header to a valid email address that is monitored. - Use a valid
To
header to ensure the email is delivered to the correct recipient. - Use a valid
Subject
header to ensure the email is properly labeled. - You can use the
mail()
function with additional parameters to specify the email encoding, charset, and other settings.
By following these steps, you should be able to configure the PHP mail function to send emails from your PHP script.