简体   繁体   中英

can't send mail through SMTP

For some reason i can't send an email through my free web hosting. The web hosting service says SMTP is enabled but when i click the send button, it will just redirect me to a white blank page. There is no error code, its just a white page. Tell me what's wrong in the code? Suggestions will be appreciated.

PS - input also doesn't work, when i press the send button with every field empty, it still opens up send.php... 输入也不起作用,当我在每个字段为空的情况下按发送按钮时,仍然会打开send.php ...

My html form :

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

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

You have to require class.smtp.php too, right now you are only including the minimal of phpmailer, but to send mails with SMTP, you need to include SMTP too.

You can download it from their github here.

For doing it all a lot more simpler, I would recomend you to use their autoloader instead, this way you don't have to require all the classes you need yourself, you will of course still have to download the required classes.

add after

require_once("class.phpmailer.php");

this

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

then you can replace this line

$mail->SetFrom = $from;

by this

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

And tell me what you get ?

I get just a blank page again...

Replace the send.php by this (don't forget your password)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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