Im trying to send bulk of emails containing passwords to the students taking an exam in a particular subject.Now, Im having an error "SMTP Error: Could not connect to SMTP host. Mailer Error () SMTP Error: Could not connect to SMTP host." What could possibly be the problem?
my code as follows:
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
//date_default_timezone_set('America/Toronto');
require_once('PHPMailer-phpmailer-5.2.0/class.phpmailer.php');
include("PHPMailer-phpmailer-5.2.0/class.smtp.php"); //optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "localhost";
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$mail->Host = "mail.yahoo.com"; // sets the SMTP server*/
$mail->Port = 26;
$mail->Username = "***********#yahoo.com"; // SMTP account username
$mail->Password = "****************"; // SMTP account password
$mail->From = "*************#yahoo.com";
$mail->FromName = "Exam System";
//$mail->IsHTML(true);
while ($row_email = mysql_fetch_array ($email)) {
$mail->Subject = "Subject: ".$row_email['subject_description']."";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "This is your password for ".$row_email['exam_title']." : ".$row_email['pass_password']."";
$mail->AddAddress($row_email['stud_email']);
if(!$mail->Send()) {
echo "Mailer Error (" . str_replace("#", "#", $row_email['stud_email']) . ') ' . $mail->ErrorInfo . '<br />';
} else {
echo "Message sent to :" . $row_email['stud_email'] . ' (' . str_replace("#", "#", $row_email['stud_email']) . ')<br />';
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}
mysql_free_result($email);
?>
Try this...
add this code for Set secutiry
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
Try with gmail, as i am not aware of Yahoo's SMTP server. [Also, use the gmail credentials (username, password)]
$mail->Host = "smtp.gmail.com";
function SendMailWithGmailSMTP($author, $email, $text)
{
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost"; // Sets the SMTP hosts
$mail->Port = 587; // Sets the default SMTP server port.
$mail->SMTPAuth = false; // Sets SMTP authentication. Utilizes the Username and Password variables.
$mail->Username = "...#gmail.com";
$mail->Password = "...";
$mail->From = $email;
$mail->FromName = $author;
$mail->CharSet = "ISO-8859-9";
$mail->AddAddress("...#gmail.com");
$mail->Subject = "Hi. This is a Subject!";
$mail->IsHTML(true);
$mail->Body = $text;
if($mail->Send()) return true; else echo '<script language="javascript">alert("'.$mail->ErrorInfo.'");</script>';
}
Write this function for send mail and call this function like this:
SendMailWithGmailSMTP("Aziz Yılmaz", "...#gmail.com", "Bla bla bla")
Related
I am trying following code to send mail
but it is showing
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
and gmail is informing me that Sign-in attempt prevented
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'sss#gmail.com';
$mail->Password = '*******';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->From = 'abc#gmail.com';
$mail->FromName = 'asdf ';
$mail->addAddress('abc#gmail.com', 'sadf ');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "Using PHPMailer";
$mail->Body = "Hi Iam using PHPMailer library to sent SMTP mail from localhost";
if(!$mail->send()) {
echo "Message could not be sent.";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
How to resolve above problem?
Try the following steps:
enable debug mode to catch possible errors
$mail->SMTPDebug = 1;
enable SMTP authentication
$mail->SMTPAuth = true;
also check for SSL support in php configuration file (php.ini)
extension=php_openssl.dll
You need set permission for send mail with gmail.
- Login Google Account
- Go Privacy Page
- Allow third party apps
After try this code:
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Since you are getting email from Google, it describes the email is trying to send but it is blocked by Google. Do the following steps.
I hope this helps.
Check if IMAP is enabled
Check here and enable less secure apps
Display Unlock Captcha
change the following in your code
isSMTP() to IsSMTP() , addAddress() to AddAddress() & isHTML() to IsHTML().
and yes check the ports also. sometoimes port also off which do not let connection to be established.
Hope it will work!
I think you have to enable POP and IMAP in you gamil. Try this
Sign in to Gmail
Click the gear in the top right.
Select Settings.
Click Forwarding and POP/IMAP.
Select Enable IMAP.
Click Save Changes.
<?
$account="email_address#gmail.com";
$password="accountpassword";
$to="mail#subinsb.com";
$from="email_address#gmail.com";
$from_name="Name";
$msg="<strong>This is a bold text.</strong>"; // HTML message
$subject="Database Backup";
/*End Config*/
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth= true;
$mail->Port = 465; // Or 587
$mail->Username= $account;
$mail->Password= $password;
$mail->SMTPSecure = 'ssl';
$mail->From = $from;
$mail->FromName= $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->addAddress($to);
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "E-Mail has been sent";
}
?>
I am trying to send an email from localhost with PHP. Here is my code:
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
require_once('class.phpmailer.php');
require 'PHPMailerAutoload.php';
require 'class.smtp.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = false; // turn on SMTP authentication
$mail->Username = "TTTTTT#gmail.com"; // SMTP username
$mail->Password = ""; // SMTP password
$mail->Port = 25;
$mail->SMTPSecure = '';
$mail->From = $email;
$mail->AddAddress("bradm#inmotiontesting.com", "Brad Markle");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
When I run this code, it shows, "message has been sent" but it does not actually send the message. What is my problem?
change this code you have:
$mail->Username = "TTTTTT#gmail.com"; // SMTP username
$mail->Password = ""; // SMTP password
$mail->Port = 25;
$mail->SMTPSecure = '';
to this:
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
In addition to what Lelio Faieta posted, enable less secure apps in gmail
and to prevent your mail going to your spam
change
$mail->From = $email;
to your email and you can put $email in body of your message
date_default_timezone_set('Asia/Dubai');
include("classes/class.phpmailer.php");
$mail = new PHPMailer();
$body = "this is <strong>testing</strong> mail ". date('Y-m-d H:i:s');
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = 'my#email.com';
$mail->Password = '*******';
$mail->SetFrom('my#email.com', 'First Last');
$mail->AddReplyTo('my#email.com','First Last');
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "to#email.com"; // add your address here
$mail->AddAddress($address, "Gmail Test");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I have script like this.Its working fine with localhost but when i moving to windows or linux servers it won't work.I want to work on both windows and linux servers.What should i do?
Error like this:
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.
try using this :
require_once ( 'class.phpmailer.php' ); // Add the path as appropriate
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server
$Mail->SMTPDebug = 2; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "tls"; //Secure conection
$Mail->Port = 587; // set the SMTP port
$Mail->Username = 'MyGmail#gmail.com'; // SMTP account username
$Mail->Password = 'MyGmailPassword'; // SMTP account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'Test Email Using Gmail';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = 'MyGmail#gmail.com';
$Mail->FromName = 'GMail Test';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->AddAddress( $ToEmail ); // To:
$Mail->isHTML( TRUE );
$Mail->Body = $MessageHTML;
$Mail->AltBody = $MessageTEXT;
$Mail->Send();
$Mail->SmtpClose();
if ( $Mail->IsError() ) {
echo "ERROR<br /><br />";
}
else {
echo "OK<br /><br />";
}
What version of PHPMailer you are using ? if you are using old version try with
$mail->Port = 587;
try to update PHPMailer version that will solve i hope !!!
i have phpmailer and i can send email via php page without any problem
but the sender automatically by username it i am use in smtp server
i want take sender email from user who write message not from default sender
and this is form code
<?php
require '../../PHPMailer/PHPMailer-master/PHPMailerAutoload.php';
$name = $_POST['name'];
$Institute = $_POST['Institute'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Debugoutput = 'html';
//$mail->SMTPDebug = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "MyGmail";
$mail->Password = "MyGmailPass";
$mail->setFrom('Mygmail', $name);
$mail->addReplyTo('MyGmail', 'First Last');
$mail->addAddress('MyEmail', 'Nasser');
$mail->Subject = 'Database Site Reminder';
$mail->Body = ($message);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
i am put `$mail->setFrom('Mygmail', $name); this like
$mail->setFrom($email, $name);
because take sender email from user , and i get Message sent
but message not arrive to my email
i cant find any solution... please assist me
thanks ...
$mail = new PHPMailer();
$body = "Body text goes here";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "Gmail Id"; // GMAIL username
$mail->Password = "PaSsWoRd"; // GMAIL password
$mail->SetFrom('fromemail#gmail.com', 'User');
$mail->AddReplyTo("fromemail#gmail.com","User");
$mail->Subject = "Subject Goes Here";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('toemail#gmail.com', 'User');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo("Success");
}
try this code... its working....
If you are using PHP Mailer from Github,
then you can set the Sender name by,
$mail->SetFrom("info#mibckerala.org", "MIBC");
I'm trying to configure PHPMailer I've uploaded 1 file which is class.phpmailer.php and created another php file with this content:
<?php
require('class.phpmailer.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // 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; // or 587
$mail->IsHTML(true);
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom("the same email address");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("the same email address");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
and i get nothing at all not a success message nor a failure message. http://www.mawk3y.net/mailtest/test.php
all answers are outdated now. Most current version (as of Feb 2018) does not have autoload anymore, and PHPMailer should be initialized as follows:
<?php
include_once(FCPATH.'PHPMailer/src/PHPMailer.php');
include_once(FCPATH.'PHPMailer/src/SMTP.php');
include_once(FCPATH.'PHPMailer/src/Exception.php');
$msj="My complete message";
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
//authentication SMTP enabled
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
//indico el puerto que usa Gmail 465 or 587
$mail->Port = 465;
$mail->Username = "xxxxxx";
$mail->Password = "xxxx";
$mail->SetFrom("xxxxxx#xxxxx.com","Name");
$mail->AddReplyTo("xxx#xxx.com","Name Replay");
$mail->Subject = "Test";
$mail->MsgHTML($msj);
$mail->AddAddress("xxxxxx#xxxxx.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
The problem is with the require method.
You first have to extract all file of phpMailer repository.
instead of writing
require('class.phpmailer.php');
you need to include path where your PHPMailerAutoload.php file extracted.so you can replace it with.
require('path-of-extracted-folder/PHPMailerAutoload.php');
for more reference you can visit it's GitHub link
https://github.com/PHPMailer/PHPMailer
I had same problem but I solved.This is the way I code
<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
// Set PHPMailer to use the sendmail transport
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'abc#gmail.com'; // SMTP username
$mail->Password = 'abc'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Set who the message is to be sent from
$mail->setFrom('abc1#gmail.com', 'First Last');
//Set an alternative reply-to address
//Set who the message is to be sent to
$mail->addAddress('abc#gmail.com', 'Shehan');
//Set the subject line
$mail->Subject = 'Test Mail';
$mail->Body = 'This is Test Mail';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>