【问题标题】:mail php $from not working邮件 php $from 不工作
【发布时间】:2013-06-03 19:02:59
【问题描述】:

我正在尝试从我的网站向我的 hotmail 帐户发送一个简单的邮件脚本,但它一直以垃圾邮件的形式出现(cgi 邮件程序)。我知道它与标题部分有关,但似乎无法掌握如何让这一切正常工作..这就是我得到的......

<?php 
$name =  $_POST['contact_name'] ;
$email = $_POST['contact_email'] ;
$from= "$name <$email>;"
$company = $_POST['contact_company'] ;
$number = $_POST['contact_phone'] ;

// $message = $_POST['contact_message'], "Name:" . $name,"Telephone Number:" .        $number;
$message = $_POST['contact_message'];
$message .= "Name:" . $name;
$message .= "Telephone Number:" . $number;
$to = "bcplumbing-heating@hotmail.co.uk";
$subject = "ContactForm";
$headers = "From:" . $from;

//modify the mail function
mail($to,$subject,$message,$headers);
?>

任何帮助或建议都会很棒...谢谢

【问题讨论】:

  • 尝试更改为$headers = "From: " . $from . "\r\n";。这仍然不能保证它会跳过垃圾邮件。
  • 我建议使用 phpmailer (sourceforge.net/projects/phpmailer)。使用时不必考虑标题,它还有很多优势。

标签: php email hotmail


【解决方案1】:

Hotmail 会进行一些复杂的垃圾邮件检查。这会遍历 RBL 列表和反向查找。

  • 例如,当您从 test@test.de 发送电子邮件时。然后 hotmail 检查来自发送邮件服务器的 IP 是否返回域 test.de(反向查找)。

  • 下一点是邮件服务器配置正确。

对于应用程序站点来说,也许更好的方法是使用自动生成的邮件系统。

我更喜欢这里Switftmailer

【讨论】:

    【解决方案2】:

    您可能想要使用PHPMailer 类,它允许您隐藏所有抽象,甚至调试邮件发送

    【讨论】:

      【解决方案3】:

      您可以使用 SMTP 从电子邮件服务器(如 gmail)发送邮件, 不要把它放在垃圾邮件文件夹中。你可以使用这个脚本:

      电子邮件页面:

      <?php
      require "email.php";
      
      $mail = new EMail;
      $mail->Username = 'somthing@mydomain.co.uk';
      $mail->Password = 'thepassword';
      
      $mail->SetFrom("some@address.com","Some name");  // Name is optional
      $mail->AddTo("someother@address.com","Someother name"); // Name is optional
      $mail->AddTo("someother2@address.com");
      $mail->Subject = "Some subject or other";
      $mail->Message = "Some html message";
      
      //Optional stuff
      $mail->AddCc("someother3@address.com","name 3");  // Set a CC if needed, name optional
      $mail->ContentType = "text/html";          // Defaults to "text/plain; charset=iso-8859-1"
      $mail->Headers['X-SomeHeader'] = 'abcde';  // Set some extra headers if required
      $mail->ConnectTimeout = 30;  // Socket connect timeout (sec)
      $mail->ResponseTimeout = 8;  // CMD response timeout (sec)
      
      $success = $mail->Send();
      
      ?>
      

      电子邮件.php:

      <?php
      
      //email.php: Sends an email using an auth smtp connection
      //v3
      
      class EMail
      {
        const newline = "\r\n";
      
        private
          $Server, $Port, $Localhost,
          $skt;
      
        public
          $Username, $Password, $ConnectTimeout, $ResponseTimeout,
          $Headers, $ContentType, $From, $To, $Cc, $Subject, $Message,
          $Log;
      
        function __construct()
        {
          $this->Server = "127.0.0.1";
          $this->Port = 25;
          $this->Localhost = "localhost";
          $this->ConnectTimeout = 30;
          $this->ResponseTimeout = 8;
          $this->From = array();
          $this->To = array();
          $this->Cc = array();
          $this->Log = array();
          $this->Headers['MIME-Version'] = "1.0";
          $this->Headers['Content-type'] = "text/plain; charset=iso-8859-1";
        }
      
        private function GetResponse()
        {
          stream_set_timeout($this->skt, $this->ResponseTimeout);
          $response = '';
          while (($line = fgets($this->skt, 515)) != false)
          {
       $response .= trim($line) . "\n";
       if (substr($line,3,1)==' ') break;
          }
          return trim($response);
        }
      
        private function SendCMD($CMD)
        {
          fputs($this->skt, $CMD . self::newline);
      
          return $this->GetResponse();
        }
      
        private function FmtAddr(&$addr)
        {
          if ($addr[1] == "") return $addr[0]; else return "\"{$addr[1]}\" <{$addr[0]}>";
        }
      
        private function FmtAddrList(&$addrs)
        {
          $list = "";
          foreach ($addrs as $addr)
          {
            if ($list) $list .= ", ".self::newline."\t";
            $list .= $this->FmtAddr($addr);
          }
          return $list;
        }
      
        function AddTo($addr,$name = "")
        {
          $this->To[] = array($addr,$name);
        }
      
        function AddCc($addr,$name = "")
        {
          $this->Cc[] = array($addr,$name);
        }
      
        function SetFrom($addr,$name = "")
        {
          $this->From = array($addr,$name);
        }
      
        function Send()
        {
          $newLine = self::newline;
      
          //Connect to the host on the specified port
          $this->skt = fsockopen($this->Server, $this->Port, $errno, $errstr, $this->ConnectTimeout);
      
          if (empty($this->skt))
            return false;
      
          $this->Log['connection'] = $this->GetResponse();
      
          //Say Hello to SMTP
          $this->Log['helo']     = $this->SendCMD("EHLO {$this->Localhost}");
      
          //Request Auth Login
          $this->Log['auth']     = $this->SendCMD("AUTH LOGIN");
          $this->Log['username'] = $this->SendCMD(base64_encode($this->Username));
          $this->Log['password'] = $this->SendCMD(base64_encode($this->Password));
      
          //Email From
          $this->Log['mailfrom'] = $this->SendCMD("MAIL FROM:<{$this->From[0]}>");
      
          //Email To
          $i = 1;
          foreach (array_merge($this->To,$this->Cc) as $addr)
            $this->Log['rcptto'.$i++] = $this->SendCMD("RCPT TO:<{$addr[0]}>");
      
          //The Email
          $this->Log['data1'] = $this->SendCMD("DATA");
      
          //Construct Headers
          if (!empty($this->ContentType))
            $this->Headers['Content-type'] = $this->ContentType;
          $this->Headers['From'] = $this->FmtAddr($this->From);
          $this->Headers['To'] = $this->FmtAddrList($this->To);
          if (!empty($this->Cc))
            $this->Headers['Cc'] = $this->FmtAddrList($this->Cc);
          $this->Headers['Subject'] = $this->Subject;
          $this->Headers['Date'] = date('r');
      
          $headers = '';
          foreach ($this->Headers as $key => $val)
            $headers .= $key . ': ' . $val . self::newline;
      
          $this->Log['data2'] = $this->SendCMD("{$headers}{$newLine}{$this->Message}{$newLine}.");
      
          // Say Bye to SMTP
          $this->Log['quit']  = $this->SendCMD("QUIT");
      
          fclose($this->skt);
      
          return substr($this->Log['data2'],0,3) == "250";
        }
      }
      ?>
      

      【讨论】:

        【解决方案4】:

        您的邮件直接进入垃圾邮件箱的原因可能是因为您的headers 非常少。

        尝试像这样扩展它们:

        $headers = 'MIME-Version: 1.0' . PHP_EOL;
        $headers .= "FROM: DezeActie.nl <noreply@website.com>" . PHP_EOL;    
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
        $headers .= "Return-Path: noreply@website.com" . PHP_EOL;
        

        要记住的另一件事是包含FROM: 部分的header 必须高于Content-type。显然,这可以防止它进入垃圾邮件箱(至少,我在 SO 上阅读并亲自尝试过 - 有效)。

        您可以通过添加进一步扩展headers

        $headers .= "Reply-To: Recipient Name &lt;email@website.com&gt;" . PHP_EOL;

        根据我的阅读,这几乎可以防止您的邮件进入垃圾邮件箱,因为您提供了很多关于您的邮件(格式)的信息。

        编辑:我看到有人发布有关 PHPMailer 和 SwiftMailer 的帖子。我个人没有使用过它们,但我很确定它们在格式化邮件功能和安全性方面会给您带来很多好处。

        【讨论】:

          猜你喜欢
          • 2012-01-03
          • 2017-10-27
          • 2013-10-05
          • 2011-09-14
          • 1970-01-01
          • 1970-01-01
          • 2016-03-04
          • 2015-08-14
          • 1970-01-01
          相关资源
          最近更新 更多