I need to send over 1000 mails and I am using PHPMailer for it.
I am sending mails as HTML and using SMTP.
My code looks like:
while($count<1000)
{
try{
if($count%20==0)
{
$mail = new PHPMailer;
$mail->SMTPDebug = true; // Enable verbose debug output
//$mail->SMTPDebug = 3;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // 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->Timeout = 200;
$mail->SMTPKeepAlive = true;
$mail->setFrom(username, sender);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
}
$msg = htmlspecialchars_decode($message);
$msg = substr($msg,0,-14);
$mail->Body = $msg;
$mail->addAddress(emails);
if($mail->send())
{
$flag="1";
}
$count++;
$mail->ClearAddresses();
$mail->Smtpclose();
}catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
}
Atmost 20 mails are only sent at a time.
Is it because mails may contain spam?
Suggest you to delay your loop, you can use sleep.
I think you can send 5 per second with no problem, but you need to check with your email provider.
Related
I have the following code sending email shipping confirmations to my customers:
while ($i < count($tracked) ) {
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.office365.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#mycompany.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('support#mycompany.com', 'mycompany');
$mail->addAddress($tracked[$i]['customers_email_address'], $tracked[$i]['customers_name']); // Add a recipient
$mail->addReplyTo('support#mycompany.com', 'mycompany');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = EMAIL_TEXT_SUBJECT;
$mail->Body = $html;
$mail->AltBody = 'Your order with mycompany.com has shipped';
$mail->send();
echo 'order confirmation sent to order#:'.$tracked[$i]['orders_id'].'<br/>';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
$i++;
}
It appears to be sending to multiple emails. What I believe is happening is that in the while loop each new customers address is added to the list without clearing it. What doesnt seem to be happening though is the message does not seem to be duplicated? Wouldn't that duplicate at as well with each pass in the loop?
In any case I think the best thing to do before the try is:
$mail = new PHPMailer(true);
so that with each pass of the while loop the $mail object would be instantiated again. Is that proper? I know that the clearAllRecipients() function exists but i also want to be sure the body is cleaned as well.
it looks like your looping through an array of $tracked. Why not use a foreach loop? Then you don't need to use a counter.
foreach ($tracked as $track ) {
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.office365.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#mycompany.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('support#mycompany.com', 'mycompany');
$mail->addAddress($track['customers_email_address'], $track['customers_name']); // Add a recipient
$mail->addReplyTo('support#mycompany.com', 'mycompany');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = EMAIL_TEXT_SUBJECT;
$mail->Body = $html;
$mail->AltBody = 'Your order with mycompany.com has shipped';
$mail->send();
echo 'order confirmation sent to order#:'.$track['orders_id'].'<br/>';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
It's because addAddress() does exactly what its name suggests; it adds an address that a message will be sent to. It doesn't set the addresses a message will be sent to.
Unsurprisingly there is a method that allows you to clear the list of addresses, called clearAddresses(), so just call that at the end of your sending loop after send() and your issue will be solved.
I'd also recommend that you base your code on the mailing list example provided with PHPMailer as it will be much faster than the code you have here. Also read the docs on sending to listshttps://github.com/PHPMailer/PHPMailer/wiki/Sending-to-lists) for furtehr advice.
My PHPMailer seems to work, but loops through the mailer only 6 times instead of 24 times. if loop used without mailer, it does return 24 addresses. I'm connected to an internal network with access from outside. can anyone clear up what is going on. ($link -> close()) is done at a later stage. also the 24 emails sent two 2 different email accounts(for testing purposes), both receive 3 emails from this phpMailer. found many posts regarding phpMailer, I haven't encountered this one so far.
if ($result = $link->query("SELECT Adres FROM Emails")) {
//Alle variabelen voor de mail
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPKeepAlive = true;
$mail->Host = 'smtp-mail.outlook.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**********#outlook.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('**********#outlook.com');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'APP';
$mail->Body = 'is this working??';
$mail->AltBody = 'is this working??';
while($row = mysqli_fetch_row($result)) {
$variable = $row[0];
$mail->addAddress($variable);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
$mail->ClearAllRecipients( );
}
$result->close();
}
You're not checking that addAddress succeeds, for example if the email addresses are invalid. If you set SMTPDebug = 2, you can watch the SMTP conversation.
Check addresses like this:
$variable = $row[0];
if (!$mail->addAddress($variable)) {
echo "skipping invalid address $variable";
continue;
}
I am trying to send different messages to different users. I made an array of email addresses and while iterating through it, I want to send message2 to user2.
While reusing the same mail instance, at the beginning of each iteration I declare $mail -> ClearAddresses(), but now user2 gets the message of user1, and user2... and so one.
What am I missing that the Address won't get cleared at the beginning of the iteration?
Thanks!
// settings
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'xxx'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx'; // SMTP username
$mail->Password = 'xxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;
$mail->CharSet = "UTF-8"; // TCP port to connect to
function sendInvoice($mail, $addresses) {
foreach($addresses as $recipient) {
$mail->ClearAddresses();
$mail->setFrom('mail#domain.eu', 'My Server');
$mail->addAddress($recipient['email'], $recipient['name']); // Add a recipient
$mail->addReplyTo('mail#domain.eu', 'My Server');
$mail->isHTML(true);
$mail->Subject = $recipient[subject];
//$mail->Body = $message;
$mail->MsgHTML($recipient[message]);
if (! $mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//echo 'Message has been sent';
}
}
}
In your code, change:
$mail->ClearAddresses();
to:
$mail->ClearAllRecipients();
This should fix the problem.
I am using php-mailer to send email.
The mail goes succesfully however in each email I get "mailed-by: gamil.com" and signed-by: "gmail.com" in mail header.
How can I remove mailed-by and signed-by tag from email ?
Here is my code
require_once('././php_mailer/class.phpmailer.php');
$body = html($msg);
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = 'smtp.gmail.com'; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com'; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = 'nabina.smartmobe#gmail.com'; // SMTP account username
$mail->Password = '******'; // SMTP account password
$mail->AddAddress($to, 'Nabina');
$mail->SetFrom('no-reply#nayacinema.com.np', 'no-reply#nayacinema.com.np');
$mail->AddReplyTo('no-reply#nayacinema.com.np', 'no-reply#nayacinema.com.np');
$mail->IsHTML(true);
$mail->Subject ='NayaCinema: '.$subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
if($attachment){
$mail->AddAttachment($attachment);
}
if($mail->Send()){
//echo 'sent'; die;
return true;
}else{
//echo ' not sent'; die;
return false;
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Thank You
I created a php application at dan.creativeloafing.com. This just takes form data and builds an html page with it, then emails the contents of that page to dan#creativeloafing.com. A couple days ago, it stopped working. I have been trying to figure this out ever since. I was using the mail() php function and have switched it over to the PHPMailer Library. This is supposedly sending the emails and I get confirmation, but nobody ever recieves the email and I get no bounceback or any errors. This is the jist of the code:
//PHPMailer
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'relay-hosting.secureserver.net'; // Specify main and backup server
$mail->Port = 25;
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->SMTPSecure = 'tsl'; // Enable encryption, 'ssl' also accepted
$mail->Username = 'dan#omgsurvey.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPDebug = 0;
$mail->WordWrap = 50;
$mail->From = 'dan#omgsurvey.com';
$mail->FromName = 'DAN Application';
$mail->addAddress('dan#creativeloafing.com'); // Name is optional
$mail->addReplyTo($repEmail);
$mail->addCC('david.miller#creativeloafing.com');
$mail->isHTML(true);
$mail->Subject = "New DAN Request: ".$campaignName;
$mail->msgHTML(file_get_contents('./tmp/DAN_REQUEST_'.$specialString.$randomNumber.'.html'));
if(!$mail->send()) {
echo '<br />Proposal could not be sent.<br />';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
} else {
echo 'Proposal has been sent';
}
The script always reaches 'Proposal has been sent.' too. This is driving me crazy!
So what was happening here is that godaddy was blocking emails that included my domain name in them. I am not sure if this was a spam issue, but they are currently looking into it. I have gotten the emails to send using a simple mail() function and by removing any references to omgsurvey.com in the email. Silly, this mail was only ever sent to two email addresses!
Isn't:
$mail->SMTPSecure = 'tsl';
supposed to be:
$mail->SMTPSecure = 'tls';
Use try...catch and PHPMailer(true); https://github.com/Synchro/PHPMailer/blob/master/examples/exceptions.phps
//Create a new PHPMailer instance
//Passing true to the constructor enables the use of exceptions for error handling
$mail = new PHPMailer(true);
try {
mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'relay-hosting.secureserver.net'; // Specify main and backup server
$mail->Port = 25;
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->SMTPSecure = 'tsl'; // Enable encryption, 'ssl' also accepted
$mail->Username = 'dan#omgsurvey.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPDebug = 0;
$mail->WordWrap = 50;
$mail->From = 'dan#omgsurvey.com';
$mail->FromName = 'DAN Application';
$mail->addAddress('dan#creativeloafing.com'); // Name is optional
$mail->addReplyTo($repEmail);
$mail->addCC('david.miller#creativeloafing.com');
$mail->isHTML(true);
$mail->Subject = "New DAN Request: ".$campaignName;
$mail->msgHTML(file_get_contents('./tmp/DAN_REQUEST_'.$specialString.$randomNumber.'.html'));
$mail->send();
echo 'Proposal has been sent';
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Thinking send() should be Send()
if(!$mail->Send()) {