Jump to content

Email Script - Names and Email Addresses


michael.davis

Recommended Posts

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 = "bubba@bubba.com";

$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|bubba@bubba.com

Oohay|oohay@yahoo.com

 

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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('Address1@somedomain.com', 'Address2@otherdomain.com', 'Address3@somedomain.net', 'Address4@somedomain.com');

/* 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: WebSite@mydomain.com\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.

 

 

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.