邮件php $从不工作

时间:2013-06-07 12:51:58

标签: php email hotmail

我正在尝试将一个简单的邮件脚本从我的网站发送到我的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);
?>

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

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

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

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

我更喜欢Switftmailer

答案 2 :(得分:0)

您可以使用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 = "\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";
  }
}
?>

答案 3 :(得分:0)

您的邮件直接进入spambox的原因可能是因为您的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;

要记住的另一件事是,包含header部分的FROM:必须高于 Content-type。显然,这使它无法进入spambox(至少,我在SO上阅读它并自己尝试了 - 工作)。

您可以通过添加

进一步扩展headers

$headers .= "Reply-To: Recipient Name <email@website.com>" . PHP_EOL;

根据我的阅读,这几乎可以防止您的邮件进入spambox,因为您提供了大量有关邮件(格式)的信息。

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