为什么邮件失败在php? [英] Why mail fails in php?

查看:166
本文介绍了为什么邮件失败在php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

<?php
//define the receiver of the email
$to = 'dannyfeher69@gmail.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. 
$message = "Hello World!\n\nThis is my mail.";
//define the headers we want passed. 
$header = "From: me@localhost.com";
//send the email
$mail_sent = @mail( $to, $subject, $message);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 

echo $mail_sent ? "Mail sent" : "Mail failed";
?>

- 它返回邮件失败

-- it returns mail failed

请帮助我

推荐答案

有几个原因可能会失败。查找原因的主要障碍是在调用mail()函数前使用错误控制操作符(@)。

There are several reasons this could fail. The main obstacle to finding the cause is the use of the error control operator (@) in front of the call to the mail() function.

其他可能的原因是缺少一个有效的From头。虽然您在$头变量中定义一个,但您不能将其传递给mail()函数。 发件人标题也是您发送电子邮件地址的有效电子邮件地址也很重要。如果不是,大多数托管公司现在将拒绝该邮件作为垃圾邮件。您可能还需要为mail()提供第五个参数,通常包含一个由-f组成的字符串,后跟当前域的有效电子邮件地址。

Other possible reasons are the lack of a valid From header. Although you define one in the $header variable, you don't pass it to the mail() function. It's also important that the From header is a valid email address on the domain you're sending the email from. If it isn't, most hosting companies will now reject the email as spam. You might also need to supply a fifth parameter to mail(), which normally consists of a string comprised of -f followed by a valid email address on the current domain.

另一种可能性是您正在尝试从您自己的计算机发送。 mail()函数不支持SMTP身份验证,所以大多数邮件服务器将拒绝来自他们无法识别的邮件。

Yet another possibility is that you are trying to send this from your own computer. The mail() function doesn't support SMTP authentication, so most mail servers will reject mail from sources they don't recognize.

只是为了添加所有的问题电子邮件中的换行符必须是回车跟随换行符的组合。在PHP中,这是\\\\,而不是\\\
\\\

And just to add to all your problems, newlines in emails must be a combination of carriage return followed by newline. In PHP, this is "\r\n", not "\n\n".

假设您正在使用远程服务器发送邮件,代码应该如下所示:

Assuming you're using a remote server to send the mail, the code should look something like this:

<?php
//define the receiver of the email
$to = 'dannyfeher69@gmail.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. 
$message = "Hello World!\r\nThis is my mail.";
//define the headers we want passed. 
$header = "From: me@localhost.com"; // must be a genuine address
//send the email
$mail_sent = mail($to, $subject, $message, $header);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 

echo $mail_sent ? "Mail sent" : "Mail failed";
?>

这篇关于为什么邮件失败在php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆