Jump to content

Recommended Posts

I have had the below code on my local community group site for a couple of months. All it does is get email addresses from the database and sends each a reminder email when I run it with a cron job each day.

 

All was fine. The database now holds over 100 emails, and now each member gets multiple copies of the same message. Today they received 12 each.

 

Is there something I am missing here please? Any advice would be great - thanks very much

 


<?php  mysql_connect("myhost.co.uk.mysql", "myusername", "mypassword") or     die("Could not connect: " . mysql_error()); 
mysql_select_db("mydbname");  

$result = mysql_query("SELECT Email FROM LtUsers WHERE Active = 'Y'");  
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {   sendMail($row[0]); } mysql_free_result($result);  
function sendMail($to){ $subject = 'Please Update Your Daily Availability'; 

$message = "Hello Everyone\n\nMy message to users here
\n\nFurther message here
\n\nAny problems, please contact us at http://www.mysite.co.uk/contact.php
\n\nYou can view your details at http://www.mysite.co.uk
\n\nThank You Very Much"; 

$headers = 'From: info@mysite.co.uk' . "\r\n" .     'Reply-To: info@mysite.co.uk' . "\r\n" .     'X-Mailer: PHP/' . phpversion();  
mail($to, $subject, $message, $headers); }  ?> 

Link to comment
https://forums.phpfreaks.com/topic/241495-email-sending-multiple-copies/
Share on other sites

In the PHP manual it does say that if you're sending a lot of emails, its not that great to use mail(), and gives some pear packages you could use.

 

I've done a bit of googling and can't verify this, but I have a vague memory of seeing this problem previously, and it having something to do with a server timeout or something?  Maybe if you add a 5 second + rest in every 5-10 emails that might help?  Also, just keep in mind some mailservers/webservers have a limit on how many emails you can send in 1 hour to deter spammers - it may pay to check what this limit is for your host.

 

Is you're error reporting on in php_ini?  Seeing any errors?

Sometimes an email server's spam filter will hiccup and cause it to send muti emails, if the server gets overloaded.  You could either talk to your server provider, or you could tell the script to sleep for a couple seconds every so many emails.

I don't really know about this particular error, but I do know that I was pretty recently looking at sending multiple emails at once. When checking out the PHP Manual page for mail() it explicitly says that mail() isn't really intended for more than one email at a time since it opens a new smtp socket for each request. Because of this it can take a couple of minutes just to send 20-30 emails.

 

Regardless of the errors you're currently getting, it's really a much better idea to use Pear's Mail function. Luckily Pear is a PHP Module that's usually already installed by most hosts. And it's really simple to use as well. Here's an example of how I used it to send a text message to a user:

 

require_once 'Mail.php';

function SendTXT()
{
    $mail =& Mail::factory("mail"); // Define $mail as the Pear Mail function
    $CarrierArray = array('@txt.att.net', '@vtext.com', '@tmomail.net', '@messaging.sprintpcs.com', '@message.alltel.com', '@psms.bluesky.as', '@myboostmobile.com', '@cellcom.quiktxt.com', '@mobile.celloneusa.com', '@csouth1.com', '@cwemail.com', '@sms.cvalley.net', '@gocbw.com', '@cingular.com', '@cingulartext.com', '@sms.cleartalk.us', '@sms.mycricket.com', '@echoemail.net', '@mobile.gci.net', '@msg.globalstarusa.com', '@gscsms.com', '@myhelio.com', '@mobile.kajeet.net', '@mymetropcs.com', '@messaging.nextel.com', '@api.panaceamobile.com', '@zsend.com', '@sms.pocket.com', '@qwestmp.com', '@rinasms.com', '@mmst5.tracfone.com', '@email.uscc.net', '@utext.com', '@viaerosms.com', '@vmobl.com');

    // Set CarrierArray with all possible carrier options so user receives txt regardless of carrier

    $GetNumber = mysql_query("SELECT Object FROM $Database->tbl_Users WHERE id = '$this->Recipient'");
    $TempObject = mysql_result($GetNumber, 0, "Object");
    $TempObject = unserialize($TempObject);

    $Permission = $TempObject->GetValue(TextPermission);
    $Number = $TempObject->GetValue(CellPhone);
    $NumberArray = array();
    
    $headers = array("From"=>"Website Name <notifications@randomaddress.com>");

    if($Permission != 1)
    {
    	return "This user does not allow text messages.";
    }
    if($Number == "")
    {
    	return "Error, User does not have cell phone number and cannot receive texts.";
    }

    $Length = count($CarrierArray); // Find number of message recipients
    for($x = 0; $x < $Length; $x++) // Loop through recipients
    {
$NumberArray[$x] = $Number . $CarrierArray[$x]; // Create email address using cell number plus carrier info
echo $NumberArray[$x] . "<br>"; // Display current email address
    }

    $mail->send($NumberArray, $headers, $this->Message); // Send message to multiple recipients.
}

 

The Pear mail function gives you the ability to send your message/email to an array full of addresses. Assuming that you don't need to customize the message per user, then this should be a great solution for you.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.