Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to alter the 'To' header of a multiple recipient email??

Status
Not open for further replies.

tippytarka

Programmer
Joined
Jul 19, 2004
Messages
115
Location
GB
i've got this little email script that sends an html email to multiple recipients...

Code:
<?
 // read list of emails from file.
 $email_list = file("elist.txt");

 // loop through email list
 for ($i = 0; $i < count($email_list); $i++) {
 $list_loop = $email_list[$i];
 
 $to .= ''.$list_loop.',';
 }
 
 
 $subject = "My email test.";
 $message = "Hello, how are you?";

 // Content-type header
 $headers  = "MIME-Version: 1.0\r\n";
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

 // additional headers
 $headers .= "From: xxx <xxx@xxx.com>\r\n";

 mail($to,$subject,$message,$headers)
 
?>

only thing, when the email is received, the 'To' header contains all the emails in elist.txt, like so....

To: paul@test.com, ann@test.com, mark@test.com, henry@test.com

i would like the 'To' header to either contain the single email address of just the recipient only ....or remain blank or be replaced with something else like customer ...any suggestions???

cheers!
 
That's how email works. All the addresses you put on the "To:" line show up. What you are looking for is the "BCC:" header. I believe you can format it like "Bcc: addr1,addr2,..." and append it to the "$headers" variable.

BTW, instead of using a "for" loop to set your recipient list, you can use the "implode" function.

Code:
 // read list of emails from file.
$email_list = file("elist.txt");

$bcc = 'Bcc: ' . implode(',',$email_list) . "\r\n";

See the PHP Manual entry on mail for more information.
 
hi kenrbnsn, am i apending the variable $bcc to the "Bcc:" header correctly????

Code:
// additional headers
$headers .= "From: me <me@test.com>\r\n";
$headers .= "Bcc: $bcc\r\n";

mail($to, $subject, $message, $headers);
 
If you set up the $bcc variable like in my example, it would be done like this:
Code:
// additional headers
$headers .= "From: me <me@test.com>\r\n";
$headers .= $bcc;
since I included the 'Bcc:' and the '\r\n' in the variable value already.

Ken
 
ok, i'm not sure why its not working? here is my code...

Code:
<?
// read list of emails from file.
$email_list = file("elist.txt");

$bcc = 'Bcc: ' . implode(',',$email_list) . "\r\n";

$to = "me@test.com";
 
$subject = "My email test.";
$message = "Hello, how are you?";

// Content-type header
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

// additional headers
$headers .= "From: me <me@test.com>\r\n";
$headers .= $bcc;

mail($to, $subject, $message, $headers);
?>

the email is delivered to the address asigned to the '$to' variable but not to the multiple addresses asigned to '$bcc'

if i echo the $bcc variable to the browser i get the following so i know that its working...

Bcc: paul@test.com ,ann@test.com ,mark@test.com

whats also unusual, when i check the html email sent to '$to'.... ,ann@test.com ,mark@test.com appears as the first line in the message area, and nowhere in my script am i printing/echoing this line???
 
Try to reverse the order of the headers:
Code:
$headers = "From: me <me@test.com>\r\n";
$headers .= $bcc;

// Content-type header
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

Also, take a look at this article.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top