【问题标题】:PHP mail configuration acceptable by all mail servers所有邮件服务器都可接受 PHP 邮件配置
【发布时间】:2014-03-03 07:21:07
【问题描述】:

我在注册后立即使用电子邮件检查。我使用 ubuntu 13.10 服务器(具有 Postfix)的本地 sendmail。问题是,我无法使用所有邮件服务器对其进行测试。而且我知道有些服务器不接收来自我的服务器的电子邮件。 事实上像gmail这样的服务器,hotmail从服务器获取电子邮件。但是像 mail.ru 这样的服务器(我测试过,垃圾邮件和收件箱都不会收到电子邮件)不会从我的服务器收到电子邮件。这是我与 PHPMailer 一起使用的邮件类。

<?php

class Mail
{
    private $mail;

    public function __construct()
    {
        $this->mail = new PHPMailer(true);

        $this->mail->IsSendmail();
        $this->mail->WordWrap = 50;
        $this->mail->CharSet = "UTF-8";
        $this->mail->IsHTML(true);

    }

    public function SendIt($to, $from_mail, $from_name, $subject, $message)
    {
        $this->mail->AddReplyTo($from_mail, $from_name);
        $this->mail->SetFrom($from_mail, $from_name);
        $this->mail->Subject = $subject;
        $this->mail->Body = $message;
        $this->mail->AddAddress($to);
        if ($this->mail->Send())
            return true;
        else {
            Yii::log('Mail error: ' . $this->mail->ErrorInfo, "error", "mail");
            return false;
        }

    }

}

我做错了什么?有什么建议么?也许我必须使用一些标题?

【问题讨论】:

  • 在 PHPMailer 中设置 ssl true 并确保您的 php.ini 也启用了 ssl 套接字。然后测试它是否有效。
  • @MT-Developer 为什么选择 ssl?
  • 检查一下。这可能是原因。

标签: php email


【解决方案1】:

我建议你简单地使用 PHPMAILER 类,这样你就可以休息一下了。

https://code.google.com/a/apache-extras.org/p/phpmailer/

这将很有帮助,因此您无需开始为编辑大量代码而烦恼,因为这会浪费您的时间用于其他工作。

检查示例文件夹中的文件以获取 SMTP 示例,并使用 SMTP 高级或基本。使用 SMTP 高级,您将能够抛出和捕获错误。这可以帮助您知道哪里有错误,直到所有错误都得到解决。请参阅 SMTP 基本代码的示例代码。

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if      not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = preg_replace('/[\]/','',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                       // 1 = errors and messages
                                       // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";   // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

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

当您完成测试并希望现在停止输出 SMTP 缓冲区消息时,请找到显示的这一行

$mail->SMTPDebug = 2;
and replace it with

$mail->SMTPDebug = false;
$mail->do_debug = 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多