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,
);
Related
Can anyone suggest corrections in these few line of code. I am very much disturbed that it is not working and time elapsing. Please help me with corrections or suggest a new one.
<?php
$email_to = 'info#khawabnama.com';
$subject = "Contact US";
$name =$_POST['flname'];
$email_from = $_POST['email'];
$message = $_POST['message'];
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success=#mail($email_to, $subject, $body, $headers);
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
// $success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
//header("Location: index.php");
//header('Location: http://www.khawabnama.com/index.php');
//die;
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);
?>
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'm having some issues in understanding how to send custom headers with the php mail() function.
What's wrong with this code?
<?php
function send_email($name, $lastname, $from, $subject, $message){
$to = 'to_adress#gmail.com';
$headers = 'To: ' . $to .
'\r\nFrom: ' . $from .
'\r\nSubject:' . $subject;
mail($to, $subject, $message, $headers);
print 'Email sent';
}
send_email('jhon', 'doe', 'from_adress#gmail.com', 'subject', 'message');
?>
It gives me no php error, and the email does not arrive.
'\r\n' is not the same as "\r\n". Try something like this instead:
$headers = 'To: ' . $to . "\r\n"
. 'From: ' . $from . "\r\n"
. 'Subject: ' . $subject;
However you don't need to repeat the To: and Subject: headers, so just using this should do:
$headers = 'From: ' . $from;
here is my code....
$subject = "This is Subject";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
$to = 'foo#foo.com';
$body = 'Mail Content Here';
mail($to, $subject, $body, $headers);
but when i open this file it sends a mail to $to successfully but with wrong headers....and my hosting server default address i.e mars.myhosting.com, instead of mydomain#domain.com how can i fix that
Look at this from php.net
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Webmaster <webmaster#example.com>' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Add the from header
Here is what I would do via PHP:
<?PHP
$to = 'email#address.com';
$subject = 'desired subject';
$message = 'desired message';
$headers = 'From: example#email.com' . "\r\n" .
'Reply-To: example#email.com' . "\r\n" .
'Return-Path: example#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I hope that helps some :)