Simple contact form not sending mail with PHP mail() function [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
HTML:
<form action="php/send-contact.php" class="contact-form" name="contact-form" method="post">
<div class="row">
<div class="col-sm-6 cols">
<input type="text" name="name" required="required" placeholder="Name*">
</div>
<div class="col-sm-6 cols">
<input type="email" name="email" required="required" placeholder="Email*">
</div>
<div class="col-sm-6 cols">
<input type="text" name="subject" required="required" placeholder="Subject*">
</div>
<div class="col-sm-12 cols">
<textarea name="message" required="required" cols="30" rows="5" placeholder="Message*"></textarea>
</div>
<div class="col-sm-12 cols">
<input type="submit" name="submit" value="Send Message" class="btn btn-send">
</div>
</div>
</form>
php/send-contact.php:
<?php
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'hello#domain.co.uk';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $body, 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<script>
alert("Thank you for getting in touch. We will contact you as soon as possible.");
</script>
<meta HTTP-EQUIV="REFRESH" content="0; url=../index.html">
</head>
The HTML alert activates and refreshes as it should, but it doesnt send any email...
I have tried numerous email address recipients.
Also, are there any special measures I should take into account (regarding the PHP elements) when adding Google ReCaptcha to this form?

So I have tested this, and I think it is doing what you want.
$name = htmlentities($_POST['name']);
$email_from = htmlentities($_POST['email']);
$subject = htmlentities($_POST['subject']);
$message = htmlentities($_POST['message']);
$email_to = 'Admin#Domain.com';//replace with your email
$headers = "From: webmaster#example.com" . "\r\n" . "CC: ". $email_from; //This adds a from field, as well as CC's the person submitting the request.
//Build the body of the eamil
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email_from . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'message: ' . $message;
$success = mail($email_to, "Contact from site X, regarding: ".$subject, $body,$headers);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<?php if($success){ //If the email was sent correctly?>
<script>
alert("Thank you for getting in touch. We will contact you as soon as possible.");
</script>
<?php header('Location: ../index.html'); }else{?>
<script>
alert("There was an error when sending the email, please try again later.");
</script>
<?php header('Location: ../index.html'); } //If the email falied?>
</head>
The other file (the html) remains the same. Simply replace your code in php/send-contact.php with this.

Related

How to replay to sender email from PHP instead of CGI-Mailer

I have tried too many different ways to get reply-to from the $_request[email] but it keeps sending the mails with the $from CGI- mailer, although all the body on my mail work´s fine..
I have tried too many ways but i can't find where is my problem.. i have looked at several answers to this question here but not any one fixes my problem.. this is my code.
<?php
$subject = 'Contacact from website';
$to = 'contact#myhosting.com';
$emailTo = $_REQUEST['email'];
// an email address that will be in the From field of the email.
$name = $_REQUEST['name'];
$email = $_REQUEST['email']; // i can't get this going to the reply-to section on the mail
$phone = $_REQUEST['phone'];
$msg = $_REQUEST['message'];
$email_from = $name.'<'.$email.'>';
$headers = "MIME-Version: 1.1";
$headers .= "Content-type: text/html; charset=utf-8";
$headers .= 'From: ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" .
'Reply-To: '. $fromEmail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$message .= 'Name : ' . $name . "\n";
$message .= 'Email : ' . $email . "\n";
$message .= 'phone : ' . $phone . "\n";
$message .= 'Message : ' . $msg;
if (#mail($to, $subject, $message, $email_from)) {
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
} else {
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';
}
?>
and this is my form:
<form name="contactForm" id='contact_form' method="post" action='email.php'>
<div class="row">
<div class="col-md-4">
<div id='name_error' class='error'>write your name</div>
<div>
<input type='text' name='name' id='name' class="form-control" placeholder="Name">
</div>
<div id='email_error' class='error'>Write a valid email</div>
<div>
<input type='email' name='email' id='email' class="form-control" placeholder="
email">
</div>
<div id='phone_error' class='error'>Write a phone number.</div>
<div>
<input type='tel' name='phone' id='phone' class="form-control" placeholder="Your name">
</div>
</div>
<div class="col-md-8">
<div id='message_error' class='error'>Please write your message here</div>
<div>
<textarea name='message' id='message' class="form-control"
placeholder="Message or quotation"></textarea>
</div>
</div>
<div class="col-md-12">
<p id='submit'>
<input type='submit' id='send_message' value='Enviar' class="btn btn-line">
</p>
<div id='mail_success' class='success'>We received your message :)</div>
<div id='mail_fail' class='error'>Please try again :/</div>
</div>
</div>
</form>
There are a few things wrong with what you posted.
Firstly, the first line for this block of code:
$message .= 'Name : ' . $name . "\n";
$message .= 'Email : ' . $email . "\n";
$message .= 'phone : ' . $phone . "\n";
$message .= 'Message : ' . $msg;
should not have a leading dot for $message .=, it should read as:
$message = 'Name : ' . $name . "\n";
Then this line:
if (#mail($to, $subject, $message, $email_from))
Since you're using $email_from as the last argument, mail() as you did for the other instance where you are sending mail, is using a valid From: with an E-mail address as it's coming "from", when the 2nd one does not contain that, you only declared it as $email_from = $name.'<'.$email.'>';.
What you will need to do is add the From: as you did for the first mailing instance.
Side note: The # symbol is an error suppressor. You might want to remove that during testing/development.
Consult the manual on the mail() function for more detail:
https://www.php.net/manual/en/function.mail.php

Issue with simple PHP Contactform

So I have some issues with my PHP Contactform on a HTML site.
Code below is what I have written in HTML. (that's not te problem)
<form action="sendmail.php" method="POST" class="contact-form">
<input type="text" placeholder="Amount" name="amount">
<input type="text" placeholder="Name" required name="name">
<input type="text" placeholder="Email Address" required name="email">
<div class="validation">
<button class="btn" name="submit">Send request</button>
// END HTML code. (BELOW is starts PHP).
$email_to = "domain#website.com";
$amount = $_POST["amount"];
$name = $_POST["name"];
$email = $_POST["email"];
$email_subject = "DOMAINNAME";
$headers = "From: " . $email . "\n";
$headers .= "Reply-To: " . $email . "\n";
$message = 'Name: ' . $name . ', email: ' . $email . ', amount: ' . $amount;
ini_set("sendmail", $email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);}
{
header("Location:https://website.com");
} else {
echo "There has been an error sending your comments. Please try later.";
}
What do I wrong? I only wanna receive the --> name, amount and mail.
Only when I press submit on my HTML site, it's send me to website.com/sendmail.php (HTTP ERROR 500).
Thank you guys.

Issues in send Email from PHP

I have this form and I am trying to send the contents of the contact us form to this mail scientology2016#gmail.com but it is not sending any mail for some reason and I can't figure out the error in this.
<form method="post" action="process.php">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control input-lg" name="name" placeholder="UserName">
</div>
<div class="form-group">
<input type="text" class="form-control input-lg" name="email" placeholder="E-Mail">
</div>
<div class="form-group">
<input type="text" class="form-control input-lg" name="usertel" placeholder="phone">
</div>
</div>
<div class="col-md-6">
<textarea class="form-control input-lg" name="subject" placeholder="Your Message"></textarea>
<button type="button" class="btn btn-primary btn-lg btn-block">Contact Us </button>
</div>
</form>
and this is the php code for sending mail which I wrote but it doesn't work
<?php
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$subject2 = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$tel = ($_GET['usertel']) ?$_GET['usertel'] : $_POST['usertel'];
$comment = ($_GET['body']) ?$_GET['body'] : $_POST['body'];
if ($_POST) $post=1;
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your comment.';
if (!$errors) {
$to = 'scientology2016#gmail.com';
$from = $name . ' <' . $email . '>';
$subject = 'Message from ' . $name;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name</td><td>' . $name . '</td></tr>
<tr><td>Subject</td><td>' . $subject2 . '</td></tr>
<tr><td>TelPhone</td><td>' . $tel . '</td></tr>
<tr><td>Email</td><td>' . $email . '</td></tr>
<tr><td>Message</td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';
$result = sendmail($to, $subject, $message, $from);
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
} else {
echo $result;
}
} else {
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo 'Back';
exit;
}
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;
}
?>
could anyone please help me in this case.
If you are using wamp server
first of all
open php.ini file in php folder and write or update
smtp_server=smtp.gmail.com
; smtp port (usually 25)
smtp_port=465
auth_username=scientology2016#gmail.com
auth_password=[MYPASSWORDHERE]

PHP Mail form showing blank responses

I have a PHP mail form from a Template that sends the email to me, but it appears all the variables are blank.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['yourname']));
$email = #trim(stripslashes($_POST['youremail']));
$subject = #trim(stripslashes($_POST['yoursubject']));
$message = #trim(stripslashes($_POST['yourmessage']));
$email_from = $email;
$email_to = 'kylef33#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
And here, the HTML:
<div class="col-sm-6">
<h1>
Contact Form
</h1>
<p>
Fill out the form to enquire directly and we will get back to you as soon as possible.
</p>
<div class="status alert alert-success" style="display: none">
</div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input name="yourname" type="text" class="form-control" required="required" placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input name="youremail" type="text" class="form-control" required="required" placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="yourmessage" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger btn-lg">Send Message</button>
<input type="hidden" name="yoursubject" value="Enquiry">
</div>
</div>
</div>
</form>
</div>
But the resulting email is:
Name:
Email:
Subject:
Message:
With none of the responses. Where am I going wrong?
Edit:
If I set the variables manually in the php file the message comes through fine:
$name = 'John';
$email = 'email#email.com;
$subject = 'Enquiry Form';
$message = 'Message here.';
I think the php file isn't getting the variables from the form correctly. How do I fix this?
I don't see the header of the email. try to add this:
$headers = "MIME-Version: 1.0\r\nFrom: $noReplay\r\nReply-To: $noReplay\r\nContent-Type: text/html; charset=utf-8";
where $noReplay is the email address to show to the receiver and the header is sent as a last parameter in mail() function.
Hope this helps!
Keep on coding,
Ares.
Try By use simple code and Set Content-type: text/html in headers it may useful:
$name = $_POST['yourname'];
$email = $_POST['youremail'];
$subject = $_POST['yoursubject'];
$message = $_POST['yourmessage'];
$email_from = $email;
$email_to = 'kylef33#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
//set the headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: <'.$email_from.'>' . "\r\n";
// Mail it
mail($email_to, $subject, $body, $headers);

PHP not sending email from html

I'm trying to make a form where the user introduces data and it gets submitted to e-mail.
However, it gets me to a blank page (file.php page), and the e-mail is not sent.
HTML code:
<form action="./sendmail.php" method="post">
<div class="log">
<input type="text" id="name" value="name" name="studname" class="form">
<input type="email" id="mail" value="mail#gmail.com" name="studmail" class="form">
<input type="tel" id="age" value="age" class="form" name="studage">
<input type="submit" value="submit!" id="submit">
</div>
</form>
And now here is my "sendmail.php" code:
<?php
/* STUDENT PARAMETERS */
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
mail('code#gmail.com', 'Message',
'NAME: ' . $studentName . '\nMAIL: ' . $studentMail . '\nAge: ' . $studentAge);
?>
How can I solve this and why is this not working? Thank you for your time.
I think you can use phpmailer class
#Telmo Vaz try this
<?php
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
$recipient='code#gmail.com';
$subject="Message";
$content = "New Message Form Contact us\n From: ".$studentName .", \n Email: ".$studentMail .",\n Age: ".$studentAge;
$headers = 'From: code#gmail.com' . "\r\n" .
'Reply-To: code#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$retval=mail($recipient, $subject, $content, $headers);
if( $retval == true ){do something;}
else{echo "Error Sending Message";}
?>
You need an email address to send the email from. The email won't be send from this address, but the selected address will be shown in the "From" section. Try this:
/* STUDENT PARAMETERS */
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
$to = "code#gmail.com";
$subject = "Message";
$message = "NAME: " . $studentName . "\nMAIL: " . $studentMail . "\nAge: " . $studentAge;
$from = $to;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

Categories