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!
Related
I wish to send a copy of the data filled out by the visitor to myself and the other copy to the visitor. Please advice!
My email is myemail#mysite.com while the visitor's email is $email
This code is as seen below:
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "myemail#mysite.com, .$email";
I'm not getting any results after hitting the submit button!
I'll be very grateful!
<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "myemail#mysite.com, .$email";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
You have to put myemail and visitors emails in one array and then run a loop, the same message will be send to all in array.
e.g.
$aEmails = array();
$aEmails[] = 'myemail#something.com';
$aEmails[] = 'visitoremail#something.com';
foreach(aEmails AS aEmail){
mail($aEmail, $subject, $email_content, $email_headers);
}
If you are not getting any result then you must check your html forms' tag it should have method="POST" attribute.
Then in here remove the dot
$recipient = "myemail#mysite.com, .$email";
like this
$recipient = "myemail#mysite.com, $email";
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().
I have made a .PHP file that when the email form is filled out it sends it to my email address. Now this all works fine, I get the email, but, I get empty fields, here is the email I received from my test:
"Name:
Email:
Messages: "
as you see, all the fields are empty even though they were filled out.
Here is the code for the .php file:
<?php
$first_name = $_POST['name'];
$email_message = $_POST['email'];
$message = $_POST['message'];
$headers = "Website message";
$headers = "From: $_email";
$to = 'enquiries#ajmoger.co.uk';
$subject = 'AJ Moger website comment submission';
$message = "
Name: $first_name \n
Email: $email_message \n
Messages: $message \n";
mail($to, $subject, $message, $headers);
header("Location: thank_you.html");
die();
?>'
and here is the HTML code for the form:
The reason is enctype="text/plain" on your <form>. Remove that from your <form> code.
There seems like a bug was filed on this topic , but the status is closed. So it's better not to use that encoding type.
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
When somebody send me a message on my websites contact form, I click the reply button to reply.
I then receive a response that the email was not delivered / failed.
Well, its because the gmail email wants to respond to the server email, instead of the email address that was entered into the Contact Form on my website.
Does anybody know how to fix this?
Here is the PHP script for my contact form:
<?php
$mailTo = 'emailaddress#gmail.com';
$name = htmlspecialchars($_POST['cform_name']);
$mailFrom = htmlspecialchars($_POST['cform_email']);
$subject = 'Message from your website';
$message_text = htmlspecialchars($_POST['cform_message']);
$message = 'From: '.$name.'; Email: '.$mailFrom.' ; Message: '.$message_text;
mail($mailTo, $subject, $message);
?>
Use this:
mail($mailTo, $subject, $message, 'Reply-To: '.$mailFrom);