My contact form is not sending email - php

I've been trying to set up a contact form to send info to my email. I've checked the code and there's no syntax error or anything but I'm not receiving any test email. Can you please help me out?
Here's the HTML:
<!--Start of Contact Form-->
<div class="large-7 medium-10 small-12 medium-centered large-centered column">
<div class="row">
<form method="post" action="email2.php">
<input type="text" name="name" class="defaultText" title="your name">
<input type="text" name="email" class="defaultText" title="your email address">
<textarea name="comments1" class="defaultText" title="Tell us about your business"></textarea>
<textarea name="comments2" class="defaultText" title="How can we help?"></textarea>
<div class="large-7 medium-10 small-12 medium-centered large-centered column">
<input type="submit" name="send message" value="Send Message">
</div>
</form>
</div>
</div>
<!--End of Contact Form-->
and here's the script:
<?php
if (isset($_POST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$comments1 = $_POST['comments1'] ;
$comments2 = $_POST['comments2'] ;
mail("info#muzedimage.com", $name, $email, $comments1, $comments2
, "From:" . $email);
echo "<script>window.location = 'http://www.muzedimage.com'</script>";
}
else
//if "email" is not filled out,
{
echo "<script>window.location = 'http://www.muzedimage.com/contact'</script>";
}
?>
I would really appreciate the help guys.

You have passed wrong arguments to your mail function. Please read the mail() documentation.
The correct format is
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
In your case below is the correct code.
$to = 'info#muzedimage.com';
$email = $_POST['email'] ;
$subject = 'add some subject'; // no subject could lead to email being flagged as spam
$comments1 = $_POST['comments1'] ;
$comments2 = $_POST['comments2'] ;
$message = $comments1 . "\n\n" . $comments2;
$headers = "From: $email";
mail($to, $subject, $message, $headers);
You need to use comments how you want to. In this case I assumed that they both consist the main message.
Hope that helps. And always read documentation!!

Related

Passing variables after form submit PHP [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 2 years ago.
Is there an efficient way to pass variables from one page to another after form submission? I'm struggling to maintain the variables that are submitting on the form page to display them on the confirmation page after being redirected on submission.
Could it be the way i'm 'storing' the data with $_POST? Should I be using sessions? If I should how would I go about storing the $_POST in $_SESSION and being able to call it in the email template as a $variable-name format? Is using header(); to redirect inefficient in this manner and maybe redirecting via ajax? Not sure how I would approach that if so.
Form:
<form id="pricing-form-inquiry" action="<?php echo get_stylesheet_directory_uri(); ?>/pricing-form/form-handler.php" method="POST" role="form">
<div class="pricing-modal" id="modal1" data-animation="slideInOutLeft">
<div class="modal-dial">
<header class="modal-head">
<a class="close-modal" aria-label="close modal" data-close></a>
</header>
<section class="modal-body">
<div class="row">
<div class="col">
<input type="text" class="" placeholder="First Name" name="first-name" required data-error="First Name is required.">
</div>
<div class="col">
<input type="text" class="" placeholder="Last Name" name="last-name" required data-error="Last Name is required.">
</div>
</div>
<input type="email" class="" placeholder="Email Address" name="email" required data-error="Email Address is required.">
<input type="text" class="" placeholder="Company" name="company" id="company">
<input type="text" class="" placeholder="Phone Number" name="phone" id="phone">
<div class="row">
<div class="col text-center"></div>
</div>
</section>
<footer class="modal-foot">
<input type="submit" class="pricing-form-submit" value="Calculate" name="submit">
</footer>
</div>
</div>
</form>
form-handler.php
if(isset($_POST['submit'])) {
$first_name = filter_var($_POST['first-name'], FILTER_SANITIZE_STRING);
$last_name = filter_var($_POST['last-name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$company = filter_var($_POST['company'], FILTER_SANITIZE_STRING);
$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
$to = "email#email.com"; // Email Address to send lead to
$subject = "Subject Goes Here!"; // Subject of generated lead email
// HTML Message Starts here
$message = "<html><body><table style='width:600px;'><tbody>";
$message = "<tr><td style='width:150px'><strong>Name: </strong></td><td style='width:400px'>$first_name $last_name </td></tr>";
$message = "<tr><td style='width:150px'><strong>Email Address: </strong></td><td style='width:400px'>$email</td></tr>";
$message = "<tr><td style='width:150px'><strong>Company Name: </strong></td><td style='width:400px'>$company</td></tr>";
$message = "<tr><td style='width:150px'><strong>Phone Number: </strong></td><td style='width:400px'>$phone</td></tr>";
$message = "</tbody></table></body></html>";
// HTML Message Ends here
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: Company <from#email.com>' . "\r\n"; // User will get an email from this email address
// $headers .= 'Cc: from#email.com' . "\r\n"; // If you want add cc
if(mail($to,$subject,$message,$headers)){
// Message if mail has been sent
echo "<script>
alert('Mail has been sent Successfully.');
</script>";
header("Location: /pricing-confirm/");
} else {
// Message if mail has been not sent
echo "<script>
alert('EMAIL FAILED');
</script>";
}
}
To pass your variables onto the pricing-confirm page, you could pass the variables in your header() function like so
header("Location: /pricing-confirm.php?name=" . $first_name);
Once on your pricing-confirm.php page, you can grab the variables from the query string
if(isset($_GET['name']) {
$name = $_GET['name'];
}
If you want to pass multiple variables at once, you can either use & in the query string, or use urldecode with an array like this
$arr = [
"firstname" => $first_name,
"lasttname" => $last_name,
]
header("Location: /pricing-confirm.php?userdetails=" . urlencode(serialize($arr)));
If you have used serialize, you can get the values in the array like this
$queryArr = unserialize(urldecode($_GET['userdetails']));
you can then access them with their array key, like so
if(isset($_GET['userdetails']) {
$arr = $_GET['userdetails'];
if(isset($arr['firstname']) {
$firstName = $arr['firstname'];
}
}

my website contact form being used to send spoof emails

My hosting provider has contacted me and said one of the sites I have designed is sending spoof emails. Done a little bit of research but I still don't really understand how/what are they are doing to send these spoof emails. However more importantly how should I approach this, would it help if I try and put one of these 'captcha' things in place on the contact form or should I change the code I have on my site. Which is shown below:
<?php
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "***";
$Subject = "Message to A R C Products";
$Name = Trim(stripslashes($_POST['Name']));
$Address = Trim(stripslashes($_POST['Address']));
$Telephone = Trim(stripslashes($_POST['Telephone']));
$Message = Trim(stripslashes($_POST['Message']));
// prepare email body text
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$Message = "
Name:$Name
Address: $Address
Telephone: $Telephone
$Message";
// send email
$success = mail($EmailTo, $Subject, $Message, $headers);
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
<h2><strong>Contact Us</strong></h2>
<form method="POST" action="contact.php">
<br/>
<p style="margin-top: 0;">Fields marked (*) are required</p>
<p style="margin-top: 0;">Your Email:* <br/>
<input type="text" name="EmailFrom">
<p style="margin-top: 0;">Name:* <br/>
<input type="text" name="Name">
<p style="margin-top: 0;">Address:<br/>
<input type="text" name="Address">
<p style="margin-top: 0;">Telephone:<br/>
<input type="text" name="Telephone">
<p style="margin-top: 0;">Message:*<br/>
<TEXTAREA NAME="Message" ROWS=6 COLS=40>
</TEXTAREA>
<p style="margin-top: 0;"><input type="submit" name="submit" value="Submit">
</form>
Take a look on filter_input to clean your input data. Also i would not use the email from the form as a from address.
$EmailFrom = filter_input(INPUT_POST,'EmailFrom', FILTER_SANITIZE_EMAIL);

Having trouble with my contact form

I'm having a little trouble with my contact form. It's sending emails to my server but somehow it's not taking the information from the "email" field. Instead, what I get in the "From:" field is my address on the server like this: "muzedima#box693.bluehost.com".It's not taking the info from the "name" field either. What did I do wrong? Also, I'm trying to get the subject line to say the name of the sender, but I failed miserably, is it possible?
Here's the HTML:
<div class="large-7 medium-10 small-12 medium-centered large-centered column">
<div class="row">
<form method="post" action="email2.php">
<input type="text" name="name" class="defaultText" title="your name">
<input type="text" name="email" class="defaultText" title="your email address">
<textarea name="comments1" class="defaultText" title="Tell us about your business"></textarea>
<textarea name="comments2" class="defaultText" title="How can we help?"></textarea>
<div class="large-7 medium-10 small-12 medium-centered large-centered column">
<input type="submit" name="send message" value="Send Message">
</div>
</form>
</div>
</div>
And here's the PHP script:
<?php
if (isset($_POST['email']))
//if "email" is filled out, send email
{
//send email
$to = 'info#muzedimage.com';
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$subject = 'Inquiry from $name';
$comments1 = $_POST['comments1'] ;
$comments2 = $_POST['comments2'] ;
$message = $comments1 . "\n\n" . $comments2;
$headers = "From: $name, $email";
mail($to, $subject, $message, $headers);
echo "<script>window.location = 'http://www.muzedimage.com'</script>";
}
else
//if "email" is not filled out,
{
echo "Oops!, you forgot your email!";
}
?>
Thanks guys!
PHP variables are not evaluated inside of single quotes, only double quotes, therefore change the following:
$subject = 'Inquiry from $name';
//to
$subject = "Inquiry from $name";
Also for the email portion, you're not doing anything with it... so do something with it.
$message = $comments1 . "\n\n" . $comments2 . "\n\n" . $email;
Now it sends the email as well.
Ohgodwhy already pointed out one of the issues with your code, so I won't bother repeating it.
In regards as to why you're receiving the From: as "muzedima#box693.bluehost.com" is because of this line:
$headers = "From: $name, $email";
It needs to be like this:
$headers = "From: $name <$email>\r\n";
The $email variable must be encapsulated by < and > and it's best to end it with \r\n
Tested and working with both Ohgodwhy's answer and mine.

HTML form to PHP script not sending mail

I'm using a template to create my website and it came with a contact page and form all set out but it did not have a php contact script so I wrote that up and set it as the action on the html form and it still won't send me anything to my email... which I have set up through gmail ( i changed the domain email exchange DNS to the gmail settings)
in the html contact form i have the following code:
<div id="contact_form"><form method="post" name="contact" action="contact-form-handler.php">
<label for="name">Name:</label> <input type="text" id="name" name="name" class="required input_field" /><div class="cleaner h10"></div>
<label for="email">Email:</label> <input type="text" id="email" name="email" class="validate-email required input_field" /><div class="cleaner h10"></div>
<label for="subject">Subject:</label> <input type="text" name="subject" id="subject" class="input_field" /><div class="cleaner h10"></div>
<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea><div class="cleaner h10"></div>
<input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" />
<input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" />
</form>
and the contact-form-handler.php contains this code bellow to process the html form:
<?php
$to = 'info#jamesreborne.co.uk';
$to .= 'damgxx#gmail.com';
// Assigning data from the $_POST array to variables
$name = $_post['sender_name'];
$email = $_post['sender_email'];
$subject = $_post['sender_subject'];
$text = $_post['sender_text'];
// Construct email subject
$content = 'www.jamesreborne.co.uk Message from visitor ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $email. "\r\n";
$body_message .= 'Subject: ' . $subject . "\r\n";
$body_message .= 'Message: ' . $text;
// Construct email headers
$headers = 'From: ' . $email . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
mail($to, $content, $body_message, $headers);
$mail_sent = mail($to, $content, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php }
else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator info#jamesreborne.co.uk');
window.location = 'contact.html';
</script>
<?php
}
?>
if anyone can help that would be great, thanks
$subject = $_POST['subject'];
$text = $_POST['text'];
Also there is no form field for name and email. Add that.
There is also an error in the part where you set recipients' emails - they are not separated so the $to variable is info#jamesreborne.co.ukdamgxx#gmail.com. It should me more like this:
<?php
$to = 'info#jamesreborne.co.uk';
$to .= ', damgxx#gmail.com';
First your $to string adds two emails in wrong way,
it should be:
$to = 'info#jamesreborne.co.uk, ';
$to .= 'damgxx#gmail.com';
Even if you correct that you wont get subject and message value. AFAIK $_POST is case sensetive(please correct if wrong). So you will have to make it $_POST not $_post.
Then the names of the inputs in html form and in php code are not matching. They should be:
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$text = $_POST['text'];
If a input field in form is subject, then
$subject = $_POST['subject'];
NOT
$subject = $_POST['sender_subject'];
EDIT:
If you are still not getting email, then your server might not have mail server installed.
Install postfix and try.

Contact form. How do I get the name along with the email?

It's my first time trying to make a contactform. And I've got a few problems
It's works, I get the email, but I don't get the name the name field with me in the email.
HTML:
<form method="post" name="contactform" action="scripts/contact.php">
<h3>Name</h3>
<input type="text" name="name">
<h3>Email Address</h3>
<input type="text" name="email">
<h3>Message</h3>
<textarea name="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form">
</form>
PHP:
<?php
$to = "name#domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $name, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>
$name is my problem. I've I have it in, the email comes from hostmaster#domane.com, If I delete it, everything works fine. But I wan't the name to be sent to me. How?
Or should I do it completely different?
Also, if you leave all the fields blank, the "user" doesn't get any error message, and a blank email is sent to me.
Hope you can help me. :)
Michael Berkowski is correct. What you'll need to do is add the name to your message's body (not in the sense of the input name= attribute, rather the body of the email).
Something like this:
<?php
$to = "name#domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$body = "Name: $name\r\n";
$body .= "Message: $message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>
Revised:
HTML:
<form method="post" name="contactform" action="scripts/contact.php">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<label for="email">Email Address</label>
<input type="text" name="email" id="email" />
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$body = "Name: $name\r\n";
$body .= "Message: $message";
$to = "name#domane.com";
$from = "automailer#mydomainname.com (Website Automailer)";
$subject = "Contact Us";
$headers = "From: $from\r\n" .
"Reply-To: $email ($name)";
$sent = mail($to, $subject, $body, $headers) ;
if($sent) { echo "Your mail was sent successfully"; }
else { echo "One of the field are not filled as requirred"; }
?>
You should read the mail function documentation on php.net.
Have a look at the function signature:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Now you're placing $name as the "$additional_headers" argument. You should pass $name and any extra relevant data in a $message argument instead.
Having that said, here's the correct code to send a message:
$sent = mail($to, $subject, "A message from $name: $message", $headers);
You should read more about how email messages are constructed. Instead of just putting a user defined message in there you probably want to specify some email headers, containing a more beautiful FROM: and the like...

Categories