Sending Mass Mail is too slow via PHP mail() function - php

I am trying to send mass mail via PHP mail() function but it is too slow. All the emails are fetched from database. Currently, I am performing this with Xampp (offline) with Sendmail. And right now in my database there are only 6 emails and it is taking 2 mins to complete sending all the mails. I am just worried about what what will happen when in production mode there will be thousands of users. A sample code is given below about what I am using. Is there any way of making it execute faster?
$from = (!empty($_POST['from']))?$_POST['from']:null;
$type = (!empty($_POST['type']))?$_POST['type']:null;
$to = (!empty($_POST['to']))?$_POST['to']:null;
$subject = (!empty($_POST['subject']))?$_POST['subject']:null;
$message = (!empty($_POST['message']))?$_POST['message']:null;
if($_POST){
$emails = $pdo->prepare("SELECT mem_email FROM members");
$emails-> execute();
$adminMail = $pdo->prepare("SELECT set_site_name, set_admin_support_mail FROM settings");
$adminMail-> execute();
$amf = $adminMail->fetch();
$headers = "From: ".$from." (".$amf['set_admin_support_mail'].")";
while($u = $emails->fetch()){
$sendTo = $u['mem_email']; // note the comma
mail($sendTo, $subject, $message, $headers);
}
}

Related

Send email from email list from database using php, mysql [duplicate]

This question already has answers here:
PHP send mail to multiple email addresses
(13 answers)
Closed 3 years ago.
I am trying to send email to all the members email id's which is stored in the database. Here is the code
<?php
$sql = mysqli_query($con,"select email from email_list where email");
//$recipients = array();
while($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)) {
$recipients[]= $row['email'];
}
$to = $recipients;
$repEmail = 'abc#gmail.com';
$subject = "$title";
$body = "$description";
$headers = 'From: xyz <'.$repEmail.'>'. "\r\n";
$headers .= 'Reply-To: abc#gmail.com' . "\r\n";
$headers .= 'BCC: ' . implode(', ', $recipients) . "\r\n";
if (mail($to, $subject, $body, $headers)){
echo "<script>alert('Email sent')</script>";
}
else {
echo "<script>alert('Email failed')</script>";
}
?>
In this above code email goes to 1 person.
When you look at the pho docs : mail() function
to
Receiver, or receivers of the mail. The formatting of this string
must comply with » RFC 2822.
Some examples are:
user#example.com
user#example.com, anotheruser#example.com
User
User , Another User
It clearly states "to" part should be in rfc2822 compliance. You are passing an array which is not acceptable and thus only first value is taken.
Also it's worth mentioning that:
Note: It is worth noting that the mail() function is not suitable for
larger volumes of email in a loop. This function opens and closes an
SMTP socket for each email, which is not very efficient. For the
sending of large amounts of email, see the » PEAR::Mail, and
» PEAR::Mail_Queue packages
.
Thus I'd suggest you to use some mailing library such as phpMailer or swiftmailer for better functionality or better use mail queue.
Your $to should be $to = "address#one.com, address#two.com, address#three.com"
So add
$recipients = array(
"youremailaddress#yourdomain.com",
// more emails here
);
$email_to = implode(',', $recipients); // your email address
add $email_to to your $to.

auto reply not working only for gmail accounts using php mail()

I have a contact form on my website which sends an email to my account and an auto-response to the users who fills the form. I could able to send an auto-reply to non-Gmail accounts but not to Gmail accounts, it's not even sent to spam. I want to know is anything missing in the code, or any settings have to be changed, let me know
code is working fine with non-Gmail accounts
<?php
$email_to = 'mailme#example.com'; //your email
$business = 'company name.,'; //business name
//$topic = $_POST['topic'];
$name = $_POST['name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$autoResponse = true; //set to false if you don't want to send an auto reply
$autoResponseSubject = "Demo Contact Form";
$autoResponseMessage = "Hi, thank you for contacting us, we will get back to you soon.";
$autoResponseHeaders = "From: $business <$email_to>\r\n";
$autoResponseHeaders .= "Reply-To: $business <$email_to>\r\n";
$headers = "From: $name <$email_from>\r\n";
$headers .= "Reply-To: $name <$email_from>\r\n";
if(#mail($email_to,$subject, $message, $headers)){
if($autoResponse === true){
mail($email_from, $autoResponseSubject, $autoResponseMessage, $autoResponseHeaders);
}
echo '1';
} else {
echo '0';
}
?>
I am not getting any errors.
Google, Microsoft, and the like, only accept email from mail servers that fulfill a number of requirements. These requirements are changing over time. This has mainly to do with preventing spam.
Things start with SPF, which is rather simple, but the normal site providing documentation has been down since feb 2019. Have a look at Wikipedia instead.
The next thing is DKIM. Without it mail certainly won't been accepted by GMail.
Then there is also DMARC.
After all of this there is still no guarantee that your mail will be accepted. Your IP could be blacklisted.
As you can probably guess by now, running your own mail server is a lot of work. I've stopped doing it years ago. I now use a third party service for this.

how to send mails to multiple list of emails from a database using php

i am working on script to send a mail to a list of emails from my table using php
presently i run a query on the table and echo all the emails meeting such requirements from the sql query
sql="SELECT * FROM people WHERE status in('member','client')";
$result = mysql_query($sql)or die("Cannot query orders_products data" . mysql_error());
while($row=mysql_fetch_array($result)) {
$list = array();
$list[] = $row['Email'];
echo implode(",",$list);
$email = 'xxxxxxxxxxx';
$emailto = implode( "," ,$list );
$subject = "xxxxxxxxxxxxxxxx";
$headers = "From: $email";
$body .= ++$i.") ".$row['title'] ."\n\n";
}
$body .= "Regards\n\n";
$body .= "xxxxxxxxxxxxxxxxxx\n\n";
$send = mail($emailto, $subject, $body, $headers);
when i try passing these emails to a mail function snippet the mail sending fails.
Please what could i be doin wrong, i need help
Update :
$list[] = $row['Email'];
echo implode(",",$list);
all the emails are displayed
But without commas.
$emailto = implode( "," ,$list );
i use the same method of imploding the array of data gotten from
$list[] = $row['Email'];
By fail i just mean i was hoping since the emails got echoed using the implode it would all successfully pass the emails to $emailto and then send a mail to each of them.
So, what I think you're trying to do is to get the email address from your table where the user is either a member or a client. Then, you have a from address, subject, and a body. In addition, it looks like maybe you're trying to add to the body some unique information. Finally, I think you want to send an individual email to each user, separately.
You could modify your code to be like this:
<?php
$sql="SELECT * FROM people WHERE status in('member','client')";
$result = mysql_query($sql) or die("Cannot query orders_products data" . mysql_error());
$emailContent = 'xxxxxxxxxxx';
$emailSubject = 'xxxxxxxxxxx';
$body = 'xxxxxxxxxxxxxxx';
$headers = "From: from#email.com";
while ($row = mysql_fetch_array($result)) {
// optionally modify body here...?
$emailBody = $body .= $row['title']; // or whatever you're trying to do here...
// send email to each individual person
mail($row['Email'], $emailSubject, $emailBody, $headers);
}
I think this is how you'd modify your code to get it to do what you're asking.

PHP mail() BCC - display only the end receivers address in To: header

I'm attempting to BCC to a list of subscribers from a database, using PHP mail(). Everything works, but I'm running into a problem that has troubled me all morning. I'm able to send the list with BCC, but are unable to append the receiving end email address to the deader "To:".
For example, I send the list to the following email addresses (test1#example.com, test2#example.com, and test3#example.com). Each email address receives an email and the other email addresses are hidden because of BCC.
My problem is that in the header, "To:" is displayed blank on all of the receiving ends of the list. That I understand and know that header won't display because I have only the BCC header within the outgoing message. I've attemted to for loop the process but I receive all of the emails, plus one for that address in one loop.
Here is my working code: The working code includes the loop that I attempted for solving the To: header. I'm able to send the email, though I receive all of the emails that were sent.
<?php
/*
Gathers the number of rows within the database. Used for the loop that displays the entire list in the BCC.
*/
session_start();
include_once '../dbconnect.php';
$result = mysql_query("SELECT * FROM news");
$num_rows = mysql_num_rows($result);
$rows = $num_rows;
/*
Requests the list from the database, please the list in a loop, displays the list only once, and places list in an operator.
*/
$result = mysql_query("SELECT * FROM `news`");
while($row = mysql_fetch_array( $result )) {
for ($x = 1; $x <= 1; $x++) {
$contacts.= "".$row['email'].",";
}
}
/*
ATTEMPT (It works... sort of): Sends mail to the list using BCC, displays the ONLY receivers email address in the To: header
*/
$result = mysql_query("SELECT * FROM `news`");
while($row = mysql_fetch_array( $result )) {
for ($x = 1; $x <= 1; $x++) {
$receiver= "".$row['email']."";
$to = "$receiver";
$subject = 'Test Email';
$headers = "From: example#example.com\r\n";
$headers .= "BCC: $contacts";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1 style="">Test Message</h1>';
$message .= '</body></html>';
mail($to,$subject, $message, $headers);
}
}
?>
My general thought was to loop, but I can't find a solution that actually solves this completely, by BBC to the list and displaying only the receiving ends email address in the To: header. Any thoughts or ideas?
Update with SMTP Server
I've been attempting to use the solution found in this thread and apply it to an SMTP server. Send the message using SendGrid was the ideal choice. I've come up with the below option, but the script only seems to send one message to one of the addresses in the database and not all the address.
<?php
require_once "Mail.php";
$sub = $_POST['subject'];
$ttl = $_POST['title'];
$img = $_POST['image'];
$bdy = $_POST['body'];
$lk = $_POST['link'];
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("") or die(mysql_error()) ;
$result = mysql_query("SELECT `email` FROM news");
$num_rows = mysql_num_rows($result);
$receivers = array();
while ($row = mysql_fetch_array($result)) {
$receivers[] = $row['email'];
}
foreach ($receivers as $receiver) { }
$from = "test#example.com";
$to = $receiver;
$subject = $sub;
$mime = "1.0";
$ctype = "text/html; charset=ISO-8859-1";
$body = '
<html><body>
<p>Test Message!</p>
</body></html>
';
$host = "";
$port = "";
$username = "";
$password = "";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => $mime ,
'Content-Type:' => $ctype);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
The code includes some general improvements to your code. I have added inline comments to explain what I have done.
<?php
// General thing: If you do not need a session, do not start one ;)
session_start();
include_once '../dbconnect.php';
// Select only what you really need from the table. This saves you memory
// and it speeds up the query.
$result = mysql_query("SELECT `email` FROM news");
// You are not using these numbers in the script you showed us. I am just
// leaving them in here to show you, how you can reuse the "$result"
// variable without querying the database again.
$num_rows = mysql_num_rows($result);
// We are reusing the "$result" here without re-querying the database, which
// speeds the whole process up and takes load away from the database. We are
// storing all receivers in a dedicated variable, to reuse them later on.
$receivers = array();
while ($row = mysql_fetch_array($result)) {
$receivers[] = $row['email'];
}
// Now, instead of querying the database again, we are using our stored mail
// addresses in "$receivers" to send the emails one by one. We could have
// done this part in the "while" loop before, but I wanted to stay close to
// your code, so you would recognize it ;)
foreach ($receivers as $receiver) {
// I have removed the "for" loop here, because it runs only once. If a loop
// only runs once and you control all its input, you really do not need a
// loop at all (except some exceptions, just in case someone knows one^^).
// You can actually just put the value of $receiver in $to. PHP is pretty
// good at typecasting of scalar types (integers, strings etc.), so you do
// not need to worry about that.
$to = $receiver;
$subject = 'Test Email';
// I am putting the headers into an array and implode them later on. This
// way we can make sure that we are not forgetting the new line characters
// somewhere.
$headers = array(
"From: example#example.com",
"MIME-Version: 1.0",
"Content-Type: text/html; charset=ISO-8859-1",
// I have removed the "BCC" header here, because this loops send out an
// email to each user seperately. It is basically me sending an email to
// you. Afterwards I copy&paste all the content to another new mail and
// send it to another fella. You would never know that you both got the
// same mail ;)
);
$message = '<html><body>';
$message .= '<h1 style="">Test Message</h1>';
$message .= '</body></html>';
// Imploding the headers.
$imploded_headers = implode("\r\n", $headers);
mail($to, $subject, $message, $imploded_headers);
}
This code basically send out one email at a time. No one who receives such an email knows which email addresses the email went to.
As mentioned in the code, this snippet can be further optimized. Since email sending itself is a pretty difficult area, I would really suggest that you find some PHP library that bundles that whole thing and work with it. You can make so many mistakes with the whole email sending thing, that I would not run a script like that in production if you do not want to get marked as spam soon after.
Add \r\n to:
$headers .= "BCC: $contacts\r\n";
Each header must be on new line.

Sending multiple CC's and BCCs with PHP PEAR MAIL

I have a project that I am working on at my job and I am using Pear's mailing. I need to use smtp because we need to be able to track everything from our mailserver. And users need to be able to log in before sending a company based email. We cannot use php's mail function fo this.
My problem is that I cant find any documentation on the net for sending CC and Bcc as well as sending multiple BCCs. It is very easy to do with php' mail funciton . All you do is add it to the $header variable like so
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
This is my code for the php function where I use PEAR
function sender_mail($email,$subject,$mailmsg, $cc, $bcc){
include("Mail.php");
/* mail setup recipients, subject etc */
//DEFAULT VALUE OF FROM
$from = "noreply#addata.net";
//GET EMAIL OF USER
$result = mysql_query("SELECT email, email_pass FROM u_perinfo WHERE user_id = '$_SESSION[uid]'")
or die("There was an error when grabbing your email information");
if(mysql_num_rows($result) > 0){
$row = mysql_fetch_array($result);
if($row[0] != ''){
$from = $row[0];
}
$email_pass = $row[1];
}
$recipients = "$email";
$headers["From"] = "$from";
$headers["To"] = "$email";
$headers["Subject"] = $subject;
$headers["Cc"] = "$cc"; //Line added by Me to see if it works
$headers["Bcc"] = "$bcc"; //Line added by Me to see if it works
//$mailmsg = "Welcome to Addatareference.com! \r\n\r\nBelow is your unique login information. \r\n\r\n(Please do not share your login information.)$accountinfo";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.emailsrvr.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "$from";
$smtpinfo["password"] = "$email_pass";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);
}
I have been trying to find a solution to this with no real info coming back my way. If someone could help me out with this I would be greatly appreciated.
There is a problem with Bcc though when using the PEAR Mail function: Thunderbird displays the Bcc header, so the recipient is not hidden, which is the whole point of Bcc. It is also displayed in the To: header list (since you have to include the Bcc list in recipients).
Edit: SOLVED - I found from another site that the solution is just to include the Bcc list in the $recipients, and not in any of the headers. It works!
Thus:
$bcc = "foo#example.com";
$to = "bar#example.com";
$headers = array(..other stuff.., 'To' => $to, ..rest of header stuff); // No Bcc header!
$recipients = $to.", ".$bcc;
$mail = $smtp->send($recipients, $headers, $message);
Edit #2: Acknowledging my source - http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/
Have you tried to add multiple addresses "," separated?
$headers['Cc'] = 'cc#example.com, bb#example.com, dd#ex.com';
This might work, according to line 218 in the source.
You just need to add all to, cc, bcc in $recipients variable. Header decides where to send.

Categories