Jump to content

Sending email from web form


maplins

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.