无法通过SMTP

时间:2015-12-04 09:31:01

标签: php email post smtp phpmailer

出于某种原因,我无法通过我的免费网络托管发送电子邮件。网络托管服务说SMTP已启用但是当我点击发送按钮时,它只会将我重定向到一个白色的空白页面。没有错误代码,它只是一个白页。 告诉我代码中的错误是什么? 建议将不胜感激。

P.S - 输入必需也不起作用,当我按下每个字段为空的发送按钮时,它仍会打开send.php ......

我的HTML格式:

<form action="send.php" method="POST">
            <div class="cdiv">
                <input name="name" class="info" type="text" placeholder="Enter Name" required />
            </div>
            <div class="cdiv">
                <input name="email" class="info" type="email" placeholder="Enter Your Email ID" required />
            </div>
            <div class="cdiv" style="height:75px;">
                <textarea name="message" class="info" rows="3" placeholder="Enter Your Message" required></textarea>
            </div>
            <button type="submit" id="sendbtn">SEND</button>
        </form>

我的send.php:

<?php
$body = $_POST['message'];
$subject = 'Automated message';
$from = $_POST['email'];
$from_name = $_POST['name'];
require_once("class.phpmailer.php");
require_once("class.smtp.php");
require_once("class.pop3.php");
$mail = new PHPMailer();  // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465; 
$mail->Username = "dhruv1103@gmail.com";  
$mail->Password = "password";           
$mail->SetFrom = $from;
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress = "dhruv1103@gmail.com";
if(!$mail->Send()) {
    $error = 'Mail error: '.$mail->ErrorInfo; 
    return false;
} else {
    $error = 'Message sent!';
    echo "Sent!";
    return true;
}
?>

2 个答案:

答案 0 :(得分:1)

你也需要class.smtp.php,现在你只包括最小的phpmailer,但是要用SMTP发送邮件,你也需要包含SMTP。

您可以从他们的github here.

下载

为了做到这一切要简单得多,我建议你改用自动加载器,这样你就不必自己需要所有的课程了,你当然还是要下载所需的课程。类。

答案 1 :(得分:0)

后添加

require_once("class.phpmailer.php");

require_once 'class.smtp.php';
require_once 'class.pop3.php';

然后你可以替换 这一行

$mail->SetFrom = $from;

由此

$mail->From     = $from;
$mail->FromName = $from_name;

告诉我你得到了什么?

我再次得到一个空白页面......

用此替换send.php(不要忘记密码)

require_once 'class.phpmailer.php';
require_once 'class.smtp.php';


$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host     = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'dhruv1103@gmail.com';
$mail->Password = 'Your password';
$mail->SMTPSecure = 'tls';
$mail->Port     = 587;
//$mail->SMTPDebug = 2;


$mail->From     = $_POST['email'];
$mail->FromName = $_POST['name'];

$mail->addAddress('dhruv1103@gmail.com');


$mail->WordWrap = 50;
$mail->Priority = 1;
$mail->isHTML(true);

$mail->Subject  = 'Automated message';
$mail->Body     = $_POST['message'];
$mail->AltBody  = 'To view the message, please use an HTML compatible email viewer!';
$mail->CharSet  = 'UTF-8';


if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

echo 'Message has been sent';