PHP - need help in php form element (Header part) - php

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..';
}
?>

Related

Mail Function is giving false return in PHP

I have tried sending email from a basic html form by php mail() function and It has given me false return
Code:
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$name = $_POST['first_name'];
$name1 = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company=$_POST['lead_object'];
$skype= $_POST['skype_id'];
$to = 'himanshu#webkidukan.com';
$subject = 'Advertiser Form Details';
$from = 'ssing648#gmail.com';
// 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 = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;"><?php $title . " " . $name . " " . "?>wrote the following:"<?php . "\n\n" . $name . "\n\n" . $name1 . "\n\n" .$email . "\n\n" .$phone . "\n\n" .$company. "\n\n" .$skype. "\n\n"?> </p>';
$message .= '</body></html>';
$t = mail($to, $from, $message, $headers,$subject);
var_dump($t);exit;
if ($t) {
echo "Mail Sent. Thank you " . $name . " .We will contact you shortly.";
}else{
echo "Failed to send email. Please try again later";
}
echo "<script type='text/javascript'>alert('Thanks for filling out our form! We will look over your message and get back to you soon.')</script>";
echo "<script> window.location.href = 'advertiser.php';</script>";
}
?>
I have this issue in three forms together , hence posting one for the understanding the mistake, that I am doing. Can anyone of you help me with the same.Or should I go for the SMTP mail option.Also I am sending the form details in mail to the user.So also check that the way to send the flyer is right or not.
There are some unwanted PHP tags while appending message. try like this
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;">'.$title." ".$name." wrote the following:"."\n\n".$name."\n\n".$name1."\n\n".$email."\n\n".$phone."\n\n".$company."\n\n".$skype."\n\n".'</p>';
$message .= '</body></html>';

Contact form php script sending spam to Gmail

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

How do I include sender's name and not just the email address while emailing message from user input form

I am trying to take inputs from a form on my website using a simple PHP script as given below:
<?php
$toemail = 'xyz#anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(mail($toemail, 'Subject', $message, 'From: ' . $email)) {
echo 'Your email was sent successfully.';
} else {
echo 'There was a problem sending your email.';
}
?>
This worked perfectly. Well, almost. The email I receive does not include the sender's name. I would want to see a normal email response (with name of the sender in the name column of inbox and I should be able to see the email address of the sender when I open the mail.)
So I made a few changes after looking up some threads like this:
<?php
$toemail = 'xyz#anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$header = 'From: '.$email_from;
if(mail($toemail, 'Subject', $message, $header)) {
echo 'Your email was sent successfully.';
} else {
echo 'There was a problem sending your email.';
}
?>
Email response is still same. What do I need to change ?
Try this format, note the spaces and the \r\n's, change your variables accordingly:
$email_headers = 'MIME-Version: 1.0' . "\r\n";
$email_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$email_headers .='From: YOURNAME <sender_email#yourdomain.com>' . "\r\n";
$email_headers .='Reply-To: reply_to_email#yourdomain.com' . "\r\n";
mail($recipient_address, $email_subject, $email_body, $email_headers);
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Quoted from http://php.net/manual/en/function.mail.php

How to add custom message in to my form mailer

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

PHP email submit form receiver not receiving the correct sendr email address

Im trying to figure out the problem with the PHP code for submit form, im doing for my friend. It is sending the emails through, but the problem is that the receiver gets a very odd email address. I am attaching an image to have a closer look.
My PHP code is:
<?php
$error = false;
$sent = false;
if(isset($_Post['name'])) {
if(empty($_Post['name']) || empty($_Post['email']) || empty($_Post['comments'])) {
$error = true;
} else {
$to = "linardsberzins#gmail.com";
$name = trim($_Post['name']);
$email = trim($_Post['email']);
$comments = trim($_Post['comments']);
$subject = "Contact Form";
$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:" . $name . "\r\n";
$mailsent = mail($to, $subject, $messages, $headers);
if($mailsent) {
$sent = true;
}
}
}
?>
Many thanks
It should be like this Sender <HIS#EXAMPLE.COM>:
$headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
Try adding the headers to the email, like this from PHP mail Manual example 2
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
If you want it to be from an email with a name, this would work
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
Add this to your headers
$headers .= "Reply-To: $replyEmail\r\n";
The From: header should include an email address as well as the name, something like
"From:My Display Name<mydisplayname#gmail.com>\r\n"
<?php
$error = false;
$sent = false;
if(isset($_Post['name'])) {
if(empty($_Post['name']) || empty($_Post['email']) || empty($_Post['comments'])) {
$error = true;
} else {
$to = "linardsberzins#gmail.com";
$name = trim($_Post['name']);
$email = trim($_Post['email']);
$comments = trim($_Post['comments']);
$subject = "Contact Form";
$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$name.' <sender#example.com>' . "\r\n";
$mailsent = mail($to, $subject, $messages, $headers);
if($mailsent) {
$sent = true;
}
}
}
?>
Try this. just change the header.
$headers .= 'From: '.$name.' <sender#example.com>' . "\r\n";
Hi This was not an error.. If you provide SENDER EMAIL then it will display the senders email address instead of this.. Otherwise it will take our hosting address.

Categories