用php发送电子邮件

时间:2012-02-09 09:15:32

标签: php email send

我收到了以下代码,在发送之前检查字段是否已填充...发送电子邮件时,我收到消息'我们已收到您的电子邮件。'但我看不到收件箱中的电子邮件,尝试了两封不同的电子邮件但结果相同......无法弄清楚为什么你能帮我。这是代码:

if($badinput == NULL){ ?>
    <h2>We have received your email .</h2>
    </div>
    <?php

    require_once("libs/inc.email_form.php");

    $email_fields = array(
        "Name"              => $_POST['name'],
        "E-Mail Address"        => $_POST['email'],
        "Telephone Number"      => $_POST['telephone'],
        "Callback"          => $_POST['callback'],
        "Enquiry"           => $_POST['enquiry']
    );

    contact_form( "myemail@yahoo.co.uk",  $_POST['email'], " Enquiry", "test", $email_fields);
}
else
{
    echo $badinput . "</div>";
}
?>

这是libs / inc.email_form.php中的函数:

function contact_form($to, $from, $subject, $message, $fields){ 
    if(!$to || !$from || !$subject || !$message || !$fields){
        print form function is missing a variable";
        return false;
    }

    $msg_body = $message."\n\nSubmitted ".date("l, F j, Y, g:i a")." [EST]\n\nSUBMISSION DETAILS:\n";

    // clean up all the variables
    foreach($fields as $k => $v){
        $msg_body .= "\n".$k.": ".clean_var($v);
    }

    // add additional info
    $referer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "could not determine" ;
    $user_agent = (isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : "could not determine" ;
    $msg_body .= "\n\nAdditional Info:\nIP = ".$_SERVER['REMOTE_ADDR']."Browser Info: ".$user_agent."Referral: ".$referer." \r";

    // send it
    $emailer = new emailer;
    if(is_array($to)){
        foreach($to as $t){
            $emailer->send_email($from, $subject, $msg_body, $to);
        }
    }else{
        $emailer->send_email($from, $subject, $msg_body, $to);
    }

    return true;
}

3 个答案:

答案 0 :(得分:2)

如果可能仍然使用标准的PHP mail()函数,我认为没有理由使用类。

请尝试使用此代码测试邮件是否实际发送:

if (mail('you@domain.ext', 'subject', 'test email'))
    echo 'Mail was sent';
else
    echo 'Mail could not be sent';

另外请检查垃圾邮件文件夹,因为许多电子邮件通过PHP邮件发送()由于标题不正确或不完整或因滥用和恶意IP信誉而被标记为垃圾邮件(特别是如果您使用共享托管)。

答案 1 :(得分:1)

看起来您实际上没有检查来自$ emailer类的返回值,因此告诉您发送电子邮件的函数确实只是误报。

我会改变:

$emailer->send_email($from, $subject, $msg_body, $to);

为:

$result = $emailer->send_email($from, $subject, $msg_body, $to);
print_r($result);

并检查$ emailer类返回的内容。更有可能它会成为&#34; 0&#34;失败或&#34; 1&#34;为了成功。

答案 2 :(得分:0)

这是您脚本的100%准确表示吗?

似乎存在一个主要的语法错误,如果它以某种方式不会对您造成错误,至少会完全改变脚本的功能。

if(!$to || !$from || !$subject || !$message || !$fields){
    print form function is missing a variable";

当然应该是:

if(!$to || !$from || !$subject || !$message || !$fields){
    print "form function is missing a variable";