michael.davis Posted April 13, 2011 Share Posted April 13, 2011 Hi Everyone! Thanks in advance for taking the time to review my post. I am pretty new to PHP so please excuse my ignorance. I am working a project for myself. I want to be able to send out newsletters to my customers and sponsors. I have written the code and here it is: <?php $subject = $_POST['subject']; $message = $_POST['message']; $email = "[email protected]"; $headers = "From: $email"; $email_list = file("elist.txt"); $total_emails = count($email_list); for ($counter=0; $counter<$total_emails; $counter++) { $email_list[$counter] = trim($email_list[$counter]); } $to = implode(",",$email_list); if ( mail($to,$subject,$message,$headers) ) { echo "The email has been sent!"; } else { echo "The email has failed!"; } ?> ----------------------------------------------------------------------------- In my elist.txt file - I have two fields. Name and email. Bubba|[email protected] Oohay|[email protected] If i leave the names out of this file, the email script works great! I want to leave the names in there and reference them in my email that I am sending out to be more personal. Can this be done and can you help? Thanks! Mike Quote Link to comment https://forums.phpfreaks.com/topic/233610-email-script-names-and-email-addresses/ Share on other sites More sharing options...
DavidAM Posted April 13, 2011 Share Posted April 13, 2011 First, you are putting all of the email addresses into the TO field of a single email message. This might work, depending on your mail server. However, this means that ALL of your customers and sponsors will have EVERYBODY'S email address. I don't know if you want this or not but IF I was a customer, I would NOT want someone sending my email address to ALL of their customers. Now, on to the problem. You stated that there are two fields in the text file, but your code is treating each line as if it was a single field. You need to split the name and email apart and use each field for its intended purpose: // Example Code, this probably will not work as written $email_list = file("elist.txt"); foreach ($email_list as $line) { list($name, $email) = explode('|', trim($line)); $message = 'Hello, ' . $name; mail($email,$subject,$message,$headers) } Of course, if you want to do it in a single call to mail(), and your mail server will handle however many addresses you provide, you can do it. BUT I would recommend NOT putting them in the TO field; use the BCC header, instead. Quote Link to comment https://forums.phpfreaks.com/topic/233610-email-script-names-and-email-addresses/#findComment-1201177 Share on other sites More sharing options...
michael.davis Posted April 13, 2011 Author Share Posted April 13, 2011 I understand your point. That makes a ton of sense. Did not realize that my code would show all the email addresses like that. I wonder how to ensure that they are BCC instead. Hmmmm Thanks for the help on the code. That looks like that will work! Quote Link to comment https://forums.phpfreaks.com/topic/233610-email-script-names-and-email-addresses/#findComment-1201181 Share on other sites More sharing options...
michael.davis Posted April 13, 2011 Author Share Posted April 13, 2011 So you are thinking more like this when setting my header variable? $headers = "From: [email protected]"; $headers .= "\r\nCc: [email protected]"; $headers .= "\r\nBcc: [email protected]\r\n\r\n"; Will this ensure the email addresses will be in the BCC form? Quote Link to comment https://forums.phpfreaks.com/topic/233610-email-script-names-and-email-addresses/#findComment-1201187 Share on other sites More sharing options...
DavidAM Posted April 15, 2011 Share Posted April 15, 2011 You could use the CC header or the TO address if you want a copy to go to your mailbox AND you don't mind everyone getting that address. The CC values are shown to everyone as well as the TO. You can use something like this to set the BCC header: $emailAddr = array('[email protected]', '[email protected]', '[email protected]', '[email protected]'); /* Leave this blank or put your own address in here EVERYONE WHO RECEIVES THIS MESSAGE WILL SEE THIS ADDRESS */ $msgTo = ''; $msgSubj = 'Hello World'; $msgBody = 'This is a test message with BCC'; $msgHdrs = "From: [email protected]\r\n"; $msgHdrs .= 'Bcc: ' . implode(', ', $emailAddr) . "\r\n\r\n"; if (mail($msgTo, $msgSubj, $msgBody, $msgHdrs)) { echo 'Mail Sent'; } else { echo 'Mail Failed'; } I have no idea if there is a limit on the number of addresses or length of the header. I imagine that at some point, the mail program is going to balk if there are too many addresses there. Quote Link to comment https://forums.phpfreaks.com/topic/233610-email-script-names-and-email-addresses/#findComment-1201843 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.