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. :)
Sending letters from sites to e-mail
Started by xle_camry, Oct 18 2010 09:16 AM
6 replies to this topic
#1
Posted 18 October 2010 - 09:16 AM
|
|
|
#2
Posted 18 October 2010 - 10:05 AM
<?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
#3
Posted 19 October 2010 - 09:06 AM
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
Posted 19 October 2010 - 06:36 PM
You would put that on the processing page (what your form calls after the user submits)
You can get the form data through $_POST:
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 20 October 2010 - 10:15 AM
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?
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
Posted 20 October 2010 - 11:55 AM
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:
Then it's just to add the head-tags and the rest of the page around this form...
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
I study Information Systems at Karlstad University when I'm not on CodeCall
#7
Posted 21 October 2010 - 09:55 AM
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. :)


Sign In
Create Account


Back to top









