I am able to send plain text emails from my contact form using php, but I am not able to send the content as HTML. Thought I had added the headers correctly, but apparently there is still a problem.
This is the script I am using:
<?php
$to = 'test#hotmail.com';
$subject = 'From your Web';
$email = $_REQUEST['email'] ;
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
//if "email" is filled out, send email
if (!trim($_REQUEST['name']) == "" )
{
if (!trim($_REQUEST['message']) == "" )
{
//send email
$name = $_REQUEST['name'] ;
$message = $_REQUEST['message'] ;
$mail = '
<html>
<body>
<table border=1>
<tr>
<td>Name:</td>
<td>'.$name.' </td>
</tr>
<tr>
<td>Email:</td>
<td>'.$email.'</td>
</tr>
<tr>
<td>Msg:</td>
<td>'.$message.'</td>
</tr>
</table>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $mail, "From:" . $email, $headers);
//mail($to, $subject, "From: " . $name . "/" . $email . " Msg: ".$message, "From:" . $email);
echo 'Thank you!';
}
else{
echo 'No empty msg';
}
}
else{
echo 'This is not a name';
}
}
else{
echo 'No correct email';
}
?>
try this. code is un-tested. and it's been eons since i've written in php so i could have easily mangled some syntax in there.
$tacos = "crunchy";
$to = "tacos4eva#email.com";
$subject = "tacos";
$mail = "the tacos are $tacos today.";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: taco_master#email.com' . "\r\n";
$success = mail($to, $subject, $mail, $headers);
if ($success) echo "tacos were sent successfully";
else echo "tacos fell on the floor";
http://php.net/manual/en/function.mail.php
from the page...
When sending mail, the mail must contain a From header. This can be
set with the additional_headers parameter, or a default can be set in
php.ini.
<?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);
?>
Related
I have such code for send email:
$email = $_POST['message_email'];
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
before that I check whether the email is correct so it has to be.
Then I send an email:
$sent = wp_mail($newTo, $subject, $companyName . "\n" . $email . "\n" . $phoneNumber . "\n" . strip_tags($message) . "\n" . $outputMail, $headers);
and it doesn't work. I've tried change headers to:
$headers = 'From: My Name <myname#mydomain.com>' . "\r\n\\";
and then it works. Why my code does't work? I need to provide that email retreived from form.
Try like this:
****Ive edited the code****
$from = "your#mail.com";
$to = "to#mail.com";
$message = "Message";
$subject = 'subject';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
if(mail($from, $subject, $message, $headers)){
//success
}else{
//error
}
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
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..';
}
?>
The following code is sending an email from my website, but the email comes from cgi-mailer#kundenserver.de, how do i change this to the sender's email address, which i have given the variable $email:
<?php
if(isset($_POST['submit'])) {
$msg = 'Name: ' .$_POST['FirstName'] .$_POST['LastName'] ."\n"
.'Email: ' .$_POST['Email'] ."\n"
.'Message: ' .$_POST['Message'];
$email = $_GET['Email'];
mail('me#example.com', 'Message from website', $msg );
header('location: contact-thanks.php');
} else {
header('location: contact.php');
exit(0);
}
?>
Adding the header From: to my mail command seems to allow me to change the email address, but i can't work out how to do it to the variable.
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
For more reference
http://php.net/manual/en/function.mail.php
Declare the variable in the 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);
?>
Edit:
<?php
if(isset($_POST['submit'])) {
$msg = 'Name: ' .$_POST['FirstName'] .$_POST['LastName'] ."\n"
.'Email: ' .$_POST['Email'] ."\n"
.'Message: ' .$_POST['Message'];
$email = $_GET['Email'];
$headers = 'From: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('me#example.com', 'Message from website', $msg, $headers );
header('location: contact-thanks.php');
} else {
header('location: contact.php');
exit(0);
}
?>
Add this to the header
$headers .= 'From: ' . $from . "\r\n";
$headers .='Reply-To: $from' . "\r\n" ;
mail($to,$subject,$message,$headers);
It should set the sender.
where
$from= "Marie Debra <marie.debra#website.com>;"
$from = $_POST['email'];
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
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.