Need city/state snippet for contact form - php

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.

Related

PHP Send Mails Problems

i'm using a normal php script for recive mail from my website directly to my personal mail. When i try to test and send from contact form a mail to me, output always said: "Something went wrong, please try again" like if there's no validation. It's my first time that i try to incorporate a php script so probably i'm missing something very oblivious.
Here's the code:
<?php
$siteOwnersEmail = 'Myemail#gmail.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
PHP mail function send email without SMTP authentication. It would be more accurate to send e-mail with SMTP authentication. I can suggest you to check the link below.
https://github.com/PHPMailer/PHPMailer

Email Not Sending from Byethost Hosting

I deployed my website and I'm trying to send an email from my website that is hosted by Byethost and it displays a success message but for some reason the email is not being received. What might be the problem?
Below you will find the PHP script.
<?php
// Replace this with your own email address
$siteOwnersEmail = 'shouman882#gmail.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
mail() function is not avilable in byethost free hosting you need to upgrade to premium.

php mail sending from a default email address instead of one I specify

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.

I have a php error that is showing for no reason and want to find a way to fix the error or remove it

Notice: Undefined variable: error in
/storage/ssd5/047/2981047/public_html/inc/sendEmail.php on line 47
OK
this error is being shown when I send an email from my own site...
I want to find a solution to this since the error is being shown on the site itself, not in the console, etc...
The message up is what is being shown on my site when the form is filled correctly...
Please, not I have no experience in PHP.
<?php
$siteOwnersEmail = '*********************';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message = "";
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//Error Starts is her if im not mistaken....
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
When you call if (!$error) { the variable $error might not have been created - it only comes into existence when either the length test or regex test fail.
As the $error variable seems to be an array when it is created you ought to create a default empty variable. You ought to also check for the other POST variables being available before trying to use them or similar errors will occur potentially.
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['contactName'],$_POST['contactEmail'],$_POST['contactSubject'],$_POST['contactMessage'] ) ){
$error=[]; # default, empty error variable
if (strlen($name) < 2) $error['name'] = "Please enter your name.";
/* etc */
if ( empty( $error ) ) {
/* try to send email */
} else {
/* errors happened - let user know or perform other tasks */
}
}
You have to assign default values to variable $error, for example
$error = '';

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.

Categories