Jump to content

Recommended Posts

The form sends email to all users without problems but duplicates the "From" header - if you're 4th in the database, you get the header 4 times, 5th gets it 5 times etc. Here is the code from the handling script. Can anyone suggest how to stop this happening?

 

//set variables from form
$form_subject=$_POST['subject'];
$content=$_POST['content'];

// pull data from the database
$results=mysql_query("select * from mailing_list");
while($row=mysql_fetch_array($results))
{

//send email
$mailto=$row['email'];
$subject="$form_subject";
$headers.="From: xxxx <xxxx@xxxx>\r\n";
$headers.="Reply-To: xxxx@xxxx\r\n";
$headers.="MIME-Version: 1.0\r\n";
$headers.="Content-type: text/plain; charset=iso-8859-1\r\n";
$message="$content" ;


mail($mailto, $subject, $message, $headers)
or die("Error sending email");

}

Link to comment
https://forums.phpfreaks.com/topic/201354-sending-email-from-web-form/
Share on other sites

you could just set it up to BCC everyone all at once instead of bogging down the server with hundreds of e-mail requests.

 

http://php.net/manual/en/function.mail.php

 

Example #4 would work well, you would only have to modify the BCC header to place everyone's e-mail in a line separated via a comma.

 

$headers .= 'Bcc: <[email protected]>, <[email protected]>, <[email protected]>' . "\r\n";

 

this way your server gets 1 e-mail request.

Pull the addresses from the database, saving them in a temporary array, then create/send the email:

 

<?php
//set variables from form
$subject=$_POST['subject'];
$message=$_POST['content'];
$bcc = array();
// pull data from the database
$results=mysql_query("select email from mailing_list");
while($row=mysql_fetch_array($results))
{
$bcc[] = $row['email'];
         }
$headers = array();
$headers[] = "From: xxxx <xxxx@xxxx>";
$headers[] = "Reply-To: xxxx@xxxx";
$headers[] = "Bcc: " . implode(', ',$bcc); //put the collected email addresses in the BCC line
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
mail('[email protected]', $subject, $message, implode("\r\n",$headers))
or die("Error sending email");
?>

 

Ken

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.