简体   繁体   English

无法使用php中的mail()函数发送邮件

[英]Unable to send mail using mail() function in php

I am trying to get contact form details through mail. 我正在尝试通过邮件获取联系表格的详细信息。 But failed to do so using mail() function. 但是无法使用mail()函数这样做。 here's my code. 这是我的代码。

if(isset($_POST['rqsubmit'])) {
    $name = htmlspecialchars($_POST['Field1']);
    $email = trim($_POST['Field2']);
    $phone = trim($_POST['Field3']);
    $msg = strip_tags($_POST['Field4']);
    //echo $name." ".$phone." ".$email." ".$msg;
    $to = 'gowtham@gmail.com';
    //$from = $email;
    $subject = "Software Development";
    $message = "Name:".$name."<br/>Phone".$phone."<br/>Message:".$msg;
    //echo $message;
    $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";   
//echo $message;    
$headers = "From: xyz@gmail.com". "\r\n".
            'Reply-To: xyz@gmail.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();    
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";       
    $ok = @mail($to, $subject, wordwrap($message, 70, "\r\n"), $headers);
    if ($ok) {
        echo "<p>Thank you for contacting us!  !!</p>";
    } else {
        echo "<p>Mail could not be sent. Sorry!</p>";
    } 
} else { 
    echo "mail not sent";  
}

I would like to know where i am going wrong. 我想知道我要去哪里错了。 Any help would b highly appreciated. 任何帮助将不胜感激。 thank you! 谢谢!

Add: 'X-Mailer: PHP/' . 添加:“ X-Mailer:PHP /”。 phpversion(); phpversion(); to: $headers 至:$ headers

Perhaps it's being rejected due to the lack of a sender email address. 可能由于缺少发件人电子邮件地址而被拒绝。

Put your own email address in the From: header, and add a Reply-To: header containing the sender's address. 将您自己的电子邮件地址放在From:标头中,并添加一个包含发件人地址的Reply-To:标头。

Can I just point out that you need to check the form data thoroughly before putting it into an email. 我能否指出您需要在将表格数据放入电子邮件之前彻底检查表格数据。 For example, I could quite easily send emails all over the place by setting Field2 to something like blah@example.com\\nBcc: spam1@spam.com, spam2@spam.com, ... . 例如,通过将Field2设置为blah@example.com\\nBcc: spam1@spam.com, spam2@spam.com, ...类的东西,我可以很容易地在各地发送电子邮件blah@example.com\\nBcc: spam1@spam.com, spam2@spam.com, ...

EDIT: Sorry, my mistake — you did add a From header. 编辑:对不起,我的错-您确实添加了From头。 However, it's quite likely that your mail server is configured to reject emails where the sender address is not on its list of approved senders. 但是,很可能您的邮件服务器配置为拒绝发件人地址不在其已批准发件人列表中的电子邮件。 That's why you need to put your own address in the From: header, and the sender's address in the Reply-To: header. 这就是为什么您需要在From:标头中放置自己的地址,而在Reply-To:标头中放置发件人的地址。

EDIT 2: Also, what on earth are you doing with $mime_boundary ? 编辑2:另外,您到底在用$mime_boundary做什么? As far as I can tell, you're not sending a valid MIME message. 据我所知,您没有发送有效的MIME消息。

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

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