PHP邮件功能


PHP mailing function

在下面的代码中,我正在尝试使用PHP邮件功能,代理在我的网站注册后将在其帐户中收到邮件。但是我使用的邮件功能代码不起作用。我的网站是实时的,我正在实时服务器中进行测试。任何人都可以看看并引导我错在哪里。

当我点击注册按钮时填写所有详细信息后,它总是执行其他部分

<?php
if(isset($_POST['reg_agent']))
{
$agent_name = $_POST['agent_name'];
$agent_name = mysql_real_escape_string($agent_name);
$agent_email = $_POST['agent_email'];
$agent_email = mysql_real_escape_string($agent_email);
$agent_password = $_POST['agent_password'];
$agent_password = mysql_real_escape_string($agent_password);
$subject = "Activate Your Account";
$message = "Dear ".$agent_name.", 'n
Thank you for registering at our website, http://www.*****.com!
You are two steps away from logging in and interacting with our site.
To activate your membership, please click here: http://www.*****.com/agent.php?agent_email=$agent_email
Once you activate your membership, you will be able to login with the following information:
Email-Id: ".$agent_email."
Password: ".$agent_password."
Thanks!
******
This is an automated response, please do not reply!";
$header = "vikrampatnaik.dev@gmail.com";
$retval = mail ($agent_email,$subject,$message,$header);
if($retval == true)
{
 mkdir("agents/".$agent_name);
   mysql_query("INSERT INTO `agent_job_status`(agent_name, agent_email, agent_mobile, agent_password, agent_city, agent_location)
                    VALUES ('$agent_name', '$agent_email', '$agent_mobile', '$agent_password', '$agent_state3', '$agent_location')");
    echo '<div class="alert alert-success signin"  style="text-align: center;"><button type="button" class="close" data-dismiss="alert">&times;</button>';
    echo 'Congrats! <strong>"'.$agent_name. '"</strong> you have successfully created your account &nbsp;&nbsp; 
    Your membership information has been mailed to your email address! Please check your email and follow the instructions!' ;
    echo '</div>';
}
else{
    echo '<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">&times;</button> ';
    echo mysql_error() ;
    echo '</div>';
    echo '<div class="alert alert-success signin" style="text-align: center;">
            <button type="button" class="close" data-dismiss="alert">&times;</button>';
    echo "Message could not be sent...";
    echo "</div>";
}
}
?>

1) 您在$header中发送的其他标头无效。 假设您的意思是 vikrampatnaik.dev@gmail.com 为"发件人"地址,则标头变量应为:

$header = 'From: vikrampatnaik.dev@gmail.com';

2)除非在这种情况下,您使用的是Gmail的SMTP,否则您的邮件很有可能被阻止为垃圾邮件。 这是因为您的"发件人"地址与真正发送邮件的服务器不匹配。 为了提高邮件实际送达的机会,您应该有一个与您的域匹配的发件人地址,并将您的 Gmail 地址作为回复,如下所示:

$header = 'From: no-reply@***yourdomain***.com' . "'r'n" .
'Reply-To: vikrampatnaik.dev@gmail.com' . "'r'n" . 

参考资料:http://php.net/manual/en/function.mail.php

我遇到了同样的问题,我不得不安装"sendmail"来解决它。在 Linux 上,它是这样安装的:

sudo apt-get install sendmail