[thelist] PHP mail with multiple recipients and form data not working
Simon Willison
cs1spw at bath.ac.uk
Wed Sep 17 09:33:22 CDT 2003
Joel D Canfield wrote:
> Can anyone see why this isn't working? No mail, blank response page.
I've no idea why it isn't working I'm afraid - at a guess, I'd check
that the ini_set command isn't causing it to die horribly for some
mysterious reason.
However, I do have a suggestion for how you can halve the length of the
script and make it much easier to maintain (for adding fields in the
future for example):
<?php
$today = date("F j, Y, g:i a");
$email = "email at example.com";
$subject = "Website Download ($today)";
// Array of form-name/human-readable-description pairs
$fields = array(
'fname' => 'First Name',
'lname' => 'Last Name',
'coinst' => 'Co/Institution',
'address1' => 'Address 1',
'address2' => 'Address 2',
'city' => 'City',
'zip' => 'Zip Code',
'phone' => 'Phone',
'email' => 'E-Mail',
'howfound' => 'How Found',
'searchengine' => 'Search Engine',
'keywords' => 'Keywrods Used',
'magenta' => 'Magenta Demo',
'magentaoff' => 'Magenta Office Demo',
'remclassic' => 'Remark Classic',
'remoffice' => 'Remark Office',
'remeweb' => 'Remark Web',
'demomanual' => 'Demo Manual',
'magoffinstructions' => 'Mag Office instructions',
'remarklit' => 'Remark Lit',
'comments' => 'Comments'
);
$body = '';
foreach ($fields as $name => $description) {
$body .= $description.': '.$_POST[$name]."\r\n";
}
$headers = "From: webmaster at example.com\n";
$headers .= "Reply-to: webmaster at example.com\n";
$headers .= "CC: email at yahoo.com\n";
$headers .= "Bcc: joel at spinhead.com\n";
$headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
mail ($email, $subject, $body, $headers) or die("Could not send mail");
?>
The $fields array means that instead of needing to create 22 variables
for all the different bits of form data, you can just define a single
array and have a loop do all the work of putting together the $body
variable for you.
Hope that helps,
Simon Willison
>
> Of course, I'm using real e-mail addresses and domain names ;)
>
> Thanks ever so much
>
> spinhead
>
> ====================================================================
> <?php
> $today = date("F j, Y, g:i a");
>
> $fname = $_POST['fname'];
> $lname = $_POST['lname'];
> $coinst = $_POST['coinst'];
> $address1 = $_POST['address1'];
> $address2 = $_POST['address2'];
> $city = $_POST['city'];
> $state = $_POST['state'];
> $zip = $_POST['zip'];
> $phone = $_POST['phone'];
> $email = $_POST['email'];
> $howfound = $_POST['howfound'];
> $searchengine = $_POST['searchengine'];
> $keywords = $_POST['keywords'];
> $magenta = $_POST['magenta'];
> $magentaoff = $_POST['magentaoff'];
> $remclassic = $_POST['remclassic'];
> $remoffice = $_POST['remoffice'];
> $remweb = $_POST['remweb'];
> $demomanual = $_POST['demomanual'];
> $magoffinstructions = $_POST['magoffinstructions'];
> $remarklit = $_POST['remarklit'];
> $comments = $_POST['comments'];
>
> ini_set('SMTP', "mail.example.com");
>
> $email = "email at example.com";
> $subject = "Website Download ($today)";
>
> $body = "First Name: $fname\r\n";
> $body .= "Last Name: $lname\r\n";
> $body .= "Co/Institution: $coinst\r\n";
> $body .= "Address1: $address1\r\n";
> $body .= "Address2: $address2\r\n";
> $body .= "City: $city\r\n";
> $body .= "State: $state\r\n";
> $body .= "Zip Code: $zip\r\n";
> $body .= "Phone: $phone\r\n";
> $body .= "E-mail: $email\r\n";
> $body .= "How Found: $howfound\r\n";
> $body .= "Search Engine: $searchengine\r\n";
> $body .= "Keywords Used: $keywords\r\n";
> $body .= "Magenta Demo: $magenta\r\n";
> $body .= "Magenta Office Demo: $magentaoff\r\n";
> $body .= "Remark Classic: $remclassic\r\n";
> $body .= "Remark Office: $remoffice\r\n";
> $body .= "Remark Web: $remweb\r\n";
> $body .= "Demo Manual: $demomanual\r\n";
> $body .= "Mag Office instructions: $magoffinstructions\r\n";
> $body .= "Remark Lit: $remarklit\r\n";
> $body .= "Comments: $comments\r\n"
>
> $headers = "From: webmaster at example.com\n";
> $headers .= "Reply-to: webmaster at example.com\n";
> $headers .= "CC: email at yahoo.com\n";
> $headers .= "Bcc: joel at spinhead.com\n";
> $headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
>
> mail ($email, $subject, $body, $headers) or die("Could not send mail");
>
> ?>
>
> ====================================================================
More information about the thelist
mailing list