2
   

Creating a reusable dynamic PHP email/mailer script...

 
 
Monger
 
Reply Fri 10 Dec, 2004 12:23 pm
...I wrote the following simple PHP mail script based on some code Craven sent me a while back:

Code:<?php
//Comma separated emails can allow for sending to multiple recipients.
$to = "[email protected]";

$extra = "From: ".$_POST['email']."\r\nReply-To: ".$_POST['email']."\r\n";
$subject = "Web form submission";
$mess = "This is from: ".$_POST['name']." (".$_SERVER['REMOTE_ADDR'].")\n"
. "\nEmail: " . $_POST['email']
. "\nTel: " . $_POST['tel']
. "\nEtc: " . $_POST['etc'];

mail ($to, $subject, $mess, $extra);
?>


...The specifics of it are not important, but I'm hoping to modify it to work with a long, complex form with dozens of input fields to fill out which may be messed around with regularly.

As such, I'm hoping to not have to write into the mail script all the variables like I'm doing above...
I'm going to make up invalid code here, but this is the kinda thing I'm hoping to do, instead of manually writing all variables & captions into my $mess variable:

Code://THIS SCRIPT CONTAINS BOGUS CODE...Do not try this at home!

$i = 0;
$mess = "";
while ($i <= $_POST.length - 1) {
$mess .= convertVarNameToString($_POST[$i])
. ": "
. $_POST[$i]
. "\n";
$i++;
}


In addition to convertVarNameToString() being bogus, I suspect that $_POST.length won't return the number of items in the array and using $_POST[] with a number as the array item won't work, but hopefully it's a little clearer from that what I'm trying to change ...that is, if I submit data to the script from a form with 2 input objects -- $email with the value "[email protected]", and $name with the value "Monger" -- my $mess variable should contain the following:

"email: [email protected]\nname: Monger\n"

Is this (e.g. converting variable names to strings, for starters) even possible? If so, any pointers in the right direction to help me accomplish it would be cool... Smile


Edit: removed autoresponder & validation stuff to shorten code.
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 2 • Views: 2,587 • Replies: 5
No top replies

 
Monger
 
  1  
Reply Fri 10 Dec, 2004 01:35 pm
Thinking...I suppose including an array of hidden input items like...

Code:<input type="hidden" name="inputArray" value="Email" />
<input type="hidden" name="inputArray" value="Name" />
<input type="hidden" name="inputArray" value="Tel" />
...

...in my HTML form could allow me to get around converting PHP variable names to strings, though it wouldn't be as elegant a solution...
0 Replies
 
Monger
 
  1  
Reply Fri 10 Dec, 2004 11:23 pm
OK well, I've completed a working solution for this, which I'll post below.

I wasn't able to create a true array of hidden inputs by just using the same names for them, but at least the method used here still allows me to avoid writing a huge list of variable names & their captions into the mail script itself (which keeps the mail script code shorter & allows it to be reusable by multiple forms).

Heres' a sample HTML form to go with it (validation JavaScript not included):
Code:<form method="post" action="mail.php" onSubmit="return validate()">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Phone: <input type="text" name="tel" /><br />
More: <input type="text" name="etc" /><br />
<input type="submit" name="submit" value="Submit" />

<input type="hidden" name="input_1" value="name" />
<input type="hidden" name="input_2" value="email" />
<input type="hidden" name="input_3" value="tel" />
<input type="hidden" name="input_4" value="etc" />
<input type="hidden" name="numInputs" value="4" />
</form>


Here's my mail.php file:
Code:<?php
//Comma separated emails can allow for sending to multiple recipients.
$to = "[email protected]";

$extra = "From: ".$_POST['email']."\r\nReply-To: ".$_POST['email']."\r\n";
$subject = "Contact form submission";
$currInputName = "";

$mess = "This is from: ".$_POST['name']." (IP: ".$_SERVER['REMOTE_ADDR'].")\n";
$i = 1;

//Append "\nName: Value" to $mess for every item passed by the "input_x" hidden inputs
while ($i <= $_POST['numInputs']) {
$currInputName = $_POST['input_' . $i];
$mess .= "\n" .$currInputName. ": " .$_POST[$currInputName];
$i++;
}

mail ($to, $subject, $mess, $extra);
?>


The messages returned need to be improved/customized, but there ya have it...

I know very little PHP so if you see a better way to do this than what I've hacked out above, I'm all ears.

Edit: removed autoresponder & validation stuff to shorten code.
0 Replies
 
Craven de Kere
 
  1  
Reply Wed 22 Dec, 2004 02:25 am
If the formatting of the email is not important to you, this following (very clean and very basic) script will handle any variables you throw at it (no need to declare them as hidden fields either).

Code:<?php
$mess = print_r($_POST, true);
$extra = "From: $email\r\nReply-To: $email\r\n";
$to = "[email protected]";
$subject = "subject";
mail ($to, $subject, $mess, $extra);
?>
0 Replies
 
Monger
 
  1  
Reply Wed 22 Dec, 2004 08:44 am
Cool! Thanks!

For others who read this, here's an example email returned by Craven's script above:
Code:Array
(
[name] => My Name
[email] => [email protected]
[phone] => 555-555-5555
[enquiry] => This be my enquiry.
[submit_x] => 40
[submit_y] => 7
)


Although, like Craven mentioned, that won't allow you to format the email, since it does output the variable names along with their values, it's good enough for my purposes.

Also, if you're using PHP 5, global variables defaults to off, so you may need to replace the 2 references to "$email" in Craven's script with "$_POST['email']". (I needed to on my server.)
0 Replies
 
Craven de Kere
 
  1  
Reply Sun 26 Dec, 2004 11:28 pm
I plan to extend a mail script extensively and will post it but that example was to show that all the posted variables are available in a superglobal array.

Once you know where to look you can do different things with it:

Code:<?php
// sets the initial message variable, you can use this chance to add the IP
$message = "Message<br />";
// loops through the array appending to the message variable
foreach ($_POST as $key => $value)
{
// the period is what is appending to the variable, it is one concatenation operator
$message .= "$key: $value<br />";
}

echo $message;

?>
0 Replies
 
 

Related Topics

Webdevelopment and hosting - Question by harisit2005
Showing an Ico File - Discussion by Brandon9000
how to earn money in internet - Discussion by rizwanaraj
The version 10 bug. Worse then Y2K! - Discussion by Nick Ashley
CSS Border style colors - Question by meesa
There is no Wisdom in Crowds - Discussion by ebrown p
THANK YOU CRAVEN AND NICK!!! - Discussion by dagmaraka
I'm the developer - Discussion by Nick Ashley
 
  1. Forums
  2. » Creating a reusable dynamic PHP email/mailer script...
Copyright © 2025 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 01/15/2025 at 11:58:16