Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » Form mail: User defines recipient in form RSS

Form mail: User defines recipient in form

This thread was started by UNarmed and has been viewed 531 times, and contains 8 replies, with the last reply made by UNarmed.
Post Reply
1
27 points at 100%
UNarmed, WDF User Private message  
Posted January 28 '10 at 04:02 AM
      Posts: 72
Hey i need to setup some sort of form mailer but with one distinct difference. Instead of the recipient being predefined i need something that allows the user to define the recipient of the email in the form itself.

In a nutshell ... the bellows is a example of the form.

Name: Bob
E-mail: Bob@gmail.com (the form results should be mailed to this address)
Text: Random banter!

Help would be greatly appreciated!

Advertisement Register for free to hide these ads and participate in discussions!

2
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 28 '10 at 08:25 AM
      Posts: 3,095
That's easy to do (unless your webhost is GoDaddy).

But ...

What precautions will you take to keep spammers from using your form
to mass email people using your website? That's the problem with doing
what you want to do. It's sort of a dangerous thing that could end-up getting
your webhost to close or suspend your account.
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?
mlseim is online now! Reply

3
27 points at 100%
UNarmed, WDF User Private message  
Posted January 28 '10 at 10:54 AM
      Posts: 72
Thanks for the reply, il sort that out later ... first i would just like to get the thing working. The host is Dataflame. If you could please elaborate how i would go about doing this it would realy help me. I have been searching the web for the entire day and i have come up dry =/

4
27 points at 100%
UNarmed, WDF User Private message  
Posted January 28 '10 at 10:57 AM
      Posts: 72
I thought about using FormMail.pl for this but it turns out that it can only e-mail the form results to e-mail accounts hosted by the server and that dosent help me one bit =/

5
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 28 '10 at 12:24 PM
      Posts: 3,095
The restriction to email only by account hosted is a function of the webhost.
If Dataflame has that limitation (like GoDaddy), you won't be able to do it with PHP either.

You can test it out though ...

Copy and paste this script as "email.php", and try different recipients ... see what happens:
<?php

$recipient 
"youremailaddress@aol.com";
$subject "This is an email test";
$sender "whosenttheemail@aol.com";

$content="This is a test to see if you can send an email to anyone.";

$message "
This is an email test
--------------------------------------------------
Email from:  $sender

$content
---------------------------------------------------
"
;

$extraheaders "From: $sender\nReply-To: $sender\n\n";

if (! 
mail($recipient$subject$message$extraheaders))
  echo 
"Mail did not send for some reason.";

echo 
"Email Sent";
?>
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?
mlseim is online now! Reply

6
27 points at 100%
UNarmed, WDF User Private message  
Posted January 29 '10 at 02:44 AM
      Posts: 72
Much thanks i will give this a try =]

7
27 points at 100%
UNarmed, WDF User Private message  
Posted January 29 '10 at 03:04 AM
      Posts: 72
Ok great that worked =D now what is the nexy step? I really appreciated the help mlseim!

8
27 points at 100%
UNarmed, WDF User Private message  
Posted January 29 '10 at 09:57 AM
      Posts: 72
Ah ok i got something working, this is probebly super bad haha but its my first time working with php.

<?php

$recipient 
$_POST["recipient"];
$subject "This is an email test";
$sender "salmon@electric-heart.com";
$field1 $_POST["field1"];

$content="This is a test to see if you can send an email to anyone.";

$message "
This is an email test 2 $field1
--------------------------------------------------
Email from:  $sender

$content
---------------------------------------------------
"
;

$extraheaders "From: $sender\nReply-To: $sender\n\n";

if (! 
mail($recipient$subject$message$extraheaders))
  echo 
"Mail did not send for some reason.";

echo 
"Email Sent";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="mail.php" method="POST" name="contact" id="contact">
   e-mail: <input type="test" name="recipient" value=""><br/>
   first field: <input name="field1" type="text"><br/>
 <input name="Submit" type="submit" value="Submit"></td>

</form>
</body>
</html>

Ok thus far this is working, which i am super happy about haha =D

I need to place some HTML formating and the values from the form in the e-mail that is sent to the recipient. If you could give me a hint of how i would go about this i would realy appreciate it =D

9
27 points at 100%
UNarmed, WDF User Private message  
Posted January 29 '10 at 10:44 AM
      Posts: 72
Ok nevermind i have it all working =] Thanks for giving me the start i needed and all the help!

<?php
// recipient
$recipient $_POST["recipient"];
// subject
$subject "This is an email test";
$sender "salmon@electric-heart.com";
$field1 $_POST["field1"];

$content="This is a test to see if you can send an email to anyone.";
// message
$message "
<html>
<head>
</head>

</body>
</html>
"
;

// To send HTML mail, the Content-type header must be set
$headers  'MIME-Version: 1.0' "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' "\r\n";

$extraheaders "From: $sender\nReply-To: $sender\n\n";

if (! 
mail($recipient$subject$message$headers))
  echo 
"Mail did not send for some reason.";

echo 
"Email Sent";
?> 
<html>
<body>
<br />First field: <?php echo $_POST["field1"]; ?>
</body>
</html>

Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Introducing QiPHP Framework - FINALLY... a Ruby on Rails Killer for PHP!!! smoseley PHP 32 October 15 '09 12:08 AM
PHP Mail Form Mekhet PHP 4 June 10 '08 03:55 AM
mail () form - Customizing for my needs, help! vero PHP 10 December 6 '07 06:27 PM
My mail form causes "Page has Expired" in IE TheGardener PHP 2 March 24 '07 04:04 PM
Mail Form Input Type = Fax? Franco50 General Design Discussion 1 October 19 '04 01:26 PM