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.
Related
i'm trying to get a message to appear after my form has been submitted, i can get the form to submit and the page to then refresh but unsure how to add the message after the refresh.
i have no knowledge of PHP so sorry is its and obvious answer
thanks
my html & PHP if needed
<form action="action.php" method="POST" target="my_iframe" onsubmit="setTimeout(function(){window.location.reload();},10);">
<input type="text" id="name" name="name" placeholder="name" required="required">
<input type="email" id="email" name="email" placeholder="email" required="required">
<input type="tel" id="phone" name="phone" placeholder="phone">
<div class="form-row" style="display:none;"><input type="hidden" name="url" placeholder="URL"></div>
<textarea id="message" name="message" placeholder="message" style="height:200px" required="required"></textarea>
<input id="submit" type="submit" value="submit">
</form>
<iframe name="my_iframe" width="1" height="1" style="border:none"></iframe>
<?php
$to = 'zoeharrisondesign#gmail.com';
$subject = 'New Message Recieved!';
$from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$name = $_POST['name'];
//check honeypot
if( !empty( $honeypot ) ) {
echo 'There was a problem';
}
// 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";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = "$message \r\n |
From $name \r\n |
Tel: $phone";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your message has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Update your php sending email to this
if (mail($myEmail, $subject, $message)){
$success = "Message sent successful";
}else{
$success = "Message Sending Failed.";
}
Then add this php codes in your html codes add this code to display a message. Put it on top of your form html tag.
<?php
if (isset($success)){ echo "<div>" . $success . "</div>";}
?>
You may even add some styling on the message if you prefer.
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");
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);
}
?>
I have been searching for a working email php script, I have everything working except -
The email that is entered into the contact form receives the message instead of the pre-set recipient?
I have tried all sorts of tricks to try and get this to work, the only way I can get the mail to arrive in my web-mail account is if I type that address into the form as the senders email.
This is the 'html' form
<html>
<body>
<form method="post" action="new-mail.php">
<input type="text" name="name" id="name" placeholder="Name" />
<input type="text" name="contact" id="contact" placeholder="Email" />
<input type="text" name="subject" id="subject" placeholder="Subject" />
<textarea name="message" id="message" placeholder="Message"></textarea>
<input type="submit" class="button" value="Send Message" />
</form>
</body>
</html>
and this is the php mailer -
<?php
$subject = $_POST['subject'];
$message = $_POST['message'];
$email = 'net#webmail.address';
$who_sent = $_POST['contact'];
$headers = 'From: ' . $who_sent . "\r\n" .
'Reply-To: ' . $who_sent . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$name = $_POST['name'];
mail($email, $subject, $message, $name, $headers);
?>
When the mail is received at the intended senders email address it list's the recipient as being the intended send to address, and the from address is the web host 'user#server.web-host.net'
I am finding this a little confusing, I have tried setting the form id and name for the email field to different text, I have tried putting the recipients address directly into the mail(), and a good many other combinations too. For some reason it makes no difference to the outcome.
The host is 000webhost.com which is a linux based server if that makes any difference to this.
I have a HTML form with 4 id (name, email, message, subject), a js with all the variables declared and a PHP that should send the mail.
HTML
<form id="formail" method="post" action="">
<input type="text" id="nome" name="nome" value="" size="22" /><br />
<input type="text" id="email" name="email" value="" size="54" /><br />
<textarea id="messaggio" name="messaggio" rows="1" cols="55" style="resize: none;"></textarea><br />
<input type="text" id="subject" name="subject" value="" size="22" /><br />
<input type="submit" id="send" name="send" value="" style="cursor: pointer"/>
<br />
<div id="answer"></div>
</form>
This is the js
var valid = '';
var isr = ' requested.</h6>';
var name = $("#nome").val();
var mail = $("#email").val();
var subject = $("#subject").val();
var messaggio = $("#messaggio").val();
(follow controls about the name and mail, and the send function)
This is the php
$mail = trim($_POST['mail']);
$name = $_POST['name'];
$text = $_POST['messaggio'];
$subject = $_POST['subject'];
$ip = $_SERVER['REMOTE_ADDR'];
$to = "admin#test.com";
$message = "Username: ".$name.", ".$mail.".<br />";
$message .= "Subject: ".$subject.".<br />";
$message .= "Messaggio: <br />".$text."<br /><br />";
$message .= "IP: ".$ip."<br />";
$headers = "From: ".$mail." \r\n";
$headers .= "Reply-To: ".$mail." \r\n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";
if(mail($to, $subject, $message, $headers)){
echo "<h6>Message sent!</h6>";
}
I posted only the relevant code. When I click send I'll receive the mail, however the field "subject" is blank, as if the variables "subject" had been ignored.
Could you please help me? I'm starting to learn PHP but I'm still a newbie. Thank you.
Where is your mail function?
use following mail function:
mail($to-mail,$subject,$message,$header);
Two key points here:
a) Not sure exactly what function you're using to actually send the mail, but assuming PHP mail() you will need to use the $subject as the second parameter.
b) If you publish this on the open web you will be exploited as an open relay by spammers.
The attack you are vulnerable to is called 'header injection'.
In short, if I submit my 'mail' value as myemail#example.org%0ABcc:poorsap#example.com the script will add an extra line (the %0A is a linefeed), and submit to mail() with an extra Bcc header for however many emails I like (in this case poorsap#example.org).
Please take a read through this: http://www.securephpwiki.com/index.php/Email_Injection and consider using an alternate library to avoid this problem.