I am having trouble with my PHP code for my website. The email is being sent but has no messages.
This is the email I get when it sends. It's empty.
Data I input in my contact form
I tested my php code without the css files and bootstrap, and received the email perfectly with the messages. But when I included everything, from css and bootstrap codes, I receive the email without any messages.
HTML CODE:
<div class="contact-form bottom">
<h2> We want to hear from you . Send us a message!</h2>
<form id="main-contact-form" name="contact-form" method="post" action="send_email_test.php">
<div class="form-group">
<input type="text" name="userName" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="userEmail" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="userMessage" id="message" required class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
This is my PHP code:
<?php
$field_name = $_POST['userName'];
$field_email = $_POST['userEmail'];
$field_subject = $_POST['userSubject'];
$field_message = $_POST['userMessage'];
$mail_to = 'info#mariamulan.com'; /* Add your email address here */
$subject = 'Message from website'.$field_name; /* Create your own subject */
$body_message .= 'From: '.$field_name."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Hey! Thanks for the message! We will try to reply to you as soon as possible!');
window.location = 'index.html'; /* Where you want to get directed */
</script>
<?php
} else { ?>
<script language="javascript" type="text/javascript">
alert('Sorry, your message was not sent! Please send an email to
hello.mariamulan#gmail.com instead.');
window.location = 'index.html'; /* Where you want to get directed
*/
</script>
<?php
}
?>
Verify that your form has the correct "name" attributes.
Your code is pretty dirty tho.
Related
I've tried searching high and low for a resolution to get a contact form to send e-mails for a few days now. Currently using Bootstrap. My host allows you to install FormTools but, although the enquirys are visible within FormTools, e-mails still do not arrive.
<form action="contact.php" method="post">
<div class="row">
<div class="form-group col-md-6">
<label for="exampleInputName">Full name</label>
<input type="name" name="cf_name" class="form-control" id="exampleInputName1" aria-describedby="nameHelp" placeholder="Eg. John Smith">
</div>
<div class="form-group col-md-6">
<label for="exampleInputEmail1">Email address</label>
<input type="email" name="cf_email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="example#example.co.uk">
</div>
<div class="form-group">
<label for="exampleSelect1">Subject of enquiry</label>
<select class="form-control" name="cf_subject" id="exampleSelect1">
<option>CCTV & Security</option>
<option>Door Access</option>
<option>Electrical</option>
<option>Networking</option>
<option>PC Repairs</option>
<option>Smart Thermostats</option>
<option>Other</option>
</select>
</div>
<div class="form-group">
<label for="exampleTextarea">Message contents</label>
<textarea class="form-control" name="cf_message" id="exampleTextarea" rows="3" placeholder="My enquiry..."></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
contact.php:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_subject = $_POST['cf_subject'];
$field_message = $_POST['cf_message'];
$mail_to = 'myemail#live.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message .= 'From: '.$field_name."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly if required.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to myemail#live.com');
window.location = 'contact.html';
</script>
<?php
}
?>
I've not had much experience with PHP and I've tried a few samples of code but I feel like I'm making no progress and although I've tried searching, I'm unsure how to implement certain solutions.
If someone could help or guide me (even point out a mistake I've probably made myself) that would be amazing.
Thanks for your help!
I don't know any PHP at all so I researched a couple hours to try and get my contact form to actually save and send the inputs to my email. As of right now, I'm getting an error Failed to load resource: the server responded with a status of 405 (). Here is the contact form code and also PHP file code. They are located in the root directory, named index.html and contact.php. I'm using the template html5up.net/strata.
<form method="post" action="contact.php" id="form" class="contact-form">
<div class="row uniform 50%">
<div class="6u 12u$(xsmall)"><input type="text" name="cf_name" id="name" placeholder="Name" /></div>
<div class="6u$ 12u$(xsmall)"><input type="email" name="cf_email" id="email" placeholder="Email" /></div>
<div class="12u$"><textarea name="cf_message" id="message" placeholder="Message" rows="4"></textarea></div>
</div>
<ul class="actions">
<li><input type="submit" value="Send Message" class="special"/></li>
</ul>
</form>
contact.php
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'simonchangwong#gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon#template-help.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>
I got a email.php from a website. How do you add an extra field to it, like phone number?
The original one I downloaded works fine.
Email comes as>>
From: Nuski
E-mail: my#email.com
Subject: Test
Message: Test mail
But when I add $field_phone = $_POST['cf_phone']; and $body_message = "Phone: ".$field_phone."\n";
The email comes without the Name of the sender. Like>>
Phone: 5559
E-mail: flex#f.com
Subject: reCaptcha test
Message: Email Test
Here is my HTML form
<form action="email.php" method="post">
<div class="col-sm-6">
<div class="form-group">
<input class="form-control" name="cf_name" type="text" id="name" placeholder="Your Name" required>
</div>
<div class="form-group">
<input class="form-control" name="cf_phone" type="text" id="phone" placeholder="Phone" >
</div>
<div class="form-group">
<input class="form-control" name="cf_email" type="email" id="email" placeholder="Your Email" required>
</div>
<div class="form-group">
<input class="form-control" name="cf_subject" type="text" id="subject" placeholder="Subject" required>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<textarea class="form-control btn-block" name="cf_message" id="comments" placeholder="Please include your address and item code for delivery" required></textarea><br>
</div>
<div class="form-group">
<input type="submit" class="btn btn-block" id="submit" value="Send Message">
</div>
</div>
<div class="col-sm-12">
<p class="contact-success">Your Message has been Successfully Sent!</p>
<p class="contact-error">Error! Something went wrong!</p>
</div>
</form>
Here is my email.php
<?php
$field_name = $_POST['cf_name'];
$field_phone = $_POST['cf_phone']; //added by me
$field_email = $_POST['cf_email'];
$field_subject = $_POST['cf_subject'];
$field_message = $_POST['cf_message'];
$mail_to = 'ask#aki.co';
$subject = 'Message from AkiYTeeS - '.$field_subject;
$body_message = "From: ".$field_name."\n";
$body_message = "Phone: ".$field_phone."\n"; //added by me
$body_message .= "E-mail: ".$field_email."\n";
$body_message .= "Subject: ".$field_subject."\n";
$body_message .= "Message: ".$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to ');
window.location = 'index.html';
</script>
<?php
}
?>
Replace:
$body_message = "Phone: ".$field_phone."\n"; //added by me
With:
$body_message .= "Phone: ".$field_phone."\n"; //added by me // You forgot a "." here
Also note that it's better to use \r\n unstead of just \r or \n as windows mac and linux each need a different of the 2 so by adding both it should always work
$body_message = "Phone: ".$field_phone."\n";
You have forgotten the concatenation operator in the above line.
use "." (dot)
$body_message .= "Phone: ".$field_phone."\n";
I'm having trouble to set this email form working properly
I have this contact.html:
<form mehtod="post" action="contact.php">
<hr />
<div class="row">
<div class="large-12 columns">
<label>Name
<input type="text" name="cf_name" placeholder="Name" />
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Email
<input type="text" name="cf_email" placeholder="Email" />
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Your message
<textarea name="cf_message" placeholder="Your message"></textarea>
</label>
</div>
</div>
<input class="button" type="reset" value="Clear">
<input class="button success expand" type="submit" value="Submit">
</form>
And this contact.php:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'my#email.com';
$subject = 'Personal Website Message from '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. I will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. There is a problem with your message, if keep aving trouble click on the Email me button above.');
window.location = 'contact.html';
</script>
<?php
}
?>
But when I upload this, and test it, nothing is sent to my email, why ? For me everything seems to be right ...
Your mail client may not be allowing sending emails without authorization. Unfortunately PHP's mail function does not support username + password authorization, so you have to look for third party extensions.
Please check the spelling of your 'method' attribute in the form element.
The PHP code you supplied specifies $_POST as the variable that contains data relevant to the form.
Your misspelling causes the server use the default 'get' method to post the data (for reference, see here)
Correcting the opening tag to the following should fix the issue:
<form method="post" action="contact.php">
I am able to receive email with all the information except for the email address that is entered in the form, and when I receive the email, I get "No Sender" instead of the person's name. I am new to PHP and have done several Google searches but can't figure out what I am doing wrong. Please help. Thanks
This is my HTML:
<?php include("parts/doctype.php"); ?>
<?php include("parts/header.php"); ?>
<div class="page-wrap">
<?php include("parts/nav.php"); ?>
<div class="page-grid">
<h1 class="site-section-title">Contact Us</h1>
<div class="content-area">
<form action="send-mail.php" method="POST">
<div class="contactName">
<h1 class="heading Name">Name</h1>
<label for="first-name" class="first_name">First Name</label>
<input type="text" id="first_name" name="first_name" placeholder="Jane" autofocus required>
<label for="last_name" class="last_name">Last Name</label>
<input type="text" id="last_name" name="last_name" placeholder="Doe" required>
</div> <!-- end name -->
<div class="number">
<h1 class="heading">Phone Number</h1>
<label for="number" class="phone">Phone number</label>
<input type="tel" id="number" pattern="[0-9]{10}" name="sender_phone" placeholder="(###)-###-####">
</div> <!-- end number -->
<div class="email">
<h1 class="heading">Email Address</h1>
<label for="email" class="emailLabel">Email</label>
<input type="email" name="sender_email" placeholder="janedoe#gmail.com" required>
</div>
<div class="message">
<h1 class="heading">Message</h1>
<label for="message"></label>
<textarea name="sender_message" id="message" cols="30" rows="10" spellcheck="true" required> </textarea>
</div>
<button id="submit" class="send">Send</button>
</form>
</div> <!-- end content area -->
</div> <!-- end grid -->
</div> <!-- end page wrap -->
And this is my PHP:
<?php
$mail_to = 'removed address but I know it goes here'; // specify your email here
// Assigning data from the $_POST array to variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'Its time for a cupcake Message from visitor ' . $first_name;
// Construct email body
$body_message = 'From: ' . $first_name . $last_name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Message: ' . $message;
// Construct email headers
$headers = 'From: ' . $first_name . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. Should your message require a responce we will get back to you shortly.');
window.location = 'contactus.php';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, try your message again. Should this still not work please contact use through facebook or twitter both links located on our home page.');
window.location = 'index.php';
</script>
<?php
}
?>
Your Label for should match your input id "First-Name" and "First_Name" won't match.
Here is a link:http://www.w3schools.com/tags/att_label_for.asp
Other than that it looks pretty good. If its not working as a post try using a get for your form method. That is easier to debug because the name value pairs will be passed on the url and you can at least verify that your php routine is sent the values correctly. If that looks good then try setting a break point in your php and stepping through it.
Hope this helps!