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);
?>
Related
When ever I test this code it always fails can anyone help?
<?php if(isset($_POST['submit'])){
$to = "<<<___myEmail___>>>";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Contact Form: LewisDerbyshire.co.uk";
$subject2 = "Copy of your form submission : LewisDerbyshire.co.uk";
$message = $name . "wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$IP = "Senders IP :" . [REMOTE_ADDR];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$IP,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", I will contact you shortly.";
if ($sent) {
$result = 'Thank you,' . $name . 'Your message has been sent.';
} } else {
$result = 'Sorry' . $name . ', there was a problem.'; } ?>
Also I have <?php echo $result; ?> next to my table but how do I stop it showing the message before anyone clicks submit.
Live view
By the first look, I see that you are missing the variable name in the line where you try to get the remote IP.
Instead of just [REMOTE_ADDR], try $_SERVER['REMOTE_ADDR'].
If it does not work after this fix, please post some error messages to make it easier to help you.
You missed your input code, so I tried to make one, hope this helps.
First, you closed if tag too early.
Second, variable $sent is not defined.
Third, my $sent variable is not clear yet...
<form method="POST">
<input type="text" name="name" />
<input type="text" name="email" />
<input type="text" name="message" />
<input type="submit" name="submit" />
</form>
<?php if (isset($_POST['submit'])) {
$to = "Your mail";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Contact Form: LewisDerbyshire.co.uk";
$subject2 = "Copy of your form submission : LewisDerbyshire.co.uk";
$message = $name . "wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$IP = "Senders IP :" . $_SERVER["REMOTE_ADDR"];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
$sent = mail($to, $subject, $message, $IP, $headers);
mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", I will contact you shortly.<br/>";
if ($sent) {
$result = 'Thank you,' . $name . ' Your message has been sent.';
echo $result;
} else {
$result = 'Sorry' . $name . ', there was a problem.';
echo $result;
}
} ?>
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
We have a PHP form on our website that has worked for years. When users fill the form out in sends us the details, and sends them an email and then re-directs the user to a "thank-you" page. However, if a user puts their email address as #gmail.com the form fails to send, it doesnt show the thank-you page.
This is really bizarre. We are using PHPMailer and validate if the form has been sent using:
if($mail->Send()) {
$mailsent = true;
If a user enters #hotmail.com or at #yahoo.com everything is fine. But enter #gmail.com and $mailsent is always false.
In this situation where does the problem lye? It seems to me that our web hosts SMTP connection to Gmail is failing. Would this seem right?
Here is the code:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$name = '';
$email = '';
$mailsent = false;
$referer = '';
if(isset($_POST['name'])){
include('phpmailer/class.phpmailer.php');
$name = $_POST['name'];
$email = $_POST['email'];
$subject = 'Quote request from ' . $name;
$body = 'A quote request has been received from a user with following details:' . "\n\n" .
"Name: $name \n" .
"Email: $email \n" .
"----------------------------------------------------\n\n" .
"Place: UK".
$body .= "\n----------------------------------------------------\n\n";
$body .= "Refering: ".$referer." \n";
$mail = new PHPMailer();
$mail->Subject = $subject;
$mail->From = $email;
$mail->FromName = $name;
$mail->Body = $body;
$mail->AddAddress("dean#example.com", "Example");
if($mail->Send()) {
$mailsent = true;
} else {
$error[] = 'There was some error. Please try later.';
}
}
?>
iv added the header field in the form code but when i receive the email, the "from" field in the email shows a garbled code and not the email address of the person who sent the message. This is my message processing code:
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty($_SESSION['chapcha_code'] ) ) {
$youremail = 'I removed my address for privacy on stack overflow';
$fromsubject = 'A message from your website';
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['mail'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $nome <$mail>\r\n";
$to = $youremail;
$mailsubject = 'Message received from'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is '.$fname.' '.$lname.'
Address: '.$address.'
'.$city.', '.$zip.', '.$country.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your feedback. I will contact you shortly if needed.<br/>Go to <a href='/index.php'>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.php'>Contact Page</a>";
}
?>
You declared your ^From: header` here:
$headers = "From: $nome <$mail>\r\n";
But it isn't used when you call mail()
mail($to, $subject, $body);
You need to pass $headers as fourth parameter. Else the headers won't take effect
mail($to, $subject, $body, $headers);
You haven't declared $nome anywhere in your code nor $mail in:
$headers = "From: $nome <$mail>\r\n";
And besides that, as this comment mentions, you didn't pass the header into the mail function. See the fourth parameter of the mail function.
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);