邮件PHP $不工作



我正试图从我的网站发送一个简单的邮件脚本到我的hotmail帐户,但它不断出现作为垃圾邮件(cgi mailer)。我知道这与标题有关,从部分,但似乎无法掌握如何让它全部工作…这就是我得到的……

<?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);
?>
任何帮助或建议将是伟大的…由于

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

Hotmail做一些复杂的垃圾邮件检查。下面介绍RBL列表和反向查找。

  • 所以当你从test@test.de发送电子邮件时。然后hotmail检查发送邮件服务器的IP是否返回到域test.de(反向查找)。

  • 下一点是Email服务器配置正确

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

您可以使用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();
?>

email.php:

<?php
//email.php: Sends an email using an auth smtp connection
//v3
class EMail
{
  const newline = "rn";
  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";
  }
}
?>

您的邮件直接进入垃圾信箱的原因可能是因为您的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 <email@website.com>" . PHP_EOL;

据我所知,这基本上可以防止你的邮件进入垃圾信箱,因为你提供了很多关于你的邮件的信息(格式)。

编辑:我看到人们张贴关于PHPMailer和SwiftMailer。我个人没有使用过它们,但是我很确定它们在格式化邮件功能和安全性方面给你带来了很多好处。

相关内容

  • 没有找到相关文章

最新更新