I created a php script (for a contact form) to send emails to my Gmail account.
If I use the sender email in the header ($headers = "From: " . $email;), Gmail reports the received message as spam.
If I don't use the email in the header (e.g. the sender name $headers = "From: " . $name;) the message is not reported as spam.
Do you have any suggestion to let me use the email in the header?
Thanks!
<?php
/* Check if the url field is empty (antispam) */
if ($_POST['leaveblank'] != '' or $_POST['dontchange'] != 'http://') {
$name = $_POST['name'];
$faillink = "xxx.php";
header("Location: $faillink");
} else {
$name = $_POST['name'];
$email = $_POST['email'];
$subject_prefix = "[ContactForm]: ";
$subject = $subject_prefix . $_POST['subject'];
$message = $_POST['message'];
$to = "myemail#gmail.com";
$body = "From: " . $name . "\n";
$body .= "Email: " . $email . "\n";
$body .= "Message: " . $message . "\n";
$headers = "From: " . $email;
$oklink = "yyy.php";
$faillink = "xxx.php";
if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) {
header("Location: $faillink");
}
$retmail = mail($to, $subject, $body, $headers);
if ($retmail) {
header("Location: $oklink");
} else {
header("Location: $faillink");
}
}
?>
I solved the issue as Iain suggested so I replaced the mail headers as follows:
$headers = "From: " . "noreplay#mydomain.com" . "\r\n";
$headres .= "Reply-To: " . $email . "\r\n";
Related
I have a fully working form that sends emails on submit, I would like to have some kind of alert if the email is sent successfully. I saw a lot of solutions with jquery or other libraries but I wanna do it with php
<?php
if(isset($_POST['name']) && $_POST['email'] !='') {
$userName = $_POST['name'];
$lastName = $_POST['surname'];
$email = $_POST['email'];
$phoneNumber = $_POST['phonenumber'];
$messageSubject =$_POST['subject'];
$catalog =$_POST['select_catalog'];
$inputsValue =$_POST['radioSet1'];
$inputsValue2 =$_POST['radioSet2'];
$inputsValue3 =$_POST['radioSet3'];
$inputsValue4 =$_POST['radioSet4'];
$to = "levchegochev#gmail.com";
$body = "";
$body = "Akademija Za dizajn"."\r\n";
$body .= "Ime: ".$userName. "\r\n";
$body .= "Prezime: ".$lastName. "\r\n";
$body .= "Email: ".$email. "\r\n";
$body .= "Telefonski broj:".$phoneNumber."\r\n";
$body .= "Nacin na plakanje: ".$catalog. "\r\n";
$body .= "Opcii: ".$inputsValue. "\r\n".$inputsValue2. "\r\n" .$inputsValue3. "\r\n" .$inputsValue4."\r\n";
mail($to,$messageSubject, $body);
if(mail = true ) {
echo 'test'
}
}
?>
You can write:
if(mail == true ) {
echo '<script>alert("Email send!");</script>';
}
you can try this
if(mail) echo '<script>alert("message")</script>';
i am totally lost with why i am getting this error at this code:
$finalmessage = "
From:$_POST['name']
Email:$_POST['email']
Message:$_POST['message']
";
Here is the entire mail php code below:
<?php
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$message = $_POST['message'];
$support_address = "info#bkslegal";
$headers = "From: ".$email;
$header2 = "From: ".$support_address;
$finalmessage = "
From:$_POST['name']
Email:$_POST['email']
Message:$_POST['message']
";
if ( $name == "")
{
}
else
{
mail("$support_address","finalmessage",$headers);
$result = "Your message has been sent succesfully!"
mail("$email","Thank you for contacting us!","We will soon be in contact with you!",$header2);
}
?>
I see that you wrote your parameters wrong. You wrote:
mail("$support_address","finalmessage",$headers);
It should be:
mail ($support_address, $subject, $message, $headers);
and remove this line
//mail("$email","Thank you for contacting us!","We will soon be in contact with you!",$header2);
PHP mail SYNTAX is:
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Final code :
<?php
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$support_address = "info#bkslegal";
if($name == "") {
//show somting error message
}
else
{
$Message = "From:".$name."<br />";
$Message .= "Email:".$email."<br />";
$Message .= "Message:". $_POST['message'];
$to = $support_address;
$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
I'm not versed in PHP and had this form built for me. It works 95% of the time, but sometimes the reply-to email will be johnsmith#hadrian.lunarpages.com
I'm reading up a similar issue on this page, but not exactly sure what to change in my form...
<?php
if (isset($_REQUEST['btnSubmit-group'])) {
$date = $_REQUEST['date'];
$time = $_REQUEST['time'];
$count = $_REQUEST['count'];
$name = $_REQUEST['name'];
$organization = $_REQUEST['organization'];
$email = $_REQUEST['email'];
$grouptype = $_REQUEST['grouptype'];
$phone = $_REQUEST['phone'];
$upgrademus = $_REQUEST['upgrademus'];
$upgradeowo = $_REQUEST['upgradeowo'];
$comments = $_REQUEST['comments'];
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
if ($count == "" || $name == "" || $email == "" || $phone == "") {
echo "All fields are required, please fill out again. ";
} else {
$to = 'info#example.com';
$subject = "Group Tour Inquiry from $name ($date, $count people)";
$from = $to;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = nl2br("Private Tour:<br>" .
"<b>Name:</b> $name\r\n" .
"<b>Email:</b> $email \r\n" .
"<b>Phone:</b> $phone \r\n" .
"<b>Organization:</b> $organization\r\n" .
"<b>Date:</b> $date\r\n" .
"<b>Time:</b> $Time\r\n" .
"<b>Count:</b> $count\r\n" .
"<b>Include:</b> $upgrademus\r\n" .
"<b>Include:</b> $upgradeowo\r\n" .
"<b>Comments:</b> $comments\r\n" .
"");
// 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";
$headers .= "From: $name<$email> \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to, $subject, $body, $headers)) {
echo "<div class=\"thankyou\"></div><meta http-equiv='refresh' content='0;url=http://www.example.com/private-group-tours'>";
}
}
} else {} ?>
You're trying to send e-mail's to your self FROM the person whose entering this in, essentially attempting to spoof their email, and sometimes this will(should) get blocked. more can be read here - problem with php mail 'From' header
Change:
$headers .= "From: $name<$email> \r\n";
$headers .= "Reply-To: $email \r\n";
to
$from = 'Admin#mysite.com';
$headers .= "From: $from\r\n";
$headers .= "Reply-To: $from \r\n";
and then edit that static $from variable. Then take the users email out of the BODY of the email you receive, and send them an email.
I am a beginner in php.
I am trying php to send mail.
My mail goes to mail client but it does not show the header part well as I wanted.
I am using the following codes ----
<?php
//validation expected data exists
if(!isset($_POST["name"]) ||
!isset($_POST["city"]) ||
!isset($_POST["email"]) ||
!isset($_POST["phone"]) ||
!isset($_POST["message"])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
//Subject
$name = $_POST["name"];
$subject = "NO REPLY";
$email = $_POST["city"];
$phone = $_POST["email"];
$website = $_POST["phone"];
$message = $_POST["message"];
$header = "from: $name <$email>";
$to = 'info#mishmihillsadventure.in';
$send_contact=mail($to,$subject,$message,$header);
//Check, if message sent to your email
// Display message "We've recived your information"
if($send_contact){
echo "We've received your contact information";
}
else{
echo "ERROR";
}
?>
$email = $_POST["city"];
$phone = $_POST["email"];
Is this really what you want? Shouldn't it be:
$email = $_POST["email"];
And try the following headers:
$header = 'From: ' . $name . '<' . $email . '>' . "\r\n";
Use (atmost) following headers while sending mail via PHP -
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= "From: $name <$email>" . "\r\n";
//If want to `CC` someone add
$header .= 'Cc: abc#email.com' . "\r\n";
Using variables in Double quotes is fine.
You can try something like in code mentioned below,
<?php
$to = 'test#to.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: test#from.com' . "\r\n" .
'Reply-To: test#from.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo 'Msg send..';
} else {
echo 'Msg Not send..';
}
?>
I want to add "This message came from etcetc.com" in the e-mail form in the e-mail body itself. Hope this make sense..
my sent_email.php
<?php
$email_to = 'test#test.org';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email;
if(mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
} else {
echo 'failed'; // ... or this one to tell it that it wasn't sent
}
?>
Like this:
<?php
$email_to = 'test#test.org';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = "This message came from etcetc.com \r\n".$_POST['message'];
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email;
if(mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
} else {
echo 'failed'; // ... or this one to tell it that it wasn't sent
}
?>
All you need to do is add your text to the end of $message.
Change:
$message = $_POST['message'];
to
$message = $_POST['message'] . "\n\nThis message came from etcetc.com";
Add message before and after as you wish by
$message = "This is before posted message\r\n";
$message .= $_POST["message"];
$message .= "\r\nthis is after the posted message";