Jump to content

send multiple e-mail messages using MySql database


HalRau

Recommended Posts

I am trying to send an e-mail message from a form, to each person in a MySQL database. In some cases, but not all, there are multiple e-mail address in the recordset. Each one should receive the same message addressed to them by name (FirstName and LastName) and e-mail address.  I have tried "CONCAT_WS(' ', emailtest.Pri_EmailAddress, emailtest.Sec_EmailAddress, emailtest.Tri_EmailAddress) AS EmailTo" but it seems the relay account is rejecting the message if more than one address is in the EmailTo result.

Any ideas or scripts that can do this would be appreciated.

Thank you in advance for your help.

Link to comment
Share on other sites

Hi, This php newbie needs some help.

I have a send e-mail form that is sort of working however it sends multiple copies of the e-mail to all addresses in the database. There are three records in my test database. Here is the code I am using:

 $sm1->total_mail = $totalRows_RSemail;
  do {
    $sm1->reset();
   $sm1->setTo($row_RSemail['BusinessName'], $row_RSemail['Pri_EmailAddress']);
 if ( $row_RSemail['Sec_EmailAddress'] != NULL ) {
echo $sm1->setCc($row_RSemail['BusinessName'], $row_RSemail['Sec_EmailAddress']);
}
 if ( $row_RSemail['Tri_EmailAddress'] != NULL ) {
echo $sm1->setBcc($row_RSemail['BusinessName'], $row_RSemail['Tri_EmailAddress']);
}	
    $sm1->setSubject("".$_POST['emailsubject']."");
    // using static for body
    $sm1->setBody_Static_html("".$_POST['emailmessage']."");
    // Attachments from folder
    $sm1->setAttFolder("attachments");
    $sm1->sendMail("multiple");
  } while ($row_RSemail = mysql_fetch_assoc($RSemail));
  $sm1->done();
  $GoTo = "mailsent.php";
  if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  $GoTo .= (strpos($GoTo, '?')) ? "&" : "?";
  $GoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
  }

The way I would like it to work is this. Some of the Sec_EmailAddress and Tri_EmailAddress fields are blank (null) in the database. Lf the script encounters a null in that field it skips over that field and does not send a Cc or Bcc. If the field is not null, it sends the message to that Cc or Bcc address.

Thank you for your help.

Link to comment
Share on other sites

The basic code came from SmartMailer for php. It works fine but I'm trying to add the Cc and Bcc addresses to it.

The code I have added is this.

	 if ( $row_RSemail['Sec_EmailAddress'] != NULL ) {
echo $sm1->setCc($row_RSemail['BusinessName'], $row_RSemail['Sec_EmailAddress']);
}
 if ( $row_RSemail['Tri_EmailAddress'] != NULL ) {
echo $sm1->setBcc($row_RSemail['BusinessName'], $row_RSemail['Tri_EmailAddress']);
}	

I think the problem is in the "if" and echo statements. I only want to send a Cc or Bcc if the field is populated in the database, otherwise skip over it.

Hope that makes sense.

Link to comment
Share on other sites

Well, that might be the problem. I have used this if statement elsewhere on the site and it works there. Example:

 <?php
if ( $row_RSwbamembers['MapLocation'] != NULL ) {
echo "<a href=\"{$row_RSwbamembers['MapLocation']}\" target=_blank>Map</a> <br />";
}
?>    

 

In this case the To and Cc and Bcc addresses, if they exist, are all three in the same record. I want to send the same e-mail message to them as well. So, I tried "hacking" the working code into the script that sends the mail.

Bottom line, I only want to populate the Cc and Bcc lines if they have data, otherwise skip over them.

I am sure there is a better way to do this. Any ideas? Thank you for your help on this.

Link to comment
Share on other sites

Thanks for your help.

Yes, the same e-mail goes to all addresses. The database field for Cc and Bcc are empty (no data). They show as NULL in the the database management tool by Starfield. I can e-mail you a printout of the database table I am using for test if that will help.

 

Right now the script sort of works, but in a weird way. It sends the To message ok but also sends a Cc  and Bcc even though those fields are empty for that record. I tried removing the "echo" from the code and it still does the same thing.

Link to comment
Share on other sites

If they show as NULL then they are NULL, not empty. Empty and NULL are no the same thing, but that's a topic for a different discussion . . .

 

I think this is one of the extremely rare occasions that it would be acceptable to run a loop with a db query in it, since it's only three queries in total; one for each field. I suspect there's another way, but this is concise, and builds an array containing only the data you want. Then you can use the resulting array to send the emails.

 

$addresses = array(); // Initialize an empty array to hold the addresses
$fields = array('email', 'cc', 'bcc'); // EDIT TO REFLECT THE FIELD NAMES IN YOUR DB

foreach( $fields as $v ) {
     $query = "SELECT $v FROM your_table WHERE $v IS NOT NULL"; // EDIT your_table TO YOUR TABLE NAME
     $result = mysql_query($query);
     while( $array = mysql_fetch_row($result) ) {
          $addresses[] = $array[0];
     }
}

// show array contents
echo '<pre>';
print_r($addresses);
echo '</pre>';

Link to comment
Share on other sites

Question, will your array put all three addresses into a string? If so, the relay server at the host will ignore all but the first. For instance I tried this

$query_RSemail = "SELECT *, CONCAT_WS(' ', emailtest.Pri_EmailAddress, emailtest.Sec_EmailAddress, emailtest.Tri_EmailAddress) AS EmailTo FROM emailtest WHERE LiveOnWeb = 1";

and it worked but not when I switched to the smtp relay server which I need to do to send messages to the organizations 100 plus member.

It will let the Cc and bcc addresses go through however. That's why I was trying to do it that way.

Link to comment
Share on other sites

When you send a mass email, you should send each one individually, OR send it to the server with the To: header set to a dummy email address, and all the other addresses in the Bcc: header. Otherwise, clicking 'Reply All' in the mail client reveals all of the address to which the email was sent.

 

But truly, the best way to handle large quantities of emails is to use a third party service, IMHO.

Link to comment
Share on other sites

I understand. In this case however both the Cc and the Bcc are part of the same business so it is fine.  Most of the records only have a Primary e-mail address. A few have a Secondary e-mail address populated and even less have the Third e-mail address. Since the database table containing the e-mail addresses also contains all other information about that business including the type of business, location, etc as well as membership information, in this case I think it is best to do everything from the one central database.

Is there a way we can make it work using the code

   $sm1->setTo($row_RSemail['BusinessName'], $row_RSemail['Pri_EmailAddress']);
 if ( $row_RSemail['Sec_EmailAddress'] != NULL ) {
 $sm1->setCc($row_RSemail['BusinessName'], $row_RSemail['Sec_EmailAddress']);
}
 if ( $row_RSemail['Tri_EmailAddress'] != NULL ) {
 $sm1->setBcc($row_RSemail['BusinessName'], $row_RSemail['Tri_EmailAddress']);
}	
    $sm1->setSubject("".$_POST['emailsubject']."");

Or something similar?

Here is a link to the site so you can see what I mean, http://www.wildwoodba.org/

 

Thank you for your help and your continued interest in this issue.

Link to comment
Share on other sites

Using an array, would the result be that all the addresses be in a string like To: 'Pri_EmailAddress', 'Sec_EmailAddress', 'Tri_EmailAddress' ?

Or, would they be separated into To: 'Pri_EmailAddress', Cc: 'Sec_EmailAddress',  and Bcc:'Tri_EmailAddress'?

They cannot be in a string after the To because the relay mail server will strip off the second and third addresses.

Also, keep in mind I am a newbie as this and could you be more specific in how to setup the code in the array?

 

Thank you again for your help.

Link to comment
Share on other sites

I am not sure we can get this script to work the way I want. There is additional code in a support file that seems to be messing things up. The originators of the script won't modify it.

Is there suggestion of a script that might work? I need to send e-mail messages to each person in the data base. There cannot be multiple address in the To field or the relay server will strip them. Bcc and Cc work but need to be sent only when those fields are not empty. Otherwise skip over them.

The e-mail message can be text or html and could contain attachments.

 

any ideas?

Link to comment
Share on other sites

Using the query I posted, the data from each address field in your database would be in its own array element. As I already stated, you don't want to put an actual email address in the To: header anyhow. You use a dummy address in the To: header. All of the addresses to which you're sending a mass email go in the Bcc: header to prevent the recipients from accessing the entire list of all the addresses to which the email was sent. I don't think I can simplify it any further, except to say that you should consider outsourcing mass email sends to a reputable company that does that type of thing.

Link to comment
Share on other sites

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.