Postfix SMTP 身份验证

Postfix SMTP 身份验证

我已经安装了带有 dovecot sasl 和 TLS 的 postfix 服务器。当我尝试从 PHP 代码发送邮件时出现问题,如果我使用登录身份验证类型作为“smtp”,服务器会接受没有任何凭据的连接。如果我将其更改为“登录”,服务器会检查我的凭据,并在用户或密码错误时发出警报。

什么是 SMTP 身份验证以及如何将 postfix 配置为仅接受身份验证用户?

我的main.cf文件中的相关代码

mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_local_domain =
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtp_tls_security_level = may
smtpd_tls_security_level = may
smtp_tls_note_starttls_offer = yes
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
virtual_alias_domains = $mydomain
virtual_alias_maps = hash:/etc/postfix/virtual

zend mail PHP 代码的一部分,即使密码错误或什么都没有也可以发送邮件。

    $options   = new SmtpOptions([
    'host' => 'mail.host.com',
    'port' => '25',
    'connection_class'  => 'smtp',
    'connection_config' => [
        'username' => 'user',
        'password' => 'bad-password',
        'ssl' => 'tls'
    ]
]);

答案1

以下电子邮件脚本适用于使用 SMTP 身份验证的 PHP 电子邮件发送。您需要使用端口 465 或 587 来发送带有 SMTP 身份验证的电子邮件。

SMTP 默认端口是端口 25。如果您使用端口 25 意味着它将从服务器发送电子邮件而无需 SMTP 身份验证。

从这里下载 phpmailerhttps://github.com/PHPMailer/PHPMailer然后使用下面的编码。

<?php

require_once "vendor/autoload.php";

$mail = new PHPMailer;

//Enable SMTP debugging. 
$mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "[email protected]";                 
$mail->Password = "super_secret_password";                           
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 587;                                   

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recepient Name");

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

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

相关内容