1. Introduction
Mail() function, you can use this function to send emails.
Requirements
In order to use the Mail function, PHP must have the ability to compile and send binary mail files in your system. If you use other mail programs, such as qmail or postfix, you must be confident that you can use them to send mail packages. PHP will first look for sendmail on your path, so the following paths: :/usr/bin :/usr/sbin :/usr/etc :/etc:/usr/ucblib :/usr/lib are recommended. Users who compile PHP must also have access to binary sendmail.
Installation
These functions are part of the PHP core and can be used without being installed.
Runtime Configuration
The behavior of these functions is affected by the global Configuration file php.ini.
Table 1. Mail basic configuration options:
Name Default value Changeable value
SMTP "localhost" PHP_INI_ALL
smtp_port "25" PHP_INI_ALL
sendmail_from NULL PHP_INI_ALL
sendmail_path DEFAULT_SENDMAIL_PATH PHP_INI_SYSTEM
For more configuration options about Mail, please see the ini_set() function. The following is a brief explanation of this configuration option.
SMTP string
Used in Windows only: DNS name or IP address of the SMTP server. PHP will use the SMTP server when sending emails using the mail() function.
smtp_portint
Used in Windows only: Set the port number to connect to the specified SMTP server when sending mail using the mail() function. Default: 25. Only available after PHP 4.3.0.
sendmail_from string
When PHP sends emails in Windows systems, it will use the "From:" email address.
sendmail_path string
Where to find the sending mail program. Usually in: /usr/sbin/sendmail or /usr/lib/. The configuration options here are set to a default value so that they work properly for you. But if it fails, you can set it here.
If your system cannot use sendmail, you should follow these instructions to set up a sendmail wrapper/replacement for the mail system they provide. For example, Qmail users can set it to a new path: /var/qmail/bin/sendmail or /var/qmail/bin/qmail-inject.
qmail - handles mail correctly without any options.
Resource Type
This extension module does not define any resource type.
Predefined constants
This extension module does not define any constants
2. Usage method
Syntax:
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])
mail() The function can send the specified message (string message) to the specified email address (string to). Multiple email addresses must be separated by commas. You can use this function to send email attachments and special types.
The meaning of each parameter in the above syntax is:
string to——the email address of the recipient,
string subject——the subject of the email,
string message——the email Text,
string additional_headers - add additional email information to the head and tail of the letter.
If the email is successfully sent, the mail() function will return TRUE, otherwise it will return FALSE. Notice!
The execution method of the mail() function in Windows is different from UNIX systems in many ways.
1. It does not use local binary to form the message body;
2. Headers elements like From:, Cc:, Bcc: and Date: are not interpreted by MTA at first, but can be interpreted by PHP. PHP < 4.3 only supports Cc: and is case-sensitive, PHP >= 4.3 supports all mentioned header elements and is not case-sensitive.
2.1. Example 1. Send an email:
mail("joecool@example.com", "My Subject", "Line 1nLine 2nLine 3");
2.2. If the fourth parameter is used, this parameter string will be inserted into the header and tail of the letter. This is typically inserted to illustrate additional header information. Multiple additional header information must be separated by carriage return r and line feed n characters.
Example 2. Send an email with additional header information:
mail("nobody@example.com", "the subject", $message,"From: webmaster@{$_SERVER[’SERVER_NAME’]}rn" ."Reply-T webmaster@{$_SERVER[’SERVER_NAME’]}rn"."X-Mailer: PHP/" .phpversion());
2.3. If the additional_parameters parameter is used, the program uses the sendmail_path configuration to set an additional parameter when sending the email. For example, this can be used to set the sender's address envelope when using the -f option before mailing. When you set up the sender envelope using this method, you may need to add the user that your web server runs as to your sendmail configuration to prevent a 'X-Warning' header from being added to the message when you set the envelope sender using this method.
Example 3. Sending an email with additional header information and appending a command parameter.
mail("nobody@example.com", "the subject", $message,"From: webmaster@{$_SERVER[’SERVER_NAME’]}", "-fwebmaster@{$_SERVER[’SERVER_NAME’]}");
注意:其中的第五个参数附加在PHP 4.0.5版本中。在PHP 4.2.3以后的版本中的安全模式中被禁止,如果用它的话将返回一个警告信息和返回FALSE值。
你同样能用简单的字符串构造技术建立一个复杂的邮件消息。
例子 4. 发送一个复杂邮件
/*收件人*/ $to = "Mary " . ", " ; //注意逗号 $to .= "Kelly "; /*主题*/ $subject = "Birthday Reminders for August"; /*正文*/ $message = ’ Here are the birthdays upcoming in August! ’; /*你能设置头内容:Content-type来发送HTML格式邮件。*/ $headers = "MIME-Version: 1.0rn"; $headers .= "Content-type: text/html; charset=iso-8859-1rn"; /*附加头消息*/ $headers .= "From: Birthday Reminder rn"; $headers .= "Cc: birthdayarchive@example.comrn"; $headers .= "Bcc: birthdaycheck@example.comrn"; /*发送它*/ mail($to, $subject, $message, $headers); @example.com>@example.com>@example.com>
注意:
1、不要在收件人地址和主题中有换行符号,否则邮件可能不能被发送出去。
2、收件人地址参数(string to)中不能存在以"Something
3、PHP使用mail函数发送邮件标题乱码问题 PHP程序使用mail()函数发送邮件的时候,标题中文的话会出现乱码。
解决方法:
先用函数base64_encode() — 使用 MIME base64 对数据进行编码 标题字符串前加编码类型例如: =?UTF-8?B? 标题字符串后加:?= 邮件header说明Content-type — 防止邮件正文也乱码
举例:
$to = 'xinple@example.com'; $subject = "=?UTF-8?B?".base64_encode('邮件标题')."?="; $headers = 'MIME-Version: 1.0' . "rn"; $headers .= 'Content-type: text/html; charset=utf-8' . "rn"; // Additional headers $headers .= 'To: Xinple' . "rn"; $headers .= 'From: Admin ' . "rn"; $headers .= 'Reply-To: Xinple' . "rn"; mail($to, $subject, $message, $headers);@example>@example.com>@example.com
The above is the detailed content of Detailed explanation of usage examples of php mail() function to send emails. For more information, please follow other related articles on the PHP Chinese website!