php form send to email inputted in email field - php

I would like my simple php mailer form, to maintain sending me the filled out form; but also I would like to send them a confirmation message based on their inputted email address field - wondering if it's possible to send to email inserted in the form.
Eg: User fills out email address in the field as asked; this is where custom confirmation will be sent upon submit (along with form submission to me)
Note: // Same confirmation will be sent to every form user. I simply would like to write; with PHP - send this message to the email address inserted by current user in email field.
Updated:
I currently have the form submitting at my email:
$emailAddress = 'myemail#myemail.com';
I'm getting the fields with:
Email: '.$_POST['email'].'<br />
How do I edit it to send to the user inputted 'email as their email address' -- and spit out the generic message with it?
Recent attempt:
$emailAddress = 'myemail#myemail.com';
$to = $_POST['email'];
$message = 'Thank you for submitting the form on our website.!';
Form submits as normally; but doesn't send confirmation to '$to = $_POST['email'];'
Any pointers?
Complete code:
<?php
$emailAddress = 'myemail#myemail.com';
$to = $_POST['email'];
$message = 'Thank you for submitting the form on our website.!';
/* config end */
require "phpmailer/class.phpmailer.php";
session_name("fancyform");
session_start();
foreach($_POST as $k=>$v)
{
if(ini_get('magic_quotes_gpc'))
$_POST[$k]=stripslashes($_POST[$k]);
$_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}
$err = array();
if(!checkLen('name'))
$err[]='The name field is too short or empty!';
if(!checkLen('email'))
$err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
$err[]='Your email is not valid!';
/* if(!checkLen('phone'))
$err[]='The phone field is too short or empty!'; */
/*if(!checkLen('subject'))
$err[]='You have not selected a subject!';*/
/* if(!checkLen('message'))
$err[]='The message field is too short or empty!';*/
if((int)$_POST['captcha'] != $_SESSION['expect'])
$err[]='The captcha code is wrong!';
if(count($err))
{
if($_POST['ajax'])
{
echo '-1';
}
else if($_SERVER['HTTP_REFERER'])
{
$_SESSION['errStr'] = implode('<br />',$err);
$_SESSION['post']=$_POST;
header('Location: '.$_SERVER['HTTP_REFERER']);
}
exit;
}
$msg=
'Name: '.$_POST['name'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['email'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Referred By: '.$_POST['referer'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";
$mail->MsgHTML($msg);
$mail->Send();
unset($_SESSION['post']);
if($_POST['ajax'])
{
echo '1';
}
else
{
$_SESSION['sent']=1;
if($_SERVER['HTTP_REFERER'])
header('Location: '.$_SERVER['HTTP_REFERER']);
exit;
}
function checkLen($str,$len=2)
{
return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}
function checkEmail($str)
{
return preg_match("/^[\.A-z0-9_\-\+]+[#][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}
?>
How could I output:
$to = $_POST['email'];
$message = 'Thank you for submitting the form on our website.!';

Have a look at PHPMailer. It's a pretty hand library for sending emails.
http://phpmailer.worxware.com
You can then just use GET or POST variables to send the email to the server

Sure you can do this. It's just as straight forward as it sounds. I recommend using a mailer helper like phpmailer, but you do it with the php mail function as well.
//check the email is real
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$to = $_POST['email'];
$subject = 'Thanks for filling out my form';
$message = 'The message';
$headers = 'From: me#mywebsite.com' . "\r\n" .
'Reply-To: webmaster#mywebsite.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
straight outta the manual

I believe you are looking for the built-in PHP mail() function. You post a form to your PHP page, with HTML like so:
<form action="your_php_script.php" method="post">
E-mail: <input type="text" name="email" />
<input type="submit" value="submit">
</form>
And your_php_script.php like so:
<?php
$to = $_POST['email'];
$subject = 'Confirmation E-mail';
$message = 'Thank you for submitting the form on our website.';
$headers = 'From: donotreply#yoursite.com' . "\r\n" .
'Reply-To: webmaster#yoursite.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
http://php.net/manual/en/function.mail.php

Related

(Noob) - Simple PHP E-mail form code?

I bought a bootstrap theme just to make a simple site. It has a contact form on it and the theme creator said to make sure your hosting let's you use php. I asked my host and they said:
"You need to add email authentication to the form.
Create an email address in cpanel for use with the form, then use that
user name and password to authenticate the email sending from the
form.
http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm"
I created the e-mail in cpanel, but unfortunately, I am not knowledgable enough for the next step. I was hoping someone could help me with the code to get this form working.
This is the email.php code in it's entirety:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = "Message";
$body = "From $name, $email, \n\n$message";
$to = "myemail#gmail.com";
mail($to, $subject, $body);
?>
Is there something simple I can add to this to authenticate and make the form work?
Updated answer:
You could edit your email.php / html code to the following:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send email example</title>
</head>
<body>
<form method="post">
<input type="text" name="sender_email" placeholder="Senders email address">
<input type="text" name="receiver_email" placeholder="Receivers email address">
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" placeholder="Message"></textarea>
<button type="submit">Send email</button>
</form>
</body>
</html>
<?php
require_once('send_email_class.php');
if(!empty($_POST)){
$sender_email = $_POST['sender_email'];
$receiver_email = $_POST['receiver_email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$send_email = new send_email_class($sender_email, $receiver_email, $subject, $message);
$send_email_response = $send_email->send_email();
echo $send_email_response;
}
?>
and upload the following code as a file called: send_email_class.php to your webserver.
<?php
class send_email_class {
// Class constructor storing variables
function __construct($sender, $receiver, $subject, $message){
$this->receiver = $receiver;
$this->subject = $subject;
$this->message = $message;
$this->header = 'From: ' . $sender . '\r\n';
}
// Function to send the email
function send_email(){
if(mail($this->receiver, $this->subject, $this->message, $this->headers)){
return 'Mail sent.';
}else{
return 'Unable to send mail.';
}
}
}
?>
Ofcourse you don't have to enter all those informations from the form, you could store some informations like the $sender_email variable fixed in your PHP code.
But thats up to you. Hope this helped.
Edit:
Please replace the content of the email.php with the following:
<?php
class email{
// Class constructor storing variables
function __construct($sender_email, $sender_name, $message){
$this->sender = $sender_email;
$this->sender_name = $sender_name;
$this->message = $message;
$this->receiver = 'changeme#email.com';
$this->subject = 'Email from: ' . $sender_name;
$this->header = 'From: ' . $sender_email . '\r\n';
}
// Function to send the email
function send_email(){
if(mail($this->receiver, $this->subject, $this->message, $this->header)){
return 'Thank you ' . $this->sender_name . ' for sending the email containing the following message: ' . $this->message;
}else{
return 'Sorry ' . $this->sender_name . ' a error occured, please try again.';
}
}
}
if(!empty($_POST)){
$sender_email = $_POST['email'];
$sender_name = $_POST['name'];
$message = $_POST['message'];
$send_email = new email($sender_email, $sender_name, $message);
$send_email_response = $send_email->send_email();
echo $send_email_response;
}
?>
And edit the following code line: $this->receiver = 'changeme#email.com';
Final answer:
After receiving the files, the fix was easy, the ajax been dealing with the handling itself.
mail.php content:
<?php
$receiver = 'xxxxx#gmail.com';
$subject = 'Email from: ' . $_POST['name'];
$message = $_POST['message'];
$header = 'From: ' . $_POST['email'];
mail($receiver, $subject, $message, $header);
?>

PHP Contact Form not emailing all fields

I have VERY little experience with PHP.
I am testing out a contact form for this website. I am only getting the "subject, message, and header" fields in the email.
I need the telephone and name fields to show in my email message as well.
What am I doing wrong?
Any help is appreciated -- thanks!
<?php
$name;$email;$message;$captcha;$telephone;
if(isset($_POST['name'])){
$name=$_POST['name'];
}if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['telephone'])){
$telephone=$_POST['telephone'];
}if(isset($_POST['message'])){
$message=$_POST['message'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form. <a href=http://www.website.com#contact/>Back to Form</a></h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LcYjgcT****q9vhur7iH_O4dZPl4xUAVwW8=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo '<h2>You are spammer ! Get the #$%K out</h2>';
}else
{
// Checking For Blank Fields..
if ($_POST["name"] == "" || $_POST["email"] == "" || $_POST["telephone"] == "" || $_POST["message"] == "") {
echo "Fill All Fields..";
} else {
// Check if the "Sender's Email" input field is filled out
$email = $_POST['email'];
// Sanitize E-mail Address
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email) {
echo "Invalid Sender's Email";
} else {
$to = 'email#gmail.com';
$subject = 'Contact Form';
$message = $_POST['message'];
$name = $_POST['name'];
$headers = 'From:' . $email . "\r\n";
// Sender's Email
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message .= "\r\n Name: " . $name;
$message .= "\r\n Email: " . $email;
$message .= "\r\n Telephone: " . $telephone;
$message .= "\r\n Message: " . $message;
// Send Mail By PHP Mail Function
if (mail($to, $subject, $message, $headers)) {
echo "Your mail has been sent successfully!<a href=http://wwwwebsite.com>Back</a>";
} else {
echo "Failed to send email, try again.";
exit ;
}
}
}
}
?>
The $message variable is the content of the mail that will be sent to your adress, add the values you want to get in your mail to that variable.
like so : (since you're not using HTML mail I think)
$message .= "\r\n Name : " . $name;
$message .= "\r\n Email : " . $email;
etc..
Before you call the mail function.
ALSO :
You're adding Phone number, Email, and message in your $email variable. you should give these their own variable.

PHP form to returning all the information

I am a complete novice when it comes to php, I have got the below php which sends me back the 'message' and sends an auto response to the user, as well as redirecting them to the 'thank you' page. Problem I am having is that it won't return the users name that they fill in on the form, any ideas?
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.', $message, "From: Krew Kut Hair<$email>")) {
$autoreply = "Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Assuming the name is in one of the form fields, you should be able to retrieve it. As Barmar says - all you have to do is use it somewhere in the body or the message. How can you tell the name is missing if you don't echo it out somewhere.
Try this:
$autoreply = "Thank you ".$name." for ...
If the name is still "missing" - you can try to see all the post variables like this:
echo "<PRE>Post Vars\n"; print_r($_POST);
If you have an input named "name" like:
<input type="text" name="name" value="" />
Check if it's containing data with e.g. :
echo 'The value of name is ['.$name.']';
If it is containing data you just can use the $name variable in your message. If it isn't there is probably something wrong in your HTML form.
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
$content = "<strong>Name:<strong><br />".$name."<br />";
$content .= "<strong>Message:<strong><br />".$message."<br />";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.<br />', $content, "From: Krew Kut Hair<$email>")) {
$autoreply = "Hi ".$name.". Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Also read the comments on your question. I strongly recommend to find an other way instead of using extract().

PHP reply-to error - need person's email, not admin

I have the below PHP contact form that has a CAPTCHA code to ensure is correct. However, when I reply to the email from the website it puts a random email which i believe is the server admin, however, I want it to be the persons email who sent the form in. below is the code, could you possibly be able to help me?
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty ($_SESSION['chapcha_code'] ) ) {
$youremail = 'info#example.com';
$fromsubject = 'www.example.co.uk';
$title = $_POST['title'];
$fname = $_POST['fname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = $youremail;
$mailsubject = 'Message from Website'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is: '.$fname.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your message. I will contact you shortly if needed.<br/>Go to <a href='/index.html'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.html'>Contact Page</a>";
}
?>
You'll need some headers so the from address is the users mail.
Also refer to the mail docs
try this
$headers = "From: $mail\r\n";
$headers .= "Reply-To: $mail\r\n";
mail($to, $subject,$body,$headers);

Email form fields array

Who can i create an array with email form fields array.
if(isset($_POST['submit'])){
$content=array(
$name=$this->strip_tags($_POST['name']),
$email=$this->strip_tags($_POST['email']),
$phone=$this->strip_tags($_POST['phone']),
$address=$this->strip_tags($_POST['address']),
$city=$this->strip_tags($_POST['city']),
$subject=$this->strip_tags($_POST['subject']),
$message=$this->strip_tags($_POST['message'])
);
and then send it using the mail function.
Thank you in advance
You could just do this, which is a lot easier:
if(isset($_POST['submit'])) {
$to = "someone#example.com"; // send email to
$from = "Your Name <you#example.com>"; // send email from
$subject = "Form submitted"; // email subject
$body = "The form has been submitted!
Here's the form data that was submitted.
Name: ".$_POST['name']."
Email: ".$_POST['email']."
Phone: ".$_POST['phone']."
Address: ".$_POST['address']."
City: ".$_POST['city']."
Subject: ".$_POST['subject']."
Message:
".$_POST['message']."
Some other text under the form data here...";
$email = mail($to, $subject, $body, "From: $from");
if($email) { // you don't actually need this, it's just to make sure it sends :)
echo "Email sent successfully";
}
}
This will send an email with the form data submitted.
I hope this helps!

Categories