PHP邮件功能,这里有什么问题?

时间:2010-03-04 17:58:07

标签: php email

以下代码向我发送一封包含以下变量的电子邮件。最后一个if语句之一说if(mail)然后回显“你会很快联系到你”。当脚本运行时,我会回复“很快就会联系您”,但是,我从未收到过电子邮件。

我确实有一个较小的联系人脚本(在第一个和更大的一个之后发布)确实有效。

注意:contants.php和functions.php都包含在内并正常工作       WEBMASTER_EMAIL在contanstants.php中定义并且是正确的,因为       我较小的联系人脚本使用相同的变量,并通过电子邮件发送给我。

感谢您的帮助

 <?php

// pull constant variables
include("php/constants.php");

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post) {
    include ("php/functions.php");
}

// general info
$name = stripslashes($_POST['contact']);
$phone = $_POST['phone'];
$email = trim($_POST['email']);
$time_to_reach = $_POST['time-to-reach']; // what the best time to reach them?

// delivery info
$delivery_address = $_POST['del-address'];
$delivery_city = $_POST['del-city'];
$delivery_state = $_POST['del-state'];
$delivery_zip = $_POST['del-zip'];

// moving city info if applicable
$moving_address = $_POST['move-address'];
$moving_city = $_POST['move-city'];
$moving_state = $_POST['move-state'];
$moving_zip = $_POST['move-zip'];

// date needed
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];

// how long do you need the storage?
$storage_length = $_POST['time-length'];

// how many containers do you need?
$quantity_containers =  $_POST['number-of-containers'];

// how did you hear about us?
$tracker =  $_POST['tracker'];

// message
$message_holder = htmlspecialchars($_POST['message']);

$error = '';

// check general info
if(!$name) { $error .= 'Please enter your name.<br />'; }
if(!$email) { $error .= 'Please enter an e-mail address.<br />'; }
if($email && !ValidateEmail($email)) { $error .= 'Please enter a valid e-mail address.<br />'; }
if(!$time_to_reach) { $error .= 'Please select the best time to reach you.<br />'; }

// check delivery info
if(!$delivery_address) { $error .= 'Please enter you current address.<br />'; }
if(!$delivery_city) { $error .= 'Please enter your current city.<br />'; }
if(!$delivery_state) { $error .= 'Please enter your current state.<br />'; }
if(!$delivery_zip) { $error .= 'Please enter your current zip code.<br />'; }

// check date needed
if(!$month) { $error .= 'Please enter the approximate date you need the storage.<br />'; }
if(!$day) { $error .= 'Please enter the approximate date you need the storage.<br />'; }
if(!$year) { $error .= 'Please enter the approximate date you need the storage.<br />'; }

// check length of time needed
if(!$storage_length) { $error .= 'Approximatly how long will you need the storage unit for?<br />'; }

// check quantity of storages
if(!$quantity_containers) { $error .= 'How many containers will you need?<br />'; }

// check advertising tracker
if(!$tracker) { $error .= 'Please let us know how you\'ve heard of us.<br />'; }

// check message (length)
if(!$message_holder || strlen($message_holder) < 10) {
    $error .= "Please enter your message. It should have at least 10 characters.<br />";
}

// build email message
$message = "Name: {$name}
Phone: {$phone}
Email: {$email}
Best time to reach: {$time_to_reach}\n
-----------------------------------------------------
Delivery address: {$delivery_address}
              {$delivery_city}, {$delivery_state} {$delivery_zip}

Moving address: {$moving_address}
              {$moving_city}, {$moving_state} {$moving_zip}
-----------------------------------------------------
Date needed: {$month}/{$day}/{$year}
Length of time needed: {$storage_length}
Number of containers: {$quantity_containers}
Where did you hear about us? 
{$tracker}\n
Message: {$message_holder}\n";

if(!$error) {
    $mail = mail(WEBMASTER_EMAIL, $subject, $message,
         "From: residential-quote@stocor.com\r\n"
        ."Reply-To: ".$name."<".$email.">\r\n"
        ."X-Mailer: PHP/" . phpversion());

    if($mail) {
        echo '<p>Thank you, you will be contacted soon.</p>';
    }   
} else {
    echo '<div class="notification_error">'.$error.'</div>';
}

?>

以下脚本,联系脚本确实有效,这意味着我收到了一封电子邮件。

<?php

// pull constant variables
include("php/constants.php");

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post) {
    include ("php/functions.php");
}

// variables
$name = stripslashes($_POST['name']);
$phone = $_POST['phone'];
$email = trim($_POST['email']);
$tracker = $_POST['tracker'];
$message_holder = htmlspecialchars($_POST['message']);

$error = '';

// check name
if(!$name) {
    $error .= 'Please enter your name.<br />';
}

// check email
if(!$email) {
    $error .= 'Please enter an e-mail address.<br />';
}

// validate email
if($email && !ValidateEmail($email)) {
    $error .= 'Please enter a valid e-mail address.<br />';
}

// check advertising tracker
if(!$tracker) {
    $error .= 'Please let us know how you\'ve heard of us.';
}

// check message (length)
if(!$message_holder || strlen($message_holder) < 10) {
    $error .= "Please enter your message. It should have at least 10 characters.<br />";
}

// build email message
$message = "Name: {$name} \n
Phone: {$phone} \n
Email: {$email} \n
Where did you hear about us? 
{$tracker}\n\n
Message: {$message_holder}\n";

if(!$error) {
    $mail = mail(WEBMASTER_EMAIL, $subject, $message,
         "From: contact@stocor.com\r\n"
        ."Reply-To: ".$name."<".$email.">\r\n"
        ."X-Mailer: PHP/" . phpversion());

    if($mail) {
        //header("Location: thank_you.php");
        echo "Thank you. You will be contacted soon.";
    }   
} else {
    echo '<div class="notification_error">'.$error.'</div>';
}

?>

5 个答案:

答案 0 :(得分:2)

使用裸邮件功能只是要求麻烦(http://en.wikipedia.org/wiki/E-mail_injection,php特定信息:http://www.damonkohler.com/2008/12/email-injection.html),并阻止简单的调试。我建议你在邮件功能周围使用一个对象包装器,因为这有利于过滤头文件,使其成为php邮件表单标头注入垃圾邮件发送者的非标准目标,并允许你更容易调试邮件转储创建的邮件对象并查看其内容。为了进行调试,它还允许您在没有/不想拥有邮件服务器但又不想尝试使用邮件服务器的机器上提供“只是回显邮件”的替代方案进行本地测试。在您测试功能时发送邮件。

这是我创建并使用自己的包装器(可自由修改和使用): http://github.com/tchalvak/ninjawars/blob/master/deploy/lib/obj/Nmail.class.php

或者只是查看PEAR邮件:http://pear.php.net/package/Mail/

答案 1 :(得分:0)

它不会向我跳出来,为什么不尝试将错误一直向上,看看是否有效。

error_reporting(1);

在脚本的顶部。

答案 2 :(得分:0)

编辑:抱歉,我现在看到您确实已启用错误报告。确保您的INI文件也正确设置。尝试删除^ E_NOTICE,以便您也可以看到这些警告。

我遇到过mail()根本不会说什么(并且它会像成功一样执行)的问题。如果您倾向于使用mail(),则可以使用SwiftMailer,它通常会在出现问题时抛出有用的异常,并包含使用Swift_MailTransport的运输类mail()但是一个很好的面向对象的接口。

答案 3 :(得分:0)

因此,由于问题的性质(邮件被接受传递 - $ mail是真的),问题可能出现在邮件内容中。您是否可以访问邮件服务器本身?你能检查一下日志吗? var_dump()$ subject,$ message,并将标题设置为var和var_dump()。用细齿梳检查内容物。删除可疑字符和换行符,直到它确实有效。

有一件事要尝试......(但是,你的其他邮件被接受的事实说这可能不是这种情况)

http://www.php.net/manual/en/function.mail.php

  

如果未收到消息,请尝试   仅使用LF(\ n)。有些穷人   优质的Unix邮件传输代理   由CRLF自动替换LF   (如果CRLF是,则导致CR加倍   用过的)。这应该是最后的手段,   因为它不符合»RFC 2822。

mail()的问题在于它只是将邮件发送到本地sendmail守护程序。它没有为您提供有关邮件的任何有效反馈,并且中继标题有时会使您的垃圾邮件降级。

我会查看http://sourceforge.net/projects/phpmailer/

答案 4 :(得分:0)

尝试将消息中的行包装为70个字符 $ message = wordwrap($ message,70); 尝试使用\ n替换其他标题中的\ r \ n,以防邮件功能替换\ n \ r \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ r \ n \ r \ n