繁体   English   中英

无法通过SMTP发送邮件

[英]can't send mail through SMTP

由于某些原因,我无法通过免费的虚拟主机发送电子邮件。 网络托管服务说SMTP已启用,但是当我单击“发送”按钮时,它将仅将我重定向到白色空白页。 没有错误代码,仅是一个白页。 告诉我代码有什么问题吗? 建议将不胜感激。

PS- 所需的输入也不起作用,当我在每个字段为空的情况下按发送按钮时,仍然会打开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;
}
?>

您还必须要求class.smtp.php ,现在只包括最少的phpmailer,但是要使用SMTP发送邮件,您也需要包含SMTP。

您可以在此处从其github下载

为了使这一切变得更加简单,我建议您改为使用其自动加载器,这样,您不必自己需要所有类,当然您仍然必须下载所需的类。

加入之后

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';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM