sending email to gmail account using phpmailer - php

<?php
require_once 'PHPMailer-master/class.phpmailer.php';
require_once 'PHPMailer-master/class.phpmaileroauthgoogle.php';
require_once 'PHPMailer-master/PHPMailerAutoload.php';
require_once 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = TRUE;
//$mail->SMTPDebug =2;
$mail->Host = 'smtp.gmail.com';
$mail->Username = 'zhaider113#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 587;
$from = 'shahghafoor439#gmail.com';
$mail->setFrom($from, 'Ghafoor Shah');
$mail->addReplyTo($from, 'Ghafoor Shah');
$mail->addAddress('zhaider113#gmail.com', 'zeeshan');
$mail->Subject = 'This is subject';
$mail->Body = 'This is the body of email';
$mail->AltBody = 'This is the body of email';
$mail->send();
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
?>
I try to send email but it not send and give error message which is:SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting */

try with $mail->SMTPSecure = 'tls';
See full example at PHPMailer's gmail example below: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

Make sure you include correct PHPMailer Library.
And make this changes
Some Servers not response for SSL(Secure). So that change this $mail->SMTPSecure = 'tls';
And In your code you have two Mail sending option $mail->send();
//$mail->send();//Comment this
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
Now either mail sent Or either It will print Error log

Related

PHPMailer does not execute code after $mail = new PHPMailer

I have made a website on my localhost. After everything was ready to go I hosted the website. However, now PHPMailer seems to cause the trouble. Mails are not sending and it seems like PHP code does not execute after $mail = new PHPMailer command. Here is part of the code:
if($conn->query($sql)){
require_once '/home/ziptie/public_html/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = true;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mygmail#gmail.com'; // your email id
$mail->Password = 'mypassword'; // your password
$mail->SMTPSecure = 'tls';
$mail->Port = 587; //587 is used for Outgoing Mail (SMTP) Server.
$mail->setFrom('mygmail#gmail.com', 'ZIPTIE');
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$v="http://ziptie.rs/verification-page.php?sifra=$token";
$body = file_get_contents('/home/ziptie/public_html/verificationemail.html');
$body = str_replace('promenljiva', $v, $body);
$mail->Subject = 'Verifikacija kupca, ZIPTIE';
$mail->Body = $body;
$mail->addAttachment("naruzbine ".date("d-m-Y").".zip");
$mail->send();
header("Refresh:0; url=https://www.ziptie.rs/verification-page.php");
//echo '<script>alert("Verifikacioni link je poslat, proverite Vašu mejl adresu.")</script>';
/*if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}*/
}
else{
echo $conn->error;
}
Does anybody know the solution? Thanks!
found solution in upgrading to PHPMailer 6.3

PHPmailer not sending messages to gmail

I am trying to send messages from my website specialeducationnotes.co.in but not able to send the messages.
<?php
if(isset($_POST['query-submit'])) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
// $mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail#gmail.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$from = $_POST['email'];
$subject = $_POST['subject'];
$mail->From = $from;
$mail->FromName = $_POST['name'];
$mail->addAddress('specialeducationnotes1#gmail.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = '<h2 style="text-align: center;"s>Name: '.$_POST['name'].'<br>Email: '.$_POST['email'].'<br>Message: '.$_POST['query'].'</h2>';
$mail->AltBody = 'Hello! Hola! Namaste!';
if(!$mail->send()) {
//echo 'Message could not be sent.';
echo "<script>alert(' Your message could not be sent!');</script>";
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//echo 'Message has been sent';
echo "<script>alert(' Your message is sent!');</script>";
echo "<script>document.location.href='index.php'</script>";
}
}
?>
I just get a blank page with no error message or any text and email is also not sent. Can you tell what's wrong with my code?

send mail from local server using xampp in php

<?php
class EmailModel extends CI_Model{
public function email(){
require 'Email Files/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'Google account username';
$mail->Password = 'Google account Password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->addAddress('abc#gmail.com');
$mail->setFrom('abc#gmail.com');
$mail->Subject = 'Test';
$mail->Body = 'Testing';
if($mail->send()) {
echo 'Message has been sent';
} else {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
}
?>
I am using Github library for php email.
when I var_dump($mail) ; its showing all the data in the array.
now, this code is showing error: "SMTP connect() failed"
I also had this problem.Do the following steps
Go to myaccount.google.com
Click "connected apps & sites",
Turn "Allow less secure apps" to "ON" (bottom of the page).
Just I checked up with your code no error in that. you've to enable https://www.google.com/settings/security/lesssecureapps
<?php
include "PHPMailer_5.2.4/class.phpmailer.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'test#gmail.com';
$mail->Password = 'test#12123#';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->addAddress('abc#gmail.com');
$mail->setFrom('abc#gmail.com');
$mail->Subject = 'Test';
$mail->Body = 'Testing';
if($mail->send()) {
echo 'Message has been sent';
} else {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

Getting trouble with SMTP connect with PHPMailer

I am trying to send SMTP secured mail with attachment using PHPMailer.
I made this function with PHPMailer library
public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
require getcwd() .'/lib/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'tester#mydomain.com';
$mail->Password = '**************';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom($from_mail, $from_name);
$mail->addAddress($mail_to);
$mail->addReplyTo($replyto, 'no reply');
$mail->addAttachment($path);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
} else {
$sucess = 'Mail sent!';
echo $sucess;
}
}
Now if I comment the line $mail->isSMTP(); it's working fine . but I think it's not SMTP secured. Else I am getting this message: "Mail error: SMTP connect() failed. "
I searched for this problem but didn't get the proper answer to what I am looking for. Please help me.
I am working in a dev server with HTACCESS protection
Well I have found the details to fix the issue here https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting .
I have just Turned on less secure apps https://www.google.com/settings/security/lesssecureapps from my gmail account.
This takes few minutes or an hour to active.
My current code :
public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
require getcwd() .'/lib/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
//$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'mymail#gmail.com';
$mail->Password = 'mypass';
$mail->setFrom($from_mail, $from_name);
$mail->addAddress($mail_to);
$mail->addReplyTo($replyto, 'no reply');
$mail->addAttachment($path);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
} else {
$sucess = 'Mail sent!';
echo $sucess;
}
}
Else you have to Setup a app from console.developers.google.com
Follow this guide : https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2

I am receiving this error from phpmailer Message could not be sent.Mailer Error: SMTP connect() failed?

I am working on phpmailer but I am receiving this error, I dont know what is wrong. I search and did many changes. but noting helped me.
I am testing both on live server and on localhost.
on both I am receiving the error
Message could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Please have a look on my code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
require("PHPMailer/class.phpmailer.php"); // path to the PHPMailer class
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'stockholm85#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'agohar1985#gmail.com';
$mail->FromName = 'Mailer';
//$mail->addAddress('zia_gt#yahoo.com', 'Zia Ullah'); // Add a recipient
$mail->addAddress('zia_gt#yahoo.com'); // Name is optional
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
I have folder on name PHPMailer and mail.php is the file.
I will appreciate your support because I am new to php
Thank you in advance
open your cmd and type the following to get php-mailer,
wget https://github.com/PHPMailer/PHPMailer/archive/master.zip
unzip the master. mention the file path clearly in your code.
Here
<?php
require 'PHPMailer-master/PHPMailerAutoload.php'; //file path
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#gmail.com';
$mail->Password = '_password_';
$mail->SMTPSecure = 'tls';
$mail->From = 'sender#example.com';
$mail->FromName = 'Your Name';
$mail->addAddress('recipient#example.com');
$mail->isHTML(true);
$mail->Subject = 'Test Mail Subject!';
$mail->Body = 'This is SMTP Email Test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
test it

Categories