PHP邮件功能

时间:2015-03-13 04:53:41

标签: php phpmailer

在下面的代码中,我尝试使用 PHP邮件功能,其中在我的网站注册后的代理将在他/她的帐户中收到邮件。但我使用的邮件功能代码并不起作用。我的网站是直播的,我在实时服务器上测试。任何人都可以看看并指导我错误的地方。

点击注册按钮后填写所有细节后,它总是执行else部分

<?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>";
}

}
?>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我有同样的问题,我必须安装&#34; sendmail&#34;解决它。 在linux上以这种方式安装:

sudo apt-get install sendmail