使用 PHPMailer 发送电子邮件而无需 SMTP 身份验证 [英] Send email using PHPMailer without SMTP authentication

查看:111
本文介绍了使用 PHPMailer 发送电子邮件而无需 SMTP 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不经常使用 PHP,但是当我使用 PHP 并且需要编写一个发送电子邮件的函数时,我只使用了 mail() 函数.我在共享托管服务上使用过它,但我总是收到来自……嗯……不是帐户的电子邮件?机器人?它甚至没有电子邮件地址.

I don't use PHP that often but when I do and I need to write a function to send E-Mails, I just used the mail() function. I have used it on a shared hosting service and I always received the E-Mails from a... well... not an account? A bot? It didn't even have an E-Mail address.

这就是我此时此刻想要实现的 - 向我发送电子邮件,而无需真正连接到 SMTP 服务器或通过身份验证.如何使用 PHPMailer 做到这一点?在我的情况下甚至可能吗?如果你以某种方式得到了我的问题,这样的电子邮件是怎么称呼的?那些……不是由……嗯……电子邮件帐户发送的?

And that's what I want to achieve at this very moment - send an E-Mail to me without really connecting to a SMTP server or going through authentication. How can be that done with PHPMailer? Is it even possible in my case? And if you somehow got my question, how are such E-Mails even called; the ones that... aren't sent by... well... an E-Mail account?

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master\src\Exception.php';
require 'PHPMailer-master\src\PHPMailer.php';
require 'PHPMailer-master\src\SMTP.php';

$mail = new PHPMailer();
                                            
$mail->FromName = "A random name";
$mail->addAddress("myemail@gmail.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject is here";
$mail->Body = "Hello, <br>test body ";

if(!$mail->Send()) {
     echo "Mailer Error: " . $mail->ErrorInfo;
} else {
     echo "Message has been sent";
}
?>

推荐答案

这确实让我笑了起来......

This did make me laugh a bit...

不,电子邮件不能凭空出现,但电子邮件用户帐户和地址之间不一定存在直接关联.

No, emails can't just magically spring into existence, but there isn't necessarily a direct correlation between email user accounts and addresses.

当您通过调用 mail() 从共享主机帐户发送邮件时,邮件服务器知道您从哪个帐户发送邮件,因此有时不需要身份验证.例如,这就是 GoDaddy 的运作方式.不幸的是,这种方法很容易被滥用,因为通常没有什么可以阻止您对自己的身份撒谎.这就是为什么此类服务通常 a) 糟糕且 b) 对于实际传递消息极其不可靠.

When you send from a shared hosting account by calling mail(), the mail server knows the account you're sending from, and sometimes doesn't require authentication as a result. This for example is how GoDaddy operates. Unfortunately this approach is very prone to abuse because there is often little preventing you from flat-out lying about who you are. This is why such services are usually a) terrible and b) extremely unreliable for actually delivering messages.

如果您没有指定来自"地址,服务器通常会根据它确实拥有的信息组成一个 - 通常是您的用户帐户名,或运行脚本的用户名(例如 www-data),以及服务器的主机名你在,通常类似于 randomnumber.hostingprovider.example.com.查看您之前发送的邮件标题,您可能会看到类似的内容.

If you don't specify a "from" address, the server will usually make one up from information it does have – typically your user account name, or the name of the user running the script (e.g. www-data), and the hostname of the server you're on, often something like randomnumber.hostingprovider.example.com. Look at the headers of messages you've sent before, and you'll probably see something like that.

有时,对于给定共享服务器上的所有用户,此地址可能相同,因此您的递送声誉可能取决于其他人发送的内容,很可能是垃圾邮件、网络钓鱼、恶意软件等.

Sometimes this address can be the same for all users on a given shared server, so your delivery reputation can depend on what others are sending, which could well be spam, phishing, malware, etc.

这种模糊性对于可传递性来说很糟糕,所以如果您在这样的系统上托管您的联系表单,那么来自它的消息最终会进入垃圾邮件文件夹.

This vagueness is terrible for deliverability, so if you host your contact form on such a system, expect messages from it to end up in a spam folder.

如果您使用 SMTP 通过适当的"帐户,您可以获得更多的控制权和可靠性,但不幸的是,一些托管服务提供商(再次是 GoDaddy)阻止出站 SMTP 并强迫您使用他们的邮件服务器.这是一种说法,如果您想发送能够送达的电子邮件,请使用合适的托管服务提供商.

If you use SMTP to send via a "proper" account you gain a lot more control and reliability, but unfortunately some hosting providers (GoDaddy again) block outbound SMTP and force you to use their mail servers. This is a way of saying that if you want to send email that will be delivered, use a decent hosting provider.

一旦您对发送有了控制权,您就可以准确地选择发送消息的地址,这取决于身份验证限制,包括 SPF、DKIM 和 DMARC,但那是另一回事了.

Once you get control over your sending, you can choose exactly what addresses messages are sent from, subject to authentication constraints including things like SPF, DKIM, and DMARC, but that's another story.

这篇关于使用 PHPMailer 发送电子邮件而无需 SMTP 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆