PHP mail function is sending blank message body - php

I am aware there are other subjects with similar questions however their answered didn't solve my problem.
I developed a website and temporarily decided to go ahead and use PHP mail as opposed to other routes as it was the quickest and easiest (I am an amateur developer). It was working flawlessly for the past month with no issues, however 3 days ago, I started receiving emails with no message contents (these are not spam or just empty messages, I am aware that I should use validation etc).
I still successfully receive the email to my email account, it also contains the subject header being "Contact Form Request", however the body is empty i.e. $msg that should be populated aren't?
I switched over the placement of "Contact Form Request" and $msg and the $msg contents would be received in the email subject header so it is being populated, however the message body remains empty.
Any help would be appreciate.
contact.html
<div id="contact-form">
<form action="contact.php" method="post">
<div class="form-group">
<label for="inputName">Name *</label>
<input type="text" class="form-control" name="name" placeholder="Enter name" required>
</div>
<div class="form-group">
<label for="inputNumber">Contact Number *</label>
<input type="tel" class="form-control" name="phone" placeholder="Enter contact number" required inputMode="tel">
</div>
<div class="form-group">
<label for="inputEmail">Email *</label>
<input type="email" class="form-control" name="email" placeholder="Enter email" required>
</div>
<div class="form-group">
<label for="inputSubject">Subject *</label>
<input type="text" class="form-control" name="subject" placeholder="Enter subject" required>
</div>
<div class="form-group">
<label for="inputMessage">Message *</label>
<textarea type="text" class="form-control" name="message" placeholder="Enter message" rows="3" required maxlength="300"></textarea>
</div>
<p><small><strong>*</strong> These fields are required.</small></p>
<button type="submit" class="btn btn-send">Send</button>
</form>
</div>
contact.php
<?php
$webmaster_email = "test#drivingschool.co.uk";
$success_page = "thankyoucontact.html";
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$msg =
"You have received a message from " . $name . "\r\n\n" .
"Subject: " . $subject . "\r\n\n" .
"Message: " . $message . "\r\n\n" .
"Name: " . $name . "\r\n" .
"Phone: " . $phone . "\r\n" .
"Email: " . $email . "\r\n\n\n\n" .
"DISCLAIMER:\r\n\nThis e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.";
mail($webmaster_email, "Contact Form Request", $msg);
header("Location: $success_page");
?>

Your code looks OK on first inspection (not SAFE, but working).
My advise:
FIRST: Debug. Just show the message instead of actual mailing. Like hereunder:
<?php
$webmaster_email = "test#drivingschool.co.uk";
$success_page = "thankyoucontact.html";
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$msg =
"You have received a message from " . $name . "\r\n\n" .
"Subject: " . $subject . "\r\n\n" .
"Message: " . $message . "\r\n\n" .
"Name: " . $name . "\r\n" .
"Phone: " . $phone . "\r\n" .
"Email: " . $email . "\r\n\n\n\n" .
"DISCLAIMER:\r\n\nThis e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.";
echo "The message contains: $msg";
exit;
// mail($webmaster_email, "Contact Form Request", $msg);
// header("Location: $success_page");
?>
If your message contains what you expect, you must look into the email gateway. This is much harder.
I would start by making a script like this, call it testmail.php with only this inside, and see if that works.
<?php
mail('test#drivingschool.co.uk', "testing", "testing.\nDoes this arrive?");
?>
EDIT: Third thing to check:
You are sending a plain text mail. Maybe your email client only displays HTML email? Please check.
EDIT: Fourth suggestion:
I would try the mail utility itself from commandline to check if your PHP is acting strange.
So if you have SHELL access (BASH or whatever) try something like this:
mail -s "Testing mail" test#drivingschool.co.uk <<< 'Testing. Is this body visible?'
If THAT fails too, go have a chat with your hosting provider.
If that works: Something is wrong with your PHP in combination with sendmail.
Here is some background:
https://www.binarytides.com/linux-mail-command-examples/

Set from email with the header. You can also use content-type text/html if message in HTML format
$from ='testmail#mail.com';
$headers = "From:" . $from . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
#mail($webmaster_email,$subject,$message,$headers);
Hope this work

I have the same error, I think that the mail function interprets the text:
Message: $msg
as header, this may be a bug in PHP or the way SMTP works.
Here is my code:
function send_email($to, $subject, $message) {
$headers[] = 'from: kurs#jcubic.pl';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, implode(PHP_EOL, $headers));
}
send_email("mail#example.com", 'Subscription', "Email: $email");
it works fine when I remove the colon from the message.
send_email("mail#example.com", 'Subscription', "Email $email");

Related

PHP mail not showing senders email in from header frfom HTML form

Please could someone assist me in showing the senders email in the from header on the email. Please see code below, currently when i receive the email it shows the to email address in the from and the to.
HTML:
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP:
<?php
if(isset($_POST['submit'])){
$to = "bestwayhomemaintenance#gmail.com";
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
//$headers = "From:" . $from;
$headers = "From: $to \r\n";
$headers .= "Reply-To: $from \r\n";
//$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
//mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
You can't send from a gmail address unless you're sending through gmail's servers, which essentially means you can't use PHP's mail() function to do it. You may be able to try, but your messages will be marked as forgeries.
To set the envelope sender with the mail function, you need to use a -f parameter in the $additional_params parameter in the mail function.
Your script is vulnerable to header injection attacks, and it is also exploitable for cross-site scripting.
To avoid the forgery issue, I recommend sending directly through gmail, which mean you need to use SMTP, and the easiest way to do that is to use PHPMailer that you tagged this question with. Base your code on the examples provided with it.

Use PHP mail header to send email to more than one address

I'm new to this site so i'll try and explain myself as clearly as possible!
I'm currently in the process of creating a web form which when submitted sends an email to an account I have made on my server.
All is working fine but was wondering if there was a way i could add multiple accounts for the submitted form to send too.
My HTML:
<table style="width:100%">
<form action="form_complete.php" method="post">
<tr>
<th>
<input type="text" required placeholder="Name" name="name" />
</th>
<th>
<input type="text" required placeholder="Email" name="email" />
</th>
</tr>
<tr>
<th>
<input type="text" placeholder="Contact Number" name="mobile" />
</th>
<th>
<input type="text" required placeholder="Subject" name="subject" />
</th>
</tr>
<tr>
<td colspan="2"><textarea id="ta" name="message" required placeholder="Message"></textarea>
</tr>
</table>
<input id="submit" type="submit" name="submit" value="submit" />
</form>
form_complete.php
if(isset($_POST['submit'])){
$to = "rtozer#tandtcivils.co.uk"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$subject2 = "Copy of your form submission";
$message = "Name: " .$name . "\n\n mobile number: " . $mobile . ".\n\n Message:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
header ('Location: http://www.tandtcivils.co.uk/form_complete.php');
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
The code here is working correctly when sending an email to rtozer#tandtcivils.co.uk but i want to be able to add; dtozer#tandtcivils.co.uk to recieve a copy of the form submission also.
If you need any further information from me please leave a comment!
Thanks in advance,
Sam
You can have multiple TO addresses. For example:
$to = "rtozer#tandtcivils.co.uk," . $_POST['email'];
Or use CC or BCC headers:
$headers = "From:" . $from;
$headers .= "CC:" . $_POST['email'];
Or:
$headers .= "CC:rtozer#tandtcivils.co.uk," . $_POST['email'];
There are a number of ways to organize your email recipients, depending on whether you want them to be direct recipients, carbon copy recipients, or blind carbon copy recipients. Lots of examples are available in the PHP documentation.
The key benefit here, as opposed to what you attempted in your code, is that you need only send the email once as opposed to sending the same email multiple times. Just arrange multiple recipients on that one email.
Thanks to Reza Mamun try this:
//......
//...Other setting goes 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: My Name <myemail#example.com>'. "\r\n";
//Multiple CC can be added, if we need (comma separated);
$headers .= 'Cc: myboss1#example.com, myboss2#example.com' . "\r\n";
//Multiple BCC, same as CC above;
$headers .= 'Bcc: myboss3#example.com, myboss4#example.com' . "\r\n";
mail($to, $subject, $message, $headers);

Sending and Receiving mail through php contact form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
Recently, I have purchased a domain and hosting. I've also created a webpage having contact form in it. I want when the user wrote something in textarea of contact form and clicks submit then that message is sended to my gmail account and so that i can reply also to those messages. I've also used this script to do so but its not working.
This is sendemail.php file
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$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 = 'raunakhajela#gmail.com';//replace with your email
$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;
This is my contact form code in index.php
<div class="contact" id="contact">
<div class="container-3x">
<div class="block-header"> <!-- Blocks Header -->
<h2 class="title">Contact Us</h2>
</div>
<div class="block-content"> <!-- Blocks Content -->
<form action="sendemailphp" method="post" class="trigger-form-animate">
<input type="text" value="" id="name" name="name" placeholder="Name *" />
<input type="text" value="" id="email" name="email" placeholder="Email *" />
<input type="text" value="" id="website" name="website" placeholder="Website *" />
<textarea type="text" value="" id="message" name="message" rows="3" placeholder="Your Message *" ></textarea>
<div class="clr"></div>
<button>Submit Message</button>
</form>
</div>
</div>
</div>
I've also tried "http://www.mybloggertricks.com/2012/06/connect-your-website-email-address-to.html" this tutorial but its not working. Unable to find "Send through Gmail (easier to set up)" this option in gmail on adding another accounts windoww.
How can i do that?? Plzz hlp
You don't have a named subject form element which may explain why mail is failing and may very well be sent to Spam instead.
For example: <input type="text" name="subject">. Either add one of replace.
You will however need to make sure that this is filled out using a conditional statement
(see further below for an example).
$subject = #trim(stripslashes($_POST['subject']));
with
$subject = "Form submission";
You could also add this instead to your form:
<input type="hidden" name="subject" value="Form submission">
Either this or what I've outlined just above will work.
Many Email clients will either send mail to Spam or reject it altogether if a "subject" is missing, which is clearly missing from your code.
Checking if subject has been filled example:
if(isset($_POST['subject']) && !empty($_POST['subject'])){
// do someting
}
try this,both will work
<?php
$to = "usermailid#gmail.com";
$subject = "Welcome to";
$message = " Hi $username,<br /><br />
Thank you for signing up with us.<br />
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <yourmailid#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
if($mail)
{
$to = "admin#gmail.com";
$subject = "Following Customer Signed Up";
$message = " $username,Customer is signed up with us,<br /><br />
Customer Details:<br />First Name:$firstname<br/>Last Name:$lastname<br/>Email:$email<br/>
Phone:$phone<br/>Zip Code:$zip<br/>Message:$message_cust<br/><br/><br/>
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <yourmailid#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
}
?>

PHP mail only reaching certain clients

I've written a simple PHP HTML email form. That's all fine, it sends the email, the email doesn't get blocked. The problem with it is that only certain mail clients are receiving the emails: Gmail gets them fine, but another email account (used through a webmail client) doesn't, and neither do email apps I try. It appeared to work for a while after adding an X-Mailer header, but then stopped again; it also worked briefly when the recipients line was strangely formatted deliberately.
The form:
<form name="email" action="send.php" method="POST" id="mailform">
* From: <input type="text" name="from" required/><br/>
* To: <input type="text" name="to" required/><br/>
* Reply-To: <input type="text" name="reply" required/><br/>
Subject: <input type="text" name="subject" /><br/>
* Message:<br/>
<textarea name="email" rows="10" cols="100" id="message" required></textarea><br/>
<input type="button" name="convert" id="convert" value="Convert and Check" />
</form>
(the 'Convert and Check' button is there because I use Markdown to format the HTML email; that part is again working fine. It changes to a Send button once the MD is converted to HTML.)
The PHP:
$from = $_POST["from"];
$to = $_POST["to"];
$reply = $_POST["reply"];
$subject = $_POST["subject"];
$message = $_POST["email"];
$headers = "Content-Type: text/html" . "\r\n"
. "Reply-To: ".$reply . "\r\n"
. "From: ".$from . "\r\n"
. "X-Mailer: PHP/".phpversion() . "\r\n";
echo "<h3>Preview</h3><div class='content-container'>";
echo "<b>To:</b> ".htmlspecialchars($to)."<br/>";
echo "<b>Headers:</b> ".htmlspecialchars($headers)."<br/><hr/>";
echo "<div class='frame'>".$message."</div></div>";
echo "<br/><h3>Status</h3>";
$send = mail($to, $subject, $message, $headers);
if($send) {
echo "Your mail was successfully accepted for delivery.";
}
else {
echo "Sending of the email failed.";
}
Any ideas? It's got me confused - why do only some clients receive this?
because most of the email providers block emails from dynamic ips.

Send mail from homepage. What am I missing in my code?

I would like to be able to send mail from my homepage but it aint working as I want. i get a mail but it doesnt say from home. Just says Unknown. The thing I have this in two diffrent places. The other place I use other textareas and there I get everything in my mail and it works fine but this other place I am trying to work I only want them to add there email and then it should be sent with all info.
My code on my page:
<form id="subscribe" class="clearfix" method="post" action="get_mail.php">
<div class="field alignleft">
<input type="text" value="Enter your email" onclick="if(this.value=='Enter your email')this.value='';" onblur="if(this.value=='')this.value='Enter your email';" />
</div>
<div class="search-btn">
<input type="submit" value="" />
</div>
</form>
Then I have this php script
<?php
//-----------------------------------------------------
//-----------------------------------------------------
$address= "xxxxx.xxxxx#outlook.com";
//-----------------------------------------------------
//-----------------------------------------------------
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$website = $_REQUEST["website"];
$subject .= "You have an email from your web site (from $name)! \n\n";
$message_content = strip_tags($_REQUEST["message"]);
$headers = "From: $name <$email>\n";
$headers .= "Reply-To: $subject <$email>\n";
$message = "--$mime_boundary\n\n";
$message .= "You have an email from your web site: \n\n\n";
$message .= "Name: $name \n\n";
$message .= "Email: $email \n\n";
$message .= "Website: $website \n\n";
$message .= "Message: $message_content \n\n";
$message .= "--$mime_boundary--\n\n";
$mail_sent = mail($address, $subject, $message, $headers);
echo $mail_sent ? "Success, mail sent!" : "Mail failed";
?>
Two things:
Try replacing "\n" in $headers with "\r\n".
As far as I know, some hosts (I think GoDaddy or Hostgator do this for example) will override the "from" value in sent emails and change it to the one you have with them. This means that if you don't explicitly have "from#domain.com" in your hosting account you won't be able to send emails from that address and it will be always overridden. You have to contact your host to check this. I suggest also checking the script on another server if possible.
None of your form fields appear to have names - your code here:
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$website = $_REQUEST["website"];
requires form fields with the names "name", "email", and "website". For example, your "email" field needs to look like this:
<input type="text" name="email" ... />

Categories