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

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

Related

contact.php does not send email fields

My server receives emails but none of the fields are shown.
All fields are empty in the email? Do you have any suggestions? I have tried everything that I know but no results. Form fields does not pass to the php
<?php
// variables start
$name = "";
$email = "";
$message = "";
$name = trim($_POST['contactNameField']);
$email = trim($_POST['contactEmailField']);
$message = trim($_POST['contactMessageTextarea']);
// variables end
// email address starts
$emailAddress = 'mail#domain.com';
// email address ends
$subject = "Message From: $name";
$message = "<strong>From:</strong> $name <br/><br/> <strong>Message:</strong> $message";
$headers .= 'From: '. $name . '<' . $email . '>' . "\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";
//send email function starts
mail($emailAddress, $subject, $message, $headers);
//send email function ends
?>
for this form
<form action="php/contact.php" method="post" class="contactForm" id="contactForm">
<div class="form-field form-name">
<label class="contactNameField color-theme" >Name:<span>(required)</span></label>
<input type="text" name="contactNameField" id="contactNameField" />
</div>
<div class="form-field form-email">
<label class="contactEmailField color-theme" >Email:<span>(required)</span></label>
<input type="text" name="contactEmailField" id="contactEmailField" />
</div>
<div class="form-field form-text">
<label class="contactMessageTextarea color-theme" >Message:<span>(required)</span></label>
<textarea name="contactMessageTextarea" id="contactMessageTextarea"></textarea>
</div>
<div class="form-button">
<input type="submit" class="btn bg-highlight text-uppercase font-900 btn-m btn-full rounded-sm shadow-xl contactSubmitButton" value="Gönder" />
</div>
Thank you

how to send email message with php and html

I am trying to send a message from HTML form. But for some reason I am not getting anything. Could someone please help me ?
Here is my HTML form:
<form method="post" action="subb.php">
<div class="field half first">
<label for="Name">Name</label>
<input type="text" name="Name" id="name" />
</div>
<div class="field half">
<label for="Email">Email</label>
<input type="text" name="Email" id="email" />
</div>
<div class="field">
<label for="Message">Message</label>
<textarea name="Message" id="message" rows="5"></textarea>
</div>
<ul class="actions">
<button type "submit" name="submit" id="submit" class="button submit">Send message</button>
and the PHP:
<?php
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
$to = "combatstriker111#gmail.com";
$subject="new message";
mail($to , $subject , $Message, "From :" . $Name . $Email);
echo "Your message has been Sent";
?>
I have named the PHP file subb.php and listed them both in the same directories but its still not working for some reason. Any help is very much appreciated.
Something in your code was wrong mail($to , $subject , $Message, "From :" . $Name . $Email);
Mail function SYNTAX :
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
So,
<?php
if(isset($_POST['submit'])) {
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = "Name : ".$Name."<br />"
$Message .= $_POST['Message'];
$to = "combatstriker111#gmail.com";
$subject="new message";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Name <$to>' . "\r\n";
$headers .= 'From: $Name <$Email>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
if(mail($to, $subject, $Message, $headers)) {
echo "Your message has been Sent";
} else {
echo "Mesage Error";
}
}
?>
Note : Use any mail library for prevent vulnerable to header injection like PHPMailer
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];;
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'info#fullertoncomputerepairwebdesign.com';
$subject = 'Message From Website';
$headers = 'From: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'Reply-To: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$themessage = "Name: ".$name."</br>Email: ".$email."</br>Phone: ".
$phone."</br>Subject: ".$subject.
"</br>Message: ".$message;
//echo $themessage;
if(mail($to, $subject, $themessage, $headers)){
echo "message sent";
header( 'Location: http://www.fullertoncomputerepairwebdesign.com/contactus.php?smresponse=Message Sent' ) ;
}else{
echo "we have a error charlie!";
}
;
test this out and if it doesn't work it means your hosting blocks mail function.

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

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.

How can i add a default message with php mail function?

I have created an html form that after the user clicks "send" redirects to mail.php which contains php mail function that works and sends the desired message.
Is there any way to also add a default message for example "This was sent from the website" to the email?
Form from index.html
<form method="post" action="mail.php">
<div class="form-style">
<h1 class="formh1">Full Name</h1>
<input type="text" id="name" name="name" placeholder="Full name" required>
<h1 class="formh1">Email address</h1>
<input type="email" id="email" name="email" placeholder="Email address" required>
<h1 class="formh1">Message</h1>
<textarea rows="4" cols="50" id="message" name="message" placeholder="Give us your thought" required></textarea>
<div class="button-form">
<input type="submit" name="send" value="Send" />
</div>
</div>
</form>
Mail.php
<?php
if (isset($_POST['send'])) {
$from = 'myemail'; // Use your own email address
$subject = 'The following message was sent from the website';
$message = 'Fullname: ' . $_POST['name'] . "\r\n\r\n";
$message .= 'Email address: ' . $_POST['email'] . "\r\n\r\n";
$message .= 'Message: ' . $_POST['message'];
$name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
$details = trim(filter_input(INPUT_POST,"message",FILTER_SANITIZE_SPECIAL_CHARS));
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
header('Location: index.html');
if ($email) {
$headers .= "\r\nReply-To: $email";
}
$headers = "From: ".$_POST['email']."\r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8';
$success = mail($from, $subject, $message, $headers);
}
?>
Just add the default email your would like to send to the $message variable:
$message = 'This is my default message lalalalalal'
You just amend before, or after... wherever you want to inject that content.
When you use $var .= "something added!"; - You're appending/concatenating additional text in this case.
When you use $var = "something added!"; - w/o The .= you've reset the variable to the new string.
Think of .= being the same as $message = $message . "String being added to message"
$from = 'myemail'; // Use your own email address
$subject = 'The following message was sent from the website';
#EXAMPLE HERE - Also note I added htmlspecialchars
#These will convert any misc. characters to html readable content.
#Test with it, see if it fits. Just don't add it to $email = trim...
$message = "Your friend " . htmlspecialchars($_POST['name']) . " has sent you a message from www.mywebsite.com"\r\n\r\n;
$message .= 'Fullname: ' . htmlspecialchars($_POST['name']) . "\r\n\r\n";
$message .= 'Email address: ' . htmlspecialchars($_POST['email']) . "\r\n\r\n";
$message .= 'Message: ' . $_POST['message'];
$name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
$details = trim(filter_input(INPUT_POST,"message",FILTER_SANITIZE_SPECIAL_CHARS));
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

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);

Categories