首页  >  文章  >  后端开发  >  使用 PHP 安全地传送电子邮件:使用 SMTP 发送无垃圾邮件的指南

使用 PHP 安全地传送电子邮件:使用 SMTP 发送无垃圾邮件的指南

Barbara Streisand
Barbara Streisand原创
2024-09-28 06:08:01487浏览

Deliver Emails Safely with PHP: A Guide to Using SMTP for Spam-Free Emails

以下是如何使用 PHP SMTP 发送电子邮件而不进入垃圾邮件文件夹的分步示例

我们将使用 PHPMailer 库,它简化了通过 SMTP 发送电子邮件的过程,并有助于提高送达率。按照以下步骤,您将了解如何正确配置 SMTP 以避免电子邮件进入垃圾邮件文件夹。

第1步:安装PHPMailer

首先,您需要安装 PHPMailer 库。您可以使用 Composer 来完成此操作。

composer require phpmailer/phpmailer

如果您没有 Composer,您可以从 GitHub 手动下载 PHPMailer 并将其包含在您的项目中。

第 2 步:创建 PHP 邮件脚本

创建一个新文件 send_email.php,您将在其中编写脚本以使用 PHPMailer 和 SMTP 发送电子邮件。

<?php
// Load Composer's autoloader if using Composer
require 'vendor/autoload.php';

// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->isSMTP();                                          // Use SMTP
    $mail->Host = 'smtp.example.com';                         // Set the SMTP server (use your SMTP provider)
    $mail->SMTPAuth = true;                                   // Enable SMTP authentication
    $mail->Username = 'your_email@example.com';               // SMTP username
    $mail->Password = 'your_password';                        // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;       // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                        // TCP port to connect to (587 is common for TLS)

    //Recipients
    $mail->setFrom('your_email@example.com', 'Your Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');  // Add recipient
    $mail->addReplyTo('reply_to@example.com', 'Reply Address');    // Add a reply-to address

    // Content
    $mail->isHTML(true);                                        // Set email format to HTML
    $mail->Subject = 'Test Email Subject';
    $mail->Body    = 'This is a <b>test email</b> sent using PHPMailer and SMTP.';
    $mail->AltBody = 'This is a plain-text version of the email for non-HTML email clients.';

    // Send the email
    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

逐部分细分:

  1. PHPMailer 初始化:

    • 通过 Composer 包含必要的文件后,我们创建 PHPMailer 类的实例。
    • try-catch 块用于处理发送电子邮件时可能发生的任何异常。
  2. SMTP 服务器配置:

    • $mail->isSMTP() 告诉 PHPMailer 使用 SMTP 发送电子邮件。
    • $mail->Host = 'smtp.example.com':将 'smtp.example.com' 替换为您的 SMTP 提供商的主机(例如,Gmail 的 SMTP 是 'smtp.gmail.com')。
    • $mail->SMTPAuth = true:这将启用身份验证,这通常是 SMTP 服务器所需要的。
    • $mail->用户名和$mail->密码:在此处输入您的 SMTP 凭据。
    • $mail->SMTPSecure:设置加密方法。为了安全起见,建议使用 TLS (STARTTLS)。
    • $mail->Port:常见的 SMTP 端口为 TLS 的 587 和 SSL 的 465。使用您的 SMTP 服务器所需的端口。
  3. 设置发件人和收件人:

    • $mail->setFrom('your_email@example.com', 'Your Name'): 指定发件人的电子邮件和姓名。
    • $mail->addAddress('recipient@example.com', '收件人姓名'): 添加收件人的电子邮件和姓名。
    • $mail->addReplyTo(): 设置回复电子邮件地址(可选)。
  4. 设置邮件内容:

    • $mail->isHTML(true): 指定电子邮件内容将为 HTML。
    • $mail->Subject:设置电子邮件主题。
    • $mail->Body:这是电子邮件的 HTML 内容。
    • $mail->AltBody:这是电子邮件正文的纯文本版本,对于不支持 HTML 的电子邮件客户端很有用。
  5. 发送电子邮件

    • $mail->send() 尝试发送电子邮件。如果成功,则打印成功信息;否则,错误将被捕获并显示。

步骤 3:SMTP 配置和最佳实践

为了避免电子邮件进入垃圾邮件文件夹,遵循以下最佳实践至关重要:

  1. 使用信誉良好的 SMTP 提供商

    使用 Gmail、SendGrid 或 Mailgun 等受信任的 SMTP 提供商可以提高送达率,因为它们不太可能被标记为垃圾邮件。

  2. 验证您的域名:

    为您的邮件设置 SPF(发件人策略框架)、DKIM(域名密钥识别邮件)和 DMARC(基于域的邮件身份验证、报告和一致性)记录域来验证您的电子邮件的合法性。

  3. 避免垃圾内容

    确保您的电子邮件内容干净且未被标记为垃圾邮件。避免过度使用全部大写、垃圾词(如“免费”、“获胜者”等)以及过多的链接。

  4. 使用纯文本替代:

    始终包含电子邮件的纯文本版本 ($mail->AltBody)。一些电子邮件客户端将纯 HTML 电子邮件标记为可疑。

  5. 避免使用免费电子邮件服务作为发件人

    使用您自己域中的专业电子邮件地址,而不是 Gmail、Yahoo 等免费服务,以避免被标记为垃圾邮件。

  6. 限制每封电子邮件的收件人数量

    如果发送批量电子邮件,请使用适当的批量电子邮件服务,而不是在一封邮件中发送给多个收件人,以避免被标记为垃圾邮件。

第 4 步:运行脚本

将send_email.php文件上传到您的服务器并在浏览器中或通过命令行运行它:

php send_email.php

如果配置正确,您将看到消息:

Message has been sent

If there’s an error, it will display:

Message could not be sent. Mailer Error: {Error Message}

Conclusion

By using PHPMailer and a proper SMTP setup, you can ensure your emails are sent reliably and with a lower chance of landing in the spam folder. Here's a quick summary:

  1. Install PHPMailer to simplify email sending with SMTP.
  2. Configure SMTP correctly with authentication and encryption.
  3. Use proper domain verification (SPF, DKIM, DMARC) to increase deliverability.
  4. Write clean, non-spammy email content and always include a plain-text version.
  5. Run the script and monitor the success message or error log.

This approach ensures better deliverability and reduces the chances of your emails being marked as spam.

Feel free to follow me:

  • LinkedIn
  • GitHub

以上是使用 PHP 安全地传送电子邮件:使用 SMTP 发送无垃圾邮件的指南的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn