PHP Mailer不发送SMTP邮件

时间:2017-02-22 07:36:42

标签: php ajax smtp phpmailer

我无法通过Gmail smtp设置发送邮件...另一方面,在没有任何身份验证的情况下,我从 sg2nlhg096.shr.prod.sin2.secureserver.net 服务器名称收到邮件。

SMTP设置代码。

require_once "class.phpmailer.php";
require_once('class.smtp.php');

$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'abc@gmail.com';
$mail->Password = '*************';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('johndoe@gmail.com', 'John');
$mail->addReplyTo('replyto@example.com', 'Doe');
$mail->addAddress('mr.shah118@gmail.com', 'John Doe');

$mail->Subject = 'PHPMailer GMail SMTP test';

$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

$mail->AltBody = 'This is a plain-text message body';

if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
echo 0;
} else {
echo "Message sent!";
echo 1;
}

简单邮件设置......

require("class.phpmailer.php");
$mail = new PHPMailer();  // create a new object
$mail->From = $_POST['email1'];
$mail->FromName = $_POST['name1'];


$mail->addAddress("techdynastic@gmail.com", "Dynastictech Form");

//Address to which recipient will reply
$mail->addReplyTo($_POST['email1'], "Reply");
$mail->isHTML(true);

$mail->Subject = 'hidden_subject';
$mail->Body = 'body';
$mail->AltBody = "Thanks";


if (!$mail->Send()) {
//echo 'error'. mysqli_error($con);
echo 0;
} else {
//echo 'Message has beeen successfully send. we will contact as soon!';
echo 1;
}

带有ajax调用的Html文件

$.ajax({
                        type: "POST",
                        url: "../send_form_email.php",
                        data: dataString,
                        cache: false,
                        success: function (data) {
                            //$(".success_mes").html(data);

                            if (data == 1)
                            {
                                $(".success_mes").html("Message has beeen successfully send.");
                                $('#name').val('');
                                $("#email").val('');
                                $("#phone").val('');
                                $("#subject").val('');
                                $("#comments").val('');
                            } else
                            {
                                $(".mail_error").html("*Something Went Wrong ..");
                            }
                        },
                    });

1 个答案:

答案 0 :(得分:0)

您可以启用Debug以查看更多详细信息:

$mail->SMTPDebug  = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only