简体   繁体   English

使用 PHPMailer 发送电子邮件而无需 SMTP 身份验证

[英]Send email using PHPMailer without SMTP authentication

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.我不经常使用 PHP,但是当我使用 PHP 并且需要编写一个函数来发送电子邮件时,我只使用了 mail() 函数。 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.它甚至没有电子邮件地址。

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.这就是我此时想要实现的目标 - 向我发送电子邮件,而无需真正连接到 SMTP 服务器或通过身份验证。 How can be that done with PHPMailer?如何使用 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.不,电子邮件不能只是神奇地出现,但电子邮件用户帐户和地址之间不一定存在直接关联。

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.当您通过调用mail()从共享主机帐户mail() ,邮件服务器知道您从哪个帐户发送邮件,因此有时不需要身份验证。 This for example is how GoDaddy operates.例如,这就是 GoDaddy 的运作方式。 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.这就是为什么此类服务通常 a) 糟糕且 b) 对于实际传递消息极其不可靠。

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 (eg www-data ), and the hostname of the server you're on, often something like randomnumber.hostingprovider.example.com .如果您没有指定“发件人”地址,服务器通常会根据它所拥有的信息进行补充——通常是您的用户帐户名,或运行脚本的用户名(例如www-data ),以及主机名您所在的服务器,通常类似于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.这种模糊性对于可交付性来说很糟糕,因此如果您在这样的系统上托管您的联系表单,那么期望来自它的消息最终会出现在垃圾邮件文件夹中。

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.如果您使用 SMTP 通过“正确”帐户发送邮件,您将获得更多控制权和可靠性,但不幸的是,一些托管服务提供商(再次是 GoDaddy)会阻止出站 SMTP 并强迫您使用他们的邮件服务器。 This is a way of saying that if you want to send email that will be delivered, use a decent hosting provider.这是一种说法,如果您想发送将要送达的电子邮件,请使用合适的托管服务提供商。

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.一旦您控制了您的发送,您就可以准确地选择发送消息的地址,这受身份验证限制(包括 SPF、DKIM 和 DMARC 等)的约束,但那是另一回事了。

To my knowledge it is possible and have it work correctly each time.据我所知,这是可能的,并且每次都能正常工作。 In the last year I found that the e-mails I sent using the mail() function would immediately go into my spam box (as well as the junk box of others) causing great confusion.去年我发现我使用 mail() 函数发送的电子邮件会立即进入我的垃圾邮件箱(以及其他人的垃圾箱),造成很大的混乱。 I upgraded to PHPMailer to solve this and I have never set it up to use SMTP and works just fine.我升级到 PHPMailer 来解决这个问题,但我从未将它设置为使用 SMTP 并且工作得很好。

The code I have is:我的代码是:

$mail = new PHPMailer(true); 
$html = $email->get(); // This gets a standard HTML Template to use as the base of the e-mail.
$title = 'Title for Inside the HTML';
$subject = 'The E-mail Subject';
$html = str_replace('[title]',$title,$html); //Use this to replace the [title] element in my HTML Template with $title.
$html = str_replace('[date]',date("F j, Y g:i A"),$html); // Again, replaces [date] in the HTML Template with the current date and time.
$body = 'Hello My Good Friend,<br>This is just a simple <strong>HTML Message</strong><br><br>Thank you.';
$html = str_replace('[body]',$body,$html); //Add my HTML Message to the HTML Template.

try {
    //Recipients
    $mail->setFrom('noreply@example.com', 'My Organization');
    $mail->addAddress($row["email"], $row["fullname"]);     // Add a recipient
    
    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $html;
    $mail->AltBody = strip_tags($html,'<br>');
            
    $mail->send();
                
    //Return Success Message
    $rMsg .= '<div class="alert alert-success">
                <strong>Success: </strong> We have e-mailed the warning.</div>';
} catch (Exception $e) {
        $rMsg .= '<div class="alert alert-danger">
                    <strong>Error: </strong> There was an error e-mailing the warning.</div>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM