I have php code that loops through client emails and sends them reports using PHPMailer. When run in a browser, it works perfectly. I created a cron job that is able to open and run the php file. However when the cron job runs, the first email is sent but the program stops sending emails. After testing I realized after the first email is sent, the rest of the code stops working and it never loops through to the next 'while' condition.
cron job used (which does open and run the file):
/usr/bin/php -q site1/temp/test.php
The code below will send 3 emails when opened in a browser, but only sends one when run with a cron job:
<?php
$index = 0;
while($index < 3){
require (dirname(__FILE__) . DIRECTORY_SEPARATOR .'PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'my smtp provider'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'name'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->From = 'email';
$mail->FromName = 'ME';
$mail->AddAddress( "test#yahoo.com" );
$mail->isHTML(true);
$mail->Subject = 'Test email';
$mail->Body = "test";
$mail->send();
// nothing else is sent at this point after the first loop through
}//end while
?>
NOTE: using Linux Server (Parallels Plesk)
Try this code :
<?php
require (dirname(__FILE__) . DIRECTORY_SEPARATOR .'PHPMailer/PHPMailerAutoload.php');
$index = 0;
while($index < 3){
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'my smtp provider'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'name'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->From = 'email';
$mail->FromName = 'ME';
$mail->AddAddress( "test#yahoo.com" );
$mail->isHTML(true);
$mail->Subject = 'Test email';
$mail->Body = "test";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
$mail->clearAddresses();
$index++;
}
?>
Using $mail->clearAddresses(); within the loop. and Check which type of error you get.
Related
I know there is several similar threads but I have tried all without any success.
$recipient = ($_POST["to"]);
$mail->AddAddress = ($recipient);
This doesn't work. I have tried with many different combinations as well, like this:
$mail->addAddress = ($recipient, 'name');
I am also running the validate address, which returns true
var_dump(PHPMailer::validateAddress($recipient));
Still I am getting Error: You must provide at least one recipient email address
Any suggestions?
Try this one:
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.mysite.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#mysite.nl'; // SMTP username
$mail->Password = 'secure123'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('info#mysite.nl', 'My cool website'); // The email address of your site goes here
$mail->addAddress('customer#hotmail.com', 'Important customer'); //Destination address and name
$mail->Subject = 'Just saying hi!'; //Title of your mail
$mail->Body = '<h1> A test mail </h1>';
$mail->AltBody = 'Mail has been sent!';
if(!$mail->send()) {
$data = array('mailissend' => false, 'message' => $mail->ErrorInfo);
} else {
echo json_encode('Email is sended');
}
I'm using php mailer for mail triggering.
Its working fine. But I gave 2 to 5 recipients, it sends the mail to only one recipient. In future, I have to trigger a mail to nearly 100 recipients..
I've shared my code below.. Please check it..
require 'phpmailer/PHPMailerAutoload.php';
$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 = 'karthick****#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('karth*******#gmail.com', 'A**n');
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
You're code looks like it should be doing the trick. Make sure that $address doesn't contain any whitespace for the entries. For safe measure, add the trim()
function.
$mail->AddAddress(trim($address));
If that does not work, make sure that you're recipient addresses are real.
Additionally, in case the privacy of the recipients is of concern, I would recommend you use AddBCC() instead of AddAddress() so that their addresses are not revealed.
A basic idea is that make different connection (object) for each your mailing address like below if You don't have so much addresses in your array.
require 'phpmailer/PHPMailerAutoload.php';
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
$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 = 'karthick****#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('karth*******#gmail.com', 'A**n');
$mail->AddAddress($address);
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
UPDATE :
The second idea is that you can remove recepients each time and add new one and then send like below
require 'phpmailer/PHPMailerAutoload.php';
$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 = 'karthick****#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('karth*******#gmail.com', 'A**n');
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
// for clear last recipients
$mail->ClearAllRecipients( )
$mail->AddAddress($address);
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
}
i want to send email after user checkout from cart
my controller:
include('js/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server Gmail
$mail->Mailer = "smtp";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPDebug = 1;
$mail->Username = "my gmail"; //
$mail->Password = "my pass"; // SMTP password
$webmaster_email = "my gmail"; //Reply to this email ID
$email = "recipient gmail"; // Recipients email ID
$name = "John"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Aryono King";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Goeboek I-Mut");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject Test";
$mail->Body = "Test Content"; //HTML Body
if(!$mail->Send()) {echo "Mailer Error: " . $mail->ErrorInfo;}
else {echo "<strong>Email Send</strong>";}
but it show error like this
2015-05-20 21:46:43 SMTP ERROR: Failed to connect to server: (0) 2015-05-20 21:46:43 SMTP connect() failed. Mailer Error: SMTP connect() failed.
what's the problem? i can't solve it, i search everywhere and i can't fint the answer, please someone help me
Take a look at the example here.
While personally, I do it like this:
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://smtp.googlemail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $email_mail; // SMTP username
$mail->Password = $email_pass; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
Try to check against yours, like for example: $mail->Host = 'ssl://smtp.googlemail.com';, which supposed to be don't have ssl://.
Two things you need to check before using phpmailer for gmail
need to check whether SMTP port and email + password is correct or not
You need to authenticate from your gmail account for sending email from unsecured sources, means: when first you send an email, google will send you an email asking your permission to allow to forward email from your id, here your required to click allow
One more thing, Port 587 is working perfectly for me, instead of 465
Hi,
there's more...I have seen that antivirus or firewall installed in your computer may block sending emails via localhost as it may considered as spam attacks
use this code...
include 'PHPMailerAutoload.php';
function send_mail($mail_to,$mail_to_fname,$mail_to_lname,$subject,$message)
{ $mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'mail.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = " YOUR GMAIL ";
$mail->Password = " YOUR GMAIL PASSWORD";
$mail->setFrom(' YOUR GMAIL ', ' YOUR NAME ');
$mail->addAddress($mail_to,$mail_to_fname . " " . $mail_to_lname);
$mail->Subject = $subject;
$mail->msgHTML($message);
$mail->AltBody = ' ';
if (!$mail->send()){echo "FALSE" . $mail->ErrorInfo;}
else{echo "TRUE";}
}
// How to user
// send_email(" email address where to send "," reciepient first name ","reciepient last name "," subject ", " message as html code ");
you need to do one more step for allowing gmail to send messages...!
(*) accept to allow google to send emails from unsecured apps, that link will be sent to your gmail, after you send first email
So I'm trying to just send some basic email from my site to return forgot passwords. I'm not sure why its not working. I've looked around and tried to jus copy and have also tried modifying others' examples.
<?php
$to = 'temp#yahoo.com';
$subject = 'PHPMAILER';
$message = 'TEST MESSAGE';
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'tls'; // Enable encryption (tls), 'ssl' also accepted
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->Port = 587;
$mail->isHTML(true); // Set email format to HTML
$mail->Username = 'temp#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SetFrom = 'webmaster#website.net';
$mail->FromName = 'webmaster';
$mail->WordWrap = 100; // Set word wrap to 50 characters
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->Subject = $subject;
$mail->Body = $message;
$mail->addAddress($to,); // Add a recipient
if(!$mail->send()) {
echo 'Message could not be sent.<br>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
I've tested both port 465 and 587. I'm using Hostgator and I think they are restricting port 587 from use so I've been trying to use port 465. I tested the connection with telnet and it worked fine. When I try to go to the page that is in my test.php it returns a white screen until it times out and I receive error 504. I don't know where it goes wrong but hopefully somebody can help. Thank you.
I use class.phpmailer.php to send a confirmation email. It works perfectly in my local server, but when I upload it to the server in 000webhost.com it no longer works.
I just discovered mail() is not working anymore either.
Is there something I can do to solve this?
Here is the code of the function I use to send every mail in my system:
code
function correoEnviar($asunto, $mensaje, $mensajeTexto, $email)
{
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mail.yahoo.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxx#yahoo.com.mx'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'xxxx#yahoo.com.mx';
$mail->FromName = 'xxxx';
$mail->addAddress($email); // Name is optional
$mail->addReplyTo('xxxx#yahoo.com.mx', 'Information');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
//$mail->Subject = 'Por favor confirme su correo para obtener el libro El fractal y el =?UTF-8?Q?dise=C3=B1o=C2=A0gr=C3=A1fico?=';
$mail->Subject = $asunto;
$mail->Body = $mensaje;
$mail->AltBody = $mensajeTexto;
if(!$mail->send()) {
return $mail->ErrorInfo;
}
else
return 1;
}
echo correoEnviar($asunto, $mensaje, $mensajeTexto, "recipent#gmail.com");
?>
code
Are you sure you are including the file class.phpmailer.php, your code seems fine.
Paste the server's mailserver log, it will provide a better insight.
Your code is fine, it might be a server issue.
A sample code showing SMTP settings
require ("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn of SMTP authentication
$mail->Username = "YAHOO ACCOUNT"; // SMTP username
$mail->Password = "YAHOO ACCOUNT PASSWORD"; // SMTP password
$mail->SMTPSecure = "ssl";
$mail->Host = "YAHOO HOST"; // SMTP host
$mail->Port = 465;