views:

579

answers:

4

I'm looking to create a form on my website where users can enter their email address and a message (however long). Then upon clicking on a submit button, the code, sends the message as an email to my mailbox.

I was wondering if someone knew what code i could use. I am using PHP as my server-side language.

+3  A: 

You need to have access to an smtp server somewhere. Assuming you do, use the php mail function to send the mail like so:

$Name = "John doe"; //senders name 
$email = "[email protected]"; //senders e-mail adress 
$recipient = "[email protected]"; //recipient 
$mail_body = "The text for the mail..."; //mail body 
$subject = "Subject for receiver"; //subject 
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

mail($recipient, $subject, $mail_body, $header);

The smtp server is set in the php.ini file in these two lines:

SMTP = servername
smtp_port = 25

More information at w3schools site.

trex279
+2  A: 

Check this link. Your tag says php so the link is for php. BTW the link is the first one from google search!

Shoban
+2  A: 

while the above two answers provide a basic email sending suggestion, there's one thing you should consider, the codes are not secure. spammers can inject Cc: codes and send spam using the form. if they do, your smtp provider may ban your account. try a dedicated mailer, like phpmailer

Tutul
A: 

Try to use PHPForms. It is an ultimate web form builder that allows to create professionally looking email forms within just a few minutes. You will only need to create a PHP form by means of an intuitive interface and generate a code that can be easily copied and pasted to any web page.