Jump to content

Sending letters from sites to e-mail

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Hello people! I have made a site. I am amateur in php. I want to ask if anyone knows how to make that people can send letters from my site to my mail. For example, i have created textarea and button send(in contact menu). How i can make that it will be sent to my e-mail?
Thanks. :)

#2
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

PHP: mail - Manual

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#3
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Hello DEViANT, thanks. If you can, please could you explain me step by step? Where should i locate that code? In the same file where my form exists? What your functions mean, etc.. Grateful for your help! :)

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You would put that on the processing page (what your form calls after the user submits)

You can get the form data through $_POST:
$to      = 'youremail@yourpersonalemail.com';
$subject = $_POST['subject']; //from name="subject" field 
$message = $_POST['message']; //from name="message" field
$headers = 'From: contactform@yoursite.com' . "\r\n" .
    'Reply-To: contactform@yoursite.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

$return = mail($to, $subject, $message, $headers);
if($return === true) {
    echo "Your e-mail has been sent to me, thank you!";
}
Of course editing the e-mail addresses as you see fit.

Edited by Alexander, 19 October 2010 - 08:05 PM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Thank you very much Nullw0rm. I will try it. Let me ask one question more: then i should create two files-one html contact form and other-processing.php. isnt it?
Then all their letters will come to my mail yes? And also, should i post the submit button too? Like: $submit= $_POST['submit']; in the processing php?

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
You should post your submit button only if you want the text on it in the mail (good if the user has several buttons to press).
Otherwise, you can choose to have one html and one php or use the same file for it.

a combined file can look like:

<?php


if ($_POST['submit'] != "") {

    $to      = 'youremail@yourpersonalemail.com';

    $subject = $_POST['subject']; //from name="subject" field 

    $message = $_POST['message']; //from name="message" field

    $headers = 'From: contactform@yoursite.com' . "\r\n" .

        'Reply-To: contactform@yoursite.com' . "\r\n" .

        'X-Mailer: PHP/' . phpversion();


    $return = mail($to, $subject, $message, $headers);

    if($return === true) {

        $msg = "Your e-mail has been sent to me, thank you!";

    } 

}

?>

<html><head></head>

<body>

<form method="POST" action="contact.php">

Subject: <input type="text" name="subject" /><br />

Message: <textarea name="message"></textarea><br />

<input type="submit" name="submit" value="Send message" /><br />

<?php 

if ($msg != "") {

    echo '<span style="color:red;">'.$msg.'</span>';

?>

</body></html>


Then it's just to add the head-tags and the rest of the page around this form...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Thanks Orjan.Tomorrow i am free from my classes at university. I will try your code. If any problem, i will write to you and hope for your help. :)