首页 > 解决方案 > 如何向 SMTP 脚本添加身份验证?

问题描述

我正在使用这个脚本来计算主机服务器上使用的总存储空间并发送包含详细信息的电子邮件。到目前为止,它总是与不同的服务器一起工作,但新的服务器似乎需要身份验证才能发送电子邮件。

<?php

$filePath = "./";

$total = 0;
$d = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator($filePath), 
  RecursiveIteratorIterator::SELF_FIRST
);

foreach($d as $file){
  $total += $file->getSize();
}

    $to      = 'test@gmail.com';
    $subject = "Storage " .number_format($total/1048576, 2);
    $message = "Storage " .number_format($total/1048576, 2);
    $headers = "MIME-Version: 1.0\n";
    $headers .="Content-Type: text/html; charset=\"ISO-8859-1\"\n";
    $headers .= 'From: XYZHost <test@domain.com>' . "\r\n" .
                'Reply-To: test@domain.com' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();

    ini_set("SMTP", "mail.domain.com");
    mail($to, $subject, $message, $headers);
?>

交付失败并出现以下错误:

Diagnostic-Code: smtp; 550-Verification failed for <account@xyz.hosting>
 550-Called:   ---
 550-Sent:     RCPT TO:<account@xyz.hosting>
 550-Response: 550 5.1.1 User unknown: account@xyz.hosting
 550 Invalid sender <account@xyz.hosting>

有没有办法在这里添加身份验证?

标签: phpsmtp

解决方案


欢迎来到 Stack Overflow,dsl225。

PHP mail() 通常不允许您使用您选择的 SMTP 服务器,并且它根本不支持 SMTP 身份验证(当今许多邮件服务器都需要)。

我会推荐著名的 PHP 3rd 方包;PHPMailer满足您的需求。


推荐阅读