发送电子邮件 - 基础知识更多详细信息和完整示例

典型的电子邮件有三个主要组件:

  1. 收件人(表示为电子邮件地址)
  2. 主题
  3. 消息体

用 PHP 发送邮件就像调用内置函数 mail() 一样简单。mail() 最多需要五个参数,但前三个是发送电子邮件所需的全部内容(尽管常用的四个参数如下所示)。前三个参数是:

  1. 收件人的电子邮件地址(字符串)
  2. 电子邮件的主题(字符串)
  3. 电子邮件的正文(字符串)(例如电子邮件的内容)

一个最小的例子类似于以下代码:

mail('recipient@example.com', 'Email Subject', 'This is the email message body');

上面的简单示例适用于有限的情况,例如硬编码内部系统的电子邮件警报。但是,通常将作为 mail() 参数传递的数据放在变量中,以使代码更清晰,更易于管理(例如,从表单提交动态构建电子邮件)。

此外,mail() 接受第四个参数,允许你随电子邮件发送其他邮件标题。这些标题可以让你设置:

  • 用户将看到的 From 名称和电子邮件地址
  • 用户响应将发送到的 Reply-To 电子邮件地址
  • 其他非标准标题如 X-Mailer 可以告诉收件人这封电子邮件是通过 PHP 发送的
$to      = 'recipient@example.com';             // Could also be $to      = $_POST['recipient'];  
$subject = 'Email Subject';                     // Could also be $subject = $_POST['subject'];  
$message = 'This is the email message body';    // Could also be $message = $_POST['message'];  
$headers = implode("\r\n", [
    'From: John Conde <webmaster@example.com>',
    'Reply-To: webmaster@example.com',
    'X-Mailer: PHP/' . PHP_VERSION
]);

可选的第五个参数可用于将其他标志作为命令行选项传递给配置为在发送邮件时使用的程序,如 sendmail_path 配置设置所定义。例如,当使用带有 -f sendmail 选项的 sendmail / postfix 时,这可用于设置信封发件人地址。

$fifth  = '-fno-reply@example.com';

虽然使用 mail() 可能非常可靠,但绝不保证在调用 mail() 时会发送电子邮件。要查看发送电子邮件时是否存在潜在错误,你应该从 mail() 获取返回值。如果邮件成功接受发送,将返回 TRUE。否则,你将收到 FALSE

$result = mail($to, $subject, $message, $headers, $fifth);

:虽然 mail() 可能返回 TRUE,但这并不意味着电子邮件被发送或电子邮件将被接收者接收。它仅表示邮件已成功切换到系统的邮件系统。

如果你希望发送 HTML 电子邮件,则无需执行更多工作。你需要:

  1. 添加 MIME-Version 标头
  2. 添加 Content-Type 标头
  3. 确保你的电子邮件内容是 HTML
$to      = 'recipient@example.com';                            
$subject = 'Email Subject';                                     
$message = '<html><body>This is the email message body</body></html>';       
$headers = implode("\r\n", [
    'From: John Conde <webmaster@example.com>',
    'Reply-To: webmaster@example.com',
    'MIME-Version: 1.0',
    'Content-Type: text/html; charset=ISO-8859-1',
    'X-Mailer: PHP/' . PHP_VERSION
]);

这是使用 PHP 的 mail() 函数的完整示例

<?php

// Debugging tools. Only turn these on in your development environment.

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

// Special mail settings that can make mail less likely to be considered spam
// and offers logging in case of technical difficulties.

ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);

// The components of our email

$to      = 'recipient@example.com';
$subject = 'Email Subject';
$message = 'This is the email message body';
$headers = implode("\r\n", [
    'From: webmaster@example.com',
    'Reply-To: webmaster@example.com',
    'X-Mailer: PHP/' . PHP_VERSION
]);

// Send the email

$result = mail($to, $subject, $message, $headers);

// Check the results and react accordingly

if ($result) {
  
    // Success! Redirect to a thank you page. Use the
    // POST/REDIRECT/GET pattern to prevent form resubmissions
    // when a user refreshes the page.
  
    header('Location: http://example.com/path/to/thank-you.php', true, 303);
    exit;
  
}
else {
  
    // Your mail was not sent. Check your logs to see if
    // the reason was reported there for you.
  
}

也可以看看

官方文件

相关的 Stack Overflow 问题

替代邮寄者

电邮服务器

相关话题