HalRau Posted January 26, 2012 Share Posted January 26, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/ Share on other sites More sharing options...
scootstah Posted January 26, 2012 Share Posted January 26, 2012 If you separate each email with a comma you can send emails to multiple people. mail('[email protected], [email protected], [email protected]', 'subject', 'body'); Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311456 Share on other sites More sharing options...
HalRau Posted January 26, 2012 Author Share Posted January 26, 2012 I need those e-mail addresses to be pulled from the mysql database. How would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311504 Share on other sites More sharing options...
HalRau Posted January 27, 2012 Author Share Posted January 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311673 Share on other sites More sharing options...
Pikachu2000 Posted January 27, 2012 Share Posted January 27, 2012 Where did that code come from? Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311740 Share on other sites More sharing options...
HalRau Posted January 27, 2012 Author Share Posted January 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311780 Share on other sites More sharing options...
Pikachu2000 Posted January 28, 2012 Share Posted January 28, 2012 Why are you selecting records with an empty email address to begin with, if you only want records with a non-empty email address? Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311856 Share on other sites More sharing options...
HalRau Posted January 28, 2012 Author Share Posted January 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311920 Share on other sites More sharing options...
Pikachu2000 Posted January 28, 2012 Share Posted January 28, 2012 OK, so all the addresses in the database are getting the same email then? In the email, cc and bcc fields, if there is no address, is the field actually set to NULL, or is it just empty? Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311965 Share on other sites More sharing options...
HalRau Posted January 28, 2012 Author Share Posted January 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311969 Share on other sites More sharing options...
lonewolf217 Posted January 28, 2012 Share Posted January 28, 2012 what do you mean when you say "it also sends a mail for cc and bcc" ? Every message always has cc and bcc fields even if you do not populate them with anything. Are additional mails being sent in this case ? Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311970 Share on other sites More sharing options...
Pikachu2000 Posted January 28, 2012 Share Posted January 28, 2012 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1311978 Share on other sites More sharing options...
HalRau Posted January 28, 2012 Author Share Posted January 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1312041 Share on other sites More sharing options...
Pikachu2000 Posted January 28, 2012 Share Posted January 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1312045 Share on other sites More sharing options...
HalRau Posted January 29, 2012 Author Share Posted January 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1312154 Share on other sites More sharing options...
Pikachu2000 Posted January 29, 2012 Share Posted January 29, 2012 I already showed you how to get all the addresses in a single array. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1312172 Share on other sites More sharing options...
HalRau Posted January 29, 2012 Author Share Posted January 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1312306 Share on other sites More sharing options...
HalRau Posted January 30, 2012 Author Share Posted January 30, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1312615 Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2012 Share Posted January 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255836-send-multiple-e-mail-messages-using-mysql-database/#findComment-1312630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.