php_ref_mail
PHP Mail 函数
Section titled “PHP Mail 函数”PHP Mail 函数介绍
Section titled “PHP Mail 函数介绍”PHP 内置的 mail() 函数提供了一种从 PHP 脚本直接发送电子邮件的基本机制。
它常用于发送简单的通知、联系表单提交或密码重置邮件。
重要限制: mail() 函数严重依赖于服务器本地的邮件传输代理(MTA,Mail Transfer Agent)配置(例如 Sendmail 或 Postfix)。它缺乏现代应用程序中稳健发送电子邮件所需的基本功能,例如:
- 简单的 SMTP 身份验证(用户名/密码)。
- 轻松创建带附件的 HTML 电子邮件。
- 内置对不同字符编码的处理。
- 错误处理和反馈(即使电子邮件稍后未能送达,它通常也返回
true)。 - 支持现代电子邮件标准和安全实践(DKIM, SPF 对齐)。
建议: 对于任何超出最基本需求的电子邮件发送场景,强烈建议使用专用的 PHP 邮件库,例如 PHPMailer 或 Symfony Mailer。这些库提供了更可靠、灵活和功能丰富的方式来处理电子邮件发送,包括 SMTP 支持、HTML 电子邮件、附件和更好的错误报告。
mail() 函数的要求
Section titled “mail() 函数的要求”为了使 mail() 函数正常工作,运行 PHP 的服务器必须安装并正确配置了邮件传输代理 (MTA) 并使其正常运行(例如 Sendmail, Postfix, Exim)。PHP 需要知道如何与这个 MTA 交互。
所使用的具体程序和设置通常在 php.ini 配置文件中定义。
mail() 函数是 PHP 核心的一部分。无需单独安装 PHP 扩展。但是,底层的服务器级邮件系统必须正确安装和配置。
运行时配置 (php.ini)
Section titled “运行时配置 (php.ini)”mail() 函数的行为受 php.ini 中的设置影响。主要指令包括:
Directive Default 描述 可修改范围------------------ ------------- ---------------------------------------------------------------------- --------------; For Unix-like systems using sendmail/postfix etc.sendmail_path (varies) Path to the sendmail program (e.g., "/usr/sbin/sendmail -t -i"). PHP_INI_SYSTEM e.g., "/usr/sbin/sendmail -t -i"
; For Windows systems (or Unix using SMTP relay)SMTP localhost Hostname or IP address of the SMTP server to use. PHP_INI_ALLsmtp_port 25 Port number for the SMTP server. PHP_INI_ALLsendmail_from (null) Sets the "From:" address used in mail sent from Windows. PHP_INI_ALL
; General mail settingsmail.add_x_header On Add 'X-PHP-Originating-Script' header showing script filename. PHP_INI_PERDIRmail.log (null) Path to a log file to record all mail() calls (useful for debugging). PHP_INI_SYSTEM or PHP_INI_PERDIRmail.force_extra_parameters (null) Force addition of specified parameters to sendmail command. PHP_INI_SYSTEM注意: PHP_INI_SYSTEM 表示该设置只能在 php.ini 或 httpd.conf 中修改。PHP_INI_PERDIR 允许在 php.ini、.htaccess、httpd.conf 或 .user.ini 中修改。PHP_INI_ALL 允许在任何地方修改,包括通过 ini_set() 函数。
正确配置这些设置,特别是 sendmail_path (Unix) 或 SMTP/smtp_port (Windows/SMTP 中继),对于 mail() 函数的正常工作至关重要。
PHP Mail 函数
Section titled “PHP Mail 函数”| 函数 | 描述 |
|---|---|
| mail() | 发送电子邮件。接受收件人、主题、消息、头部信息和附加参数等参数。 |
| ezmlm_hash() | 计算 EZMLM 邮件列表管理器所需的哈希值(极少使用)。 |