I am going to send different emails to different people.
First email sent normal but second one is waiting for 180 sec and then start to send. I couldn't find any default settings. Once sent an email others are going to POOLING and failed it.
I have different bodies and different subjects.
code 1:
sendEmail(false, $email, $message, $subject, $dep_type);
sendEmail(true, $email, $message_client, $subject_client);
sendEmail function :
function sendEmail($client, $email, $message, $subject, $dep_type = null)
{
$from_mail = 'hello#example.com';
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->From = $from_mail;
$mail->FromName = "SenderName";
if ($client) {
$mail->addAddress($email);
} else {
$mail->addAddress('welcome#example.com');
}
$mail->addReplyTo($from_mail, 'name');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
}
https://aws.amazon.com/de/premiumsupport/knowledge-center/ec2-port-25-throttle/
Amazon EC2 throttles traffic on port 25 of all EC2 instances by
default, but you can request for this throttle to be removed.
Related
I'm sending emails with a php function sendEmail(), which gets triggered through an API call. The function is called up to 3-5 times in a minute but max 300 times a day.
Sometimes I get this error and the emails don’t get sent.
Mailer Error: The following From address failed: info#domain :
MAIL FROM command failed,Error: too much mail from Server IP,
450,4.7.1SMTP server error: MAIL FROM command failed Detail: Error:
too much mail from Server IP
I always send a second email to my email account. And here I also get sometimes the same error.
My Email server allows me to send 1000 emails in 10 minutes and max 3 SMTP connections at the same time.
My Function:
function sendEmail($serverName, $from, $subject, $to, $filePath, $msg){
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
$mail->Host = $serverName;
$mail->SMTPAuth = true;
$mail->Username = $from;
$mail->Password = <PASSWORD>;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
//Recipients
$mail->setFrom($from, $subject);
$mail->addAddress($to); //Add a recipient
$mail->addReplyTo($from, 'Info Domain');
$mail->addCC($to);
$mail->addBCC($to);
//Attachments
$mail->addAttachment($filePath);
//Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->send();
return true;
} catch (Exception $e) {
return false;
}
Some ideas?
In your code I could see the following:
$mail->addAddress($to);
$mail->addCC($to);
$mail->addBCC($to);
Why you have added '$mail->addCC' and '$mail->addBCC' to the same recipient '$to'?
Your mail server can count CC and BCC as different mail addresses and each send process will be counted as 3 mails, not one. Just remove $mail->addCC($to) and $mail->addBCC($to). They are not need if you already have '$mail->addAddress($to);'.
I am using PHPMailer to send emails. I have created a function that sends 3 emails to 3 different email addresses (sending 9 emails in total).
The first email address is receiving all the 3 emails.
The second email address is receiving 2 emails.
The third email address is receiving only 1 email.
Why this happening?
Here is my code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'lib/phpmailer/vendor/autoload.php';
$mail = new PHPMailer(true);
$mail1 = phpmaileremail($reciever1, $usertype1, $file, $subject1, $body1);
$mail2 = phpmaileremail($reciever2, $usertype2, $file, $subject2, $body2);
$mail3 = phpmaileremail($reciever3, $usertype3, $file, $subject3, $body3);
function phpmaileremail($reciever,$usertype, $file, $subject, $body)
{
global $mail;
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxx#gmail.com';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('xxx', 'xxx');
$mail->addAddress($reciever);
$mail->addAddress($reciever, $usertype);
$mail->addAttachment($file);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = 'NA';
$mail->send();
echo "Mail sent";
}
Because you're reusing the $mail object to addAddress() and send(). So the first time you call phpmaileremail() the first address gets the email. Then when you call it for the second time the second address is added and the first and second address get the email. And so on.
A simple solution would be to create the $mail object inside the phpmaileremail() function:
function phpmaileremail($reciever,$usertype, $file, $emailsubject, $email_body )
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXXX#gmail.com';
$mail->Password = 'XXXXXXXXXXXXXXXXXX';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
$mail->addAddress($reciever);
$mail->addAddress($reciever, $usertype);
// Attachments
$mail->addAttachment($file); // Add attachments
$mail->isHTML(true);
$mail->Subject = $emailsubject;
$mail->Body = $email_body;
$mail->AltBody = 'NA';
$mail->send();
echo "Mail sent";
}
PS: Not that it matters, but reciever is written receiver. I've made that mistake as well.
Kiko's answer will work, however it's not the best way. As its name suggests, addAddress adds an address, it doesn't set absolutely or replace existing recipients you've already added.
PHPMailer has a standard function to clear the list of addresses you're ending to called clearAddresses, so the right approach is to call that after each message you send and add the new address before sending the next one, so the sequence will be roughly:
addAddress();
send();
clearAddresses();
addAddress();
send();
and so on. This is most clearly demonstrated in the mailing list example provided with PHPMailer, which does its sending in a loop, calling clearAddresses each time around.
You can achieve the same thing using a new instance of PHPMailer each time (which has the effect of clearing addresses, but also clears everything else too), but it's more efficient to re-use the instance. This is especially true if you're sending over SMTP (which you are) because it will allow you to make use of keepalive, which dramatically reduces the overhead of making an SMTP connection. If you use a new instance, the connection is dropped and recreated each time. You can achieve this inside your function by making the PHPMailer instance static:
function phpmaileremail($reciever, $usertype, $file, $emailsubject, $email_body)
{
static $mail;
if ($mail === null) {
//Set everything that remains the same all the time in here
$mail = new PHPMailer();
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXXX#gmail.com';
$mail->Password = 'XXXXXXXXXXXXXXXXXX';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPKeepAlive = true;
$mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
}
$mail->addAddress($reciever, $usertype);
// Attachments
$mail->addAttachment($file); // Add attachments
$mail->isHTML(true);
$mail->Subject = $emailsubject;
$mail->Body = $email_body;
$mail->AltBody = 'NA';
$mail->send();
$mail->clearAddresses();
$mail->clearAttachments();
echo "Mail sent";
}
This has the added benefit of not using a global. Also note the use of clearAttachments, as that works the same way as addresses.
I am trying to send emails to phones that have Verizon numbers. Here is my code right now
<?php require('includes/config.php');
function Send( $ToEmail, $MessageHTML, $MessageTEXT) {
require_once ( 'classes/phpmailer/phpmailer.php' ); // Add the path as appropriate
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "box405.bluehost.com"; // Sets SMTP server
$Mail->SMTPDebug = 2; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "ssl"; //Secure conection
$Mail->Port = 465; // set the SMTP port
$Mail->Username = 'techsupport#test.com'; // SMTP fake account username
$Mail->Password = 'password1'; // SMTP fake account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->FromName = 'Tech support';
$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() ) { // ADDED - This error checking was missing
return FALSE;
}
else {
return TRUE;
}
}
$stmt = $db->prepare("select phone From members where phone not like 'no';");
$stmt->execute();
$result = $stmt->fetchAll();
$ToEmail = $result[0][0];
$ToName = 'techsupport';
$MessageHTML = "test";
$MessageTEXT = 'test';
$Send = Send( $ToEmail, $MessageHTML, $MessageTEXT);
if ( $Send ) {
echo "<h2> Sent OK</h2>";
}
else {
echo "<h2> ERROR</h2>";
}
die;
?>
this works when I try to send emails but when I use it to send texts it says sent but I do not receive anything. I know this is not because of the address because I use the same address when I text myself from gmail and it is not the sql query because I have tested it . I Think the problem is in the smtp or phpmailer for I have not used either of those a lot. Also the code runs though and prints out the SENT OK echo but nothing goes through and no errors.
[EDIT] To answer ironcito's question I am sending the email to
phonenumber#vtext.com
I know this works because I have used the same address through gmail.
I think this might be because you push the content to the mail (body) before you set the content (messagehtml/messagetext), which creates an empty email. Try changing these around, that might be the solution.
I am rather puzzled with this one.
//SMTP servers details
$mail->IsSMTP();
$mail->Host = "mail.hostserver.com";
$mail->SMTPAuth = false;
$mail->Username = $myEmail; // SMTP usr
$mail->Password = "****"; // SMTP pass
$mail->SMTPKeepAlive = true;
$mail->From = $patrickEmail;
$mail->FromName = "***";
$mail->AddAddress($email, $firstName . " " . $lastName);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $client_subject;
$mail->Body = $client_msg;
if($mail->Send())
{
$mail->ClearAllRecipients();
$mail->ClearReplyTos();
$mail->ClearCustomHeaders();
...
$mail->From = "DO_NOT_REPLY#...";
$mail->FromName = "****";
$mail->AddAddress($ToEmail1, "***"); //To: (recipients).
$mail->AddAddress($ToEmail2, "***"); //To: (recipients).
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $notification_subject;
$mail->Body = $notification_msg;
if($mail->Send())
{
...
The first email sends fine. The second one doesn't. What could be the reason for that behavior? Am I missing some kind of reset?
Update: using a different mail server seems to work so apparently it's a setting of that specific mail server causing problems. Any idea what that could be?
Some providers impose restrictions on the number of messages that can be sent within a specific time span. To determine if your problem depends by a provider "rate limit", you should try to add a pause after the first send. For example:
if ($mail->Send()) {
sleep(10); // Seconds
...
if ($mail->Send()) {
...
}
}
Then, by progressively lowering the sleep time, you should be able to determine which is the rate limit.
Try this:
As #Felipe Alameda A mentioned Remove $mail->SMTPKeepAlive = true;
// for every mail
if(!$mail->Send())
{
echo 'There was a problem sending this mail!';
}
else
{
echo 'Mail sent!';
}
$mail->SmtpClose();
IMHO you need to create new PHPMailer object for every sent email. If you want to share some common setup, use something like this:
$mail = new PHPMailer();
/* Configure common settings */
while ($row = mysql_fetch_array ($result)) {
$mail2 = clone $mail;
$mail2->MsgHTML("Dear ".$row["fname"].",<br>".$cbody);
$mail2->AddAddress($row["email"], $row["fname"]);
$mail2->send();
}
I think your problem is $mail->SMTPAuth = false;
It is hard to believe there are ISP or SMTP providers that don't require authentication, even if they are free.
You may try this to check for errors instead of or in addition to checking for send() true:
if ( $mail->IsError() ) { //
echo ERROR;
}
else {
echo NO ERRORS;
}
//Try adding this too, for debugging:
$mail->SMTPDebug = 2; // enables SMTP debug information
Everything else in your code looks fine. We use PHPMailer a lot and never had any problems with it
The key may lie in the parts you have omitted. Is the domain of the sender of both emails the same? Otherwise the SMTP host may see this as a relay attempt. If you have access to the SMTP server logs, check these; they might offer a clue.
Also, check what $mail->ErrorInfo says... it might tell you what the problem is.
i personally would try to make small steps like sending same email.. so just clear recipients and try to send identical email (this code works for me). If this code passes you can continue to adding back your previous lines and debug where it fails
and maybe $mail->ClearCustomHeaders(); doing problems
//SMTP servers details
$mail->IsSMTP();
$mail->Host = "mail.hostserver.com";
$mail->SMTPAuth = false;
$mail->Username = $myEmail; // SMTP usr
$mail->Password = "****"; // SMTP pass
$mail->SMTPKeepAlive = true;
$mail->From = $patrickEmail;
$mail->FromName = "***";
$mail->AddAddress($email, $firstName . " " . $lastName);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $client_subject;
$mail->Body = $client_msg;
// all above is copied
if($mail->Send()) {
sleep(5);
$mail->ClearAllRecipients();
$mail->AddAddress('another#email.com'); //some another email
}
...
Try with the following example.,
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$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->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address1 = "whoto#otherdomain.com";
$address2 = "whoto#otherdomain.com";
$mail->AddAddress($address1, "John Doe");
$mail->AddAddress($address2, "John Peter");
$mail->AddAttachment("images/phpmailer.gif"); // attachment if any
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment if any
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Note : Better you can make a multiple user email and name as an ARRAY, like
<?php
$recipients = array(
'person1#domain.com' => 'Person One',
'person2#domain.com' => 'Person Two',
// ..
);
foreach($recipients as $email => $name)
{
$mail->AddCC($email, $name);
}
(or)
foreach($recipients as $email => $name)
{
$mail->AddAddress($email, $name);
}
?>
i think this may help you to resolve your problem.
I think you've got organizational problems here.
I recommend:
Set your settings (SMTP, user, pass)
Create new email object with info from an array holding messages and to addresses
Send email
Goto step 2
When I'm sending a test message via PHPmailer (SMTP) my email appends to the recepients list. Here is what recipient sees in inbox email
To: mail#mail.com, Name <mail2#mail.com>
The second email is mine. How can I stop this?
Here is my code
function send_email($to, $fromName, $subject, $message, $contentType='text', $smtp_opts) {
$mail = new PHPmailer();
$mail->SetFrom($smtp_opts['fromEmail'], $fromName);
$mail->Subject = $subject;
$mail->Mailer = 'smtp';
$mail->AddAddress($to);
$mail->CharSet = "UTF-8";
$mail->IsHTML($contentType=='html');
$mail->Host = $smtp_opts['host'];
$mail->SMTPAuth = (bool)$smtp_opts['auth'];
if ($mail->SMTPAuth) {
$mail->Username = $smtp_opts['username'];
$mail->Password = $smtp_opts['password'];
}
$mail->Body = $message;
$mail->AddAddress($smtp_opts['fromEmail'], $fromName);
$result = $mail->Send();
$mail->ClearAddresses();
$mail->ClearAttachments();
return $result;
}
$smtp_opts = array( ... ); // host, port, fromEmail, auth, username, password
send_email('mail#mail.com', 'Name', 'Subj', 'Msg', 'html', $smtp_opts);
$mail->AddAddress($smtp_opts['fromEmail'], $fromName);
If I am not mistaken, this command adds another recipient to the list of recipients. Try to remove it and send another test E-Mail. You shouldn't be getting the copy-mail then.
I think the problem is in this line
$mail->AddAddress($smtp_opts['fromEmail'], $fromName);