
这是如何使用 php smtp 发送电子邮件而不进入垃圾邮件文件夹的分步示例。
我们将使用 phpmailer 库,它简化了通过 smtp 发送电子邮件的过程,并有助于提高送达率。按照以下步骤,您将了解如何正确配置 smtp 以避免电子邮件进入垃圾邮件文件夹。
首先,您需要安装 phpmailer 库。您可以使用 composer 来完成此操作。
composer require phpmailer/phpmailer
如果您没有 composer,您可以从 github 手动下载 phpmailer 并将其包含在您的项目中。
创建一个新文件 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}";
}
phpmailer 初始化:
立即学习“PHP免费学习笔记(深入)”;
smtp 服务器配置:
设置发件人和收件人:
设置邮件内容:
phpList提供开源电子邮件营销服务,包括分析、列表分割、内容个性化和退信处理。丰富的技术功能和安全稳定的代码基础是17年持续开发的结果。在95个国家使用,在20多种语言中可用,并用于去年发送了250亿封电子邮件活动。您可以使用自己的SMTP服务器部署它,或在http://phplist.com上获得免费的托管帐户。
14
发送电子邮件:
为了避免电子邮件进入垃圾邮件文件夹,遵循以下最佳实践至关重要:
使用信誉良好的 smtp 提供商:
使用 gmail、sendgrid 或 mailgun 等受信任的 smtp 提供商可以提高送达率,因为它们不太可能被标记为垃圾邮件。
验证您的域名:
为您的邮件设置 spf(发件人策略框架)、dkim(域名密钥识别邮件)和 dmarc(基于域的邮件身份验证、报告和一致性)记录域来验证您的电子邮件的合法性。
避免垃圾内容:
确保您的电子邮件内容干净且未被标记为垃圾邮件。避免过度使用全部大写、垃圾词(如“免费”、“获胜者”等)以及过多的链接。
使用纯文本替代:
始终包含电子邮件的纯文本版本 ($mail->altbody)。一些电子邮件客户端将纯 html 电子邮件标记为可疑。
避免使用免费电子邮件服务作为发件人:
使用您自己域中的专业电子邮件地址,而不是 gmail、yahoo 等免费服务,以避免被标记为垃圾邮件。
限制每封电子邮件的收件人数量:
如果发送批量电子邮件,请使用适当的批量电子邮件服务,而不是在一封邮件中发送给多个收件人,以避免被标记为垃圾邮件。
将send_email.php文件上传到您的服务器并在浏览器中或通过命令行运行它:
php send_email.php
如果配置正确,您将看到消息:
message has been sent
如果有错误,会显示:
Message could not be sent. Mailer Error: {Error Message}
通过使用 phpmailer 和正确的 smtp 设置,您可以确保您的电子邮件可靠发送,并且降低进入垃圾邮件文件夹的机会。这是一个快速总结:
此方法可确保更好的送达率并减少电子邮件被标记为垃圾邮件的机会。
欢迎关注我:
以上就是使用 PHP 安全地传送电子邮件:使用 SMTP 发送无垃圾邮件的指南的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号