0
SMTP settings...

I have been trying to send email from my local host using PHPMailer but I can't fix this error:

Mailer Error: SMTP connect() failed

$from = $_POST['Smssettings']['mail_from'];
$to = $_POST['Smssettings']['mail_to'];
$subject = $_POST['Smssettings']['mail_subject'];
$message = $_POST['Smssettings']['mail_message'];
$password="mypassword";//saving password to a varible

$mail = new PHPMailer();
$mail->IsSMTP();//Set mailer to use SMTP
$mail->CharSet = "UTF-8";
$mail->SMTPSecure = 'tls';
$mail->SMTPKeepAlive = true;  
$mail->Mailer = "smtp"; 
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = $from;/*SMTP username*/
$mail->Password = $password;/*SMTP password*/
$mail->SMTPAuth = true;/*Enable SMTP authentication*/
$mail->SMTPDebug = 0;//set debug
$mail->From = $from;// from address to which mail is sent
$mail->FromName = 'Name';// specifying sender name
$mail->AddAddress($to);// specifying to address
$mail->AddReplyTo($from, 'Information');// specifying the mail address to which replay is sent
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = $message;
chresse
  • 5,486
  • 3
  • 30
  • 47
Prathibha
  • 3
  • 4
  • where is your class.phpmailer.php and class.smtp.php file – Yogesh Prajapati May 30 '16 at 11:56
  • Possible duplicate of [SMTP Connect() failed. Message was not sent.Mailer error: SMTP Connect() failed](http://stackoverflow.com/questions/18496650/smtp-connect-failed-message-was-not-sent-mailer-error-smtp-connect-failed) – JYoThI May 30 '16 at 11:59

1 Answers1

0

You can use this coding for sending mail in localhost.But please include library.php,class.phpmailer.php and class.smtp.php before using this coding and I hope you have those files.

<?php
    include 'library.php'; // include the library file
    include "classes/class.phpmailer.php"; // include the class name
    include "classes/class.smtp.php";
    if(isset($_POST["send"])){
        $email = $_POST["email"];
        $mail   = new PHPMailer; // call the class 
        $mail->IsSMTP(); 
        $mail->SMTPAuth = true;     // turn on SMTP authentication
        $mail->SMTPSecure = "ssl";

        $mail->Host = "smtp.gmail.com"; //Hostname of the mail server
        $mail->Port = 465; //Port of the SMTP like to be 25, 80, 465 or 587
        $mail->SMTPDebug = 1;
        //$mail->SMTPAuth = false; //Whether to use SMTP authentication
        $mail->Username = "your gmail id"; //Username for SMTP authentication any valid email created in your domain
        $mail->Password = "your password"; //Password for SMTP authentication
        $mail->AddReplyTo("abcd@gmail.com", "Reply name"); //reply-to address
        $mail->SetFrom("your gmail id", "XYZ"); //From address of the mail
        // put your while loop here like below,
        $mail->Subject = "Your SMTP Mail"; //Subject od your mail
        $mail->AddAddress($email, "Asif18"); //To address who will receive this email
        $mail->MsgHTML("<b>Hi, your first SMTP mail has been received."); //Put your body of the message you can place html code here
         //Attach a file here if any or comment this line, 

        $send = $mail->Send(); //Send the mails
        if($send){
            echo '<center><h3 style="color:#009933;">Mail sent successfully</h3></center>';
        }
        else{
            echo '<center><h3 style="color:#FF3300;">Mail error: </h3></center>'.$mail->ErrorInfo;
        }
    }
    ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Send mails using SMTP and PHP in PHP Mailer using our own server or gmail server by Asif18</title>
    <meta name="keywords" content="send mails using smpt in php, php mailer for send emails in smtp, use gmail for smtp in php, gmail smtp server name"/>
    <meta name="description" content="Send mails using SMTP and PHP in PHP Mailer using our own server or gmail server"/>
    <style>
    .as_wrapper{
        font-family:Arial;
        color:#333;
        font-size:14px;
    }
    .mytable{
        padding:20px;
        border:2px dashed #17A3F7;
        width:100%;
    }
    </style>
    <body>
    <div class="as_wrapper">
        <h1>Send mails using SMTP and PHP in PHP Mailer using our own server or gmail server</h1>
        <form action="" method="post">
        <table class="mytable">
        <tr>
            <td><input type="email" placeholder="Email" name="email" /></td>
        </tr>
        <tr>
            <td><input type="submit" name="send" value="Send via SMTP" /></td>
        </tr>
        </table>
        </form>
    </div>
    </body>
    </html>
aarju mishra
  • 710
  • 3
  • 10
  • it's not work. i want to send email in live site not from localhost. when i am trying to send email it showing the error SMTP Error: Could not authenticate. – Prathibha May 31 '16 at 05:13
  • In that case.You can do one thing.First change "ssl" to "tls" and port to 587.Then first you need not to login to your gmail account which you gonna used in this coding.then open this link: http://www.google.com/accounts/DisplayUnlockCaptcha After that follow the steps and when access is enabled then try to run your program – aarju mishra May 31 '16 at 05:18