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.
Related
I have a form that sends data to the script below. It's supposed to send a message from the person specified in the form's emailaddress. Instead, I get it coming from a strange email address from my hosting company. I've checked the php over and over again and can not find out where the issue is. The email does send... just from the wrong address...
<?
// set recipient email
$mymail = "thelamp.website#yahoo.com";
// get information from form
$name = $_POST['firstname'] . " - " . $_POST['lastinitial'];
$email = $_POST['emailaddress'];
$multiplemail = $mymail . ", " . $email;
$message = $_POST['testimonial'];
$message = wordwrap($message, 200, "\r\n");
$subject = "Testimonial Submission: ";
date_default_timezone_set('US/Eastern');
$body = "Date: " . date('m-d-Y') . " - Time: " . date('h:i:s A e') . "\n Name: " . $name . "\n eMail: <" . $email . "> \n wrote: \n" . $message;
$headers = "To:" . $email . ", " . $mymail . ", From:" . $email;
// check the eMail!
$emailB = filter_var($email, FILTER_SANITIZE_EMAIL);
if (filter_var($emailB, FILTER_VALIDATE_EMAIL) === false || $emailB != $email)
{
// Display error message!
echo "This eMail adress that you entered in the form is invalid! Please go back and enter a correct eMail address!";
// Exit the checking scriptlet!
exit(0);
}
else
{
if(!mail($multiplemail, $subject, $body, $headers))
{
// Testimonial was NOT sent
die ("Testimonial could not be sent! Please try again later!");
}
else
{
// Testimonial was successfully sent
?><meta http-equiv="refresh" content="0; URL=thankyou.html"><?
}
}
?>
Any help you can give me would be greatly appreciated. Thanks in advance.
php novice back again with some minor issues, I've researched for hours but can't find the solution.
I already use this form for another form and it works fine, but this time I added the Input fields of nric, rate and a checkbox "agree". These are the 3 fields that don't validate and stop the form from sending. Any help would be appreciated
Here is the code:
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$phone = ($_GET['phone']) ? $_GET['phone'] : $_POST['phone'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$nric = ($_GET['nric']) ?$_GET['nric'] : $_POST['nric'];
$rate = ($_GET['rate']) ?$_GET['rate'] : $_POST['rate'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
$agree = ($_GET['agree']) ?$_GET['agree'] : $_POST['agree'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your Full name Surname in UPPERCASE.';
if (!$phone) $errors[count($errors)] = 'Please enter your contact number - e.g. +6012345678.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$nric) $errors[count($errors)] = 'Please enter your Business No or NRIC if not a business.';
if (!$rate) $errors[count($errors)] = 'Please enter the rate you wish to pay (in RM)';
if (!$comment) $errors[count($errors)] = 'Please describe what you require this person for and when.';
if (!$agree) $errors[count($errors)] = 'Please agree to the Booking Fee.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - replace your email here
$to = 'me#myemail.com';
//sender - from the form
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Message from ' . $name;
$message = 'Name: ' . $name . '<br/><br/>
Phone: ' . $phone . '<br/><br/>
Email: ' . $email . '<br/><br/>
NRIC: ' . $nric . '<br/><br/>
Rate: ' . $rate . '<br/><br/>
Agree: ' . $agree . '<br/><br/>
Message: ' . nl2br($comment) . '<br/>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo 'Back';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
Are you sure it isn't as simple as a missing quote.
Look at
$to = me#myemail.com';
It should be
$to = 'me#myemail.com'; // ** Missing leading quote on string
My PHP contact form is working correctly and outputs all info including senders IP address in the subsequent e-mail. I'm having trouble coding the PHP form to also receive the sender's city and state in the email that the form generates.
I've tried several code snippets, but the most I get is the city and state output on the web page above the form. I need it in the email instead. Looking for a free service.
PHP form
<?php
include 'contact_config.php';
session_start();
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
include 'functions.php';
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$level = stripslashes($_POST['level']);
$age = stripslashes($_POST['age']);
$drums = stripslashes($_POST['drums']);
$referral = stripslashes($_POST['referral']);
$city = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
$subject = " Website Inquiry";
$message = "
Name: ".$_POST['name']
."
E-mail Address: ".$_POST['email']
."
Level: ".$_POST['level']
."
Age: ".$_POST['age']
."
Drums: ".$_POST['drums']
."
Referral: ".$_POST['referral']
."
Message: ".$_POST['content']
."
City: ".$_POST['city']
."
IP Address: ". $_SERVER['REMOTE_ADDR'];
$error = '';
// Check name
if(!$name)
{
$error .= 'Please enter your name.<br />';
}
// Check email
if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}
if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.<br />';
}
if(isset($_SESSION['captcha_keystring']) && strtolower($_SESSION['captcha_keystring']) != strtolower($_POST['captcha']))
{
$error .= "Incorrect captcha numbers.<br />";
}
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}
}
?>
Look at this part of your code:
$message = "
Name: ".$_POST['name']
."
E-mail Address: ".$_POST['email']
."
Level: ".$_POST['level']
."
Age: ".$_POST['age']
."
Drums: ".$_POST['drums']
."
Referral: ".$_POST['referral']
."
Message: ".$_POST['content']
."
City: ".$_POST['city']
."
IP Address: ". $_SERVER['REMOTE_ADDR'];
You are concatting Variables to a new Variable. Just add $_POST['state'] or how you called these fields, to this large Variable. Because you already added $_POST['city'].
So if it is not showing in the email, the Fields you are sending, are named differently.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm a bit of an amateur when it comes to PHP and so need some help please. I have made a contact form on my website which can be seen here:
http://babylace.co.uk/thegardenmedic.co.uk/contact.html
I am using a contact.php file to handle the form submission. When I fill in the form it submits and says successful (feel free to try) but I am not receiving the email in my inbox. I have changed the email in this question to example to keep my email private. My code for contact.php is as follows:
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$comments = $_POST['comments'];
if(trim($name) == '') {
echo '<div class="alert alert-error">You must enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="alert alert-error">You must enter a valid email address.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="alert alert-error">You must enter a valid email address.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div class="alert alert-error">You must enter your phone number.</div>';
exit();
} else if(trim($address) == '') {
echo '<div class="alert alert-error">You must enter your post code.</div>';
exit();
} else if(trim($comments) == '') {
echo '<div class="alert alert-error">You must include a message.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "example#example.com";
//$address = "example#example.com"";
$address = "example#example.com"";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Contact Form';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email $email or via Phone $phone, $email";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<div class='alert alert-success'>";
echo "<h3>Message Sent Successfully.</h3><br>";
echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
echo "</div>";
} else {
echo 'ERROR!';
}
Your script seems fine so far. Though, it might not be your PHP code which is wrong. Maybe you need to setup your SMTP Auth, see here:
Sending email with PHP from an SMTP server
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to send email using php script but its not working can you suggest me the error
The following code
<?php
try
{
$mail="atul#divyampg.0fess.us";
$contents="message";
$emailto1="atulkumaronline#gmail.com";
$subject="testing";
$headers="adesh";
mail($emailto1, $subject, $contents, $headers);
echo "mail send";
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
?>
http://php.net/manual/en/function.mail.php.
"adesh" is not a header.
Valid headers:-
'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Also, mail will return a bool value. You can implement like this :-
$contents="message";
$emailto1="atulkumaronline#gmail.com";
$subject="testing";
if(mail($emailto1, $subject, $contents))
echo "Sent";
else
echo "Error sending Email";
try this.
.
if(isset($_POST['send']))
{
$name = $_POST['name'];
$email = $_POST['email']; //'email' must be the same as name="email" in the input
$subject = $_POST['subject'];
$message = $_POST['message'];
$spamcheck = $_POST['spamcheck'];
if(trim($name) == '')
{
$error = '<div class="error">Please enter your name!</div>';
}
else if(trim($email) == '')
{
$error = '<div class="error">Please enter your email address!</div>';
}
else if(!isEmail($email))
{
$error = '<div class="error">You have enter an invalid e-mail address. Please, try again!</div>';
}
else if(trim($subject) == '')
{
$error = '<div class="error">Please enter a subject!</div>';
}
else if(trim($message) == '')
{
$error = '<div class="error">Please enter your message!</div>';
}
else if(trim($spamcheck) == '')
{
$error = '<div class="error">Please enter the number for Spam Check!</div>';
}
else if(trim($spamcheck) != '5')
{
$error = '<div class="error">Spam Check: The number you entered is not correct! 2 + 3 = ????</div>';
}
if($error == '')
{
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}
// make sure to change the email address below or you will nver receive mails
// the email will be sent to:
$to = "henry4u.u4edozie#gmail.com";
// the email subject
// '[Contact Form] :' will appear automatically in the subject.
// You can change the text if you wish.
$subject = '[Contact Form] : ' . $subject;
// the mail message ( add any additional information if you want )
$msg = "$message";
//Extras: User info (Optional!)
//Delete this part if you don't need it
//Display user information such as Ip address and browsers information...
$msg .= " \r\n\n---User information--- \r\n"; //Title
$msg .= "User IP : ".$_SERVER["REMOTE_ADDR"]."\r\n"; //Sender's IP
$msg .= "Browser info : ".$_SERVER["HTTP_USER_AGENT"]."\r\n"; //User agent
$msg .= "User come from : ".$_SERVER["HTTP_REFERER"]; //Referrer
// END Extras
mail($to, $subject, $msg, "From: $name <$email>\r\nReply-To: $name <$email>\r\nReturn-Path: $email\r\n");
?>
<!-- Message sent! (change the text below as you wish)-->
<h1>Congratulations!!</h1>
<p>Thank you <b><?=$name;?></b>, your message is sent! We will get back to you as soon as possible.</p>
<p>Return to the home page or use the navigation above.</p>
<!--End Message Sent-->
<?php
}
}
if(!isset($_POST['send']) || $error != '')
{
?>