sending form to email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
t3h_vagrant
Forum Newbie
Posts: 1
Joined: Tue May 06, 2008 5:12 am

sending form to email

Post by t3h_vagrant »

ok so lets say that i dont know much about php but here goes. im making this website with some forms in it. i need to know how to send the filled information to my email. any example or tutorial or template would be useful! thenks!
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: sending form to email

Post by Kadanis »

If you want very basic, I suggest you look up the mail() function on php.net

If you want more advanced, have a look at Swift mail. There is a dedicated forum on this site for Swift as the author is one of the contributors here.

mail() will offer you basic plaintext mails, Swift can offer you full HTML, mail merge and loads more. Have a look at both and see which covers your needs.

As for the form stuff, I've included some very basic examples below. You'll obviously need to pad these out to suit.

Code: Select all

 
<!-- Very basic form for mailing. Put this in mail.htm -->
<html>
<head />
<body>
<form name="mailer" method="post" action="mail.php">
    To: <input name="to" value="" type="text" /><br />
    Subject: <input name="subject" value="" type="text" /><br />
    Message: <textarea name="message" />
    <input type="submit" value="Send" />
</form>
</body>
</html>
 

Code: Select all

 
<?php
#mail.php
 
#get the form variables from the super-global
#sets the variables to null if they are not picked up from the form
$to = (isset($_POST['to'])) ? $_POST['to'] : null;
$subject = (isset($_POST['subject'])) ? $_POST['subject'] : null;
$message = (isset($_POST['message'])) ? $_POST['message'] : null;
 
$from = 'myfromaddress@mydomain.tld';
 
if (!is_null($to) && !is_null($subject) && !is_null($message)){
    #insert mail code here using either Swift or mail()
} else {
    #error handling
}
?>
 
Post Reply