简体   繁体   English

我在PHP邮件中遇到标题问题

[英]I am having problems with Headers in PHP mail

I am having a problem getting the header to work in the mail PHP. 我在使标题在邮件PHP中工作时遇到问题。 It is actually a contact form and the "From" parameter is essential. 它实际上是一个联系表格,“发件人”参数是必不可少的。 Here is part of the code: 这是代码的一部分:

$headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "From: " . $name . " <".$from.">\r\n";



        // If there are no errors, send the email
        if (!$errName && !$errEmail && !$errMessage && !$errVerify ) {
        $success = mail($to, $subject, $body, $headers);

            // redirect to success page 
            if ($success){
              $result='<div style="margin-top:20px" class="alert alert-success">Thank you for contacting us. We will get back to you as soon as possible.</div>';
            }
            else{
              $result='<div style="margin-top:20px" class="alert alert-success">There was a problem.</div>';
            }
        }

However, this will only print out the result as unsuccessful. 但是,这只会打印出不成功的结果。 But when I remove the header from mail(), the email gets sent successfully. 但是,当我从mail()删除标题时,电子邮件被成功发送。

I think you don't need the MIME-Version. 我认为您不需要MIME版本。 Try with something like this: 尝试这样的事情:

    $headers = "Content-Type: text/html; charset=utf-8\n";        
    $headers .= "From: ".$name."\n";

Anyway sending mails is more complex than it seems, as for example some servers only accept \\n instead of \\r\\n. 无论如何,发送邮件比看起来要复杂得多,例如,某些服务器仅接受\\ n而不是\\ r \\ n。 It is usualy much better to use robust libraries like phpmailer or swiftmail. 通常,使用健壮的库(例如phpmailer或swiftmail)要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM