使用 php 发送电子邮件

发布于 2025-01-03 17:54:36 字数 2031 浏览 0 评论 0原文

我收到以下代码,在发送之前,我检查字段是否已填充...发送电子邮件时,我收到消息“我们已收到您的电子邮件”。但我在收件箱中看不到该电子邮件,尝试了两封不同的电子邮件,但结果相同...不明白为什么你可以帮助我。这是代码:

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( "[email protected]",  $_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;
}

I got the following code and before sending i check the fields are populated or not...when sending the email i get the message 'We have received your email .' but i cannot see the email in my inbox, tried it with two different emails but same results... cannot figure out why can you help me please. here is the code:

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( "[email protected]",  $_POST['email'], " Enquiry", "test", $email_fields);
}
else
{
    echo $badinput . "</div>";
}
?>

here is the function in 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡の花香 2025-01-10 17:54:36

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

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

if (mail('[email protected]', 'subject', 'test email'))
    echo 'Mail was sent';
else
    echo 'Mail could not be sent';

另请检查垃圾邮件文件夹,因为许多通过 PHP mail() 发送的电子邮件由于标头不正确或不完整或由于滥用和不良 IP 声誉而被标记为垃圾邮件(特别是如果您正在使用共享主机)。

I see no reason for using a class if it's just probably still using the standard PHP mail() function.

Please try using this code to test if mail actually get sent:

if (mail('[email protected]', 'subject', 'test email'))
    echo 'Mail was sent';
else
    echo 'Mail could not be sent';

Also please check the Spam folder as many emails send through PHP mail() get flagged as spam due to incorrect or incomplete headers or because of abuse and bad IP reputation (especially if you're using shared hosting).

落叶缤纷 2025-01-10 17:54:36

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

我会将: 更改

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

为:

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

并检查 $emailer 类返回的内容。更有可能的是,“0”表示失败,“1”表示成功。

It doesn't seem that your actually checking the return value from the $emailer class, so the function telling you your email is sent really is just a false positive.

I would change:

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

to:

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

and check what the $emailer class is returning. more then likely it's going to be a "0" for failed or "1" for success.

埖埖迣鎅 2025-01-10 17:54:36

这是你的脚本 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";

Is that a 100% accurate representation of your script?

There appears to be a major syntax error, which if it somehow doesn't error out on you, will at least totally change the script's functionality.

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

Surely, it should be:

if(!$to || !$from || !$subject || !$message || !$fields){
    print "form function is missing a variable";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文