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;
Related
I¨ve got a problem answering mails sent from my website, as the senders emailaddress isn´t showing anywhere, only the webmasters email
Here is my code:
<?php>
$from="kim.s.nielsen#mail.dk";
$email="info#intotext.dk";
$name=$_POST['name'];
$message=$_POST['message'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" . 'Reply-To: '. $fromEmail . "\r\n" . mail($email, $name, $message, $headers);
print"Din besked er sendt. Vi vender tilbage så hurtigt som muligt." ?>
Use this format to send your email, this will show the senders email address, try this. you have concatenated the mail function with your header, mail() is not header it is used to send your email.
$fullname = "full name";
$from = "sender#mail";
$to = "reciever#mail";
$subject = "Your subject";
$message = "<h1> heading </h1><p> Message </p>";
$headers = [
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1',
'From: ' . htmlspecialchars($fullname) . " <$from>",
'X-Mailer: PHP/' . phpversion()
];
mail($to,
htmlspecialchars($subject),
nl2br(htmlspecialchars($message)),
implode("\r\n", $headers)
);
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I was trying to send mail by php, the function mail returns 1, but i didn't get
the mail in my inbox
if (isset($_POST["firstName"]) == false)
return;
$to = "mymail#mail.com";
$subject = $_POST["subject"];
$message = $_POST["content"];
$headers = 'From: ' . $_POST["email"] . "\r\n" .
'Reply-To: ' . $_POST["email"] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$from = "From: " . $_POST["firstName"] . " " . $_POST["lastName"] . " <" . $_POST["email"] . ">";
echo mail($to, $subject, $message, $headers);
echo '<script type="text/javascript">alert("sent message succesfully");</script>';
Using PHP's mail() function it's possible. Remember mail function will not work in Local server.
<?php
$to = 'abc#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: admin#example.com' . "\r\n" .
"CC: somebodyelse#example.com";
mail($to, $subject, $message, $headers);
?>
Reference:
http://php.net/manual/en/function.mail.php
The below code is designed to send an email when a sale has been made. The body of the email contains information like the customers name, their email address etc. All I'd like to do is add a line break to each of the fields within the final output of the email. I've tried a simple HTML line break and /n and /r which didnt work....any ideas? Thanks!
<?php
//Enter email to receive purchase alerts
///////////////////////////////////////////
$alert_email = 'sales#my-domain.com';
///////////////////////////////////////////
$username = $_POST['username'];
$password = $_POST['password'];
$to = $alert_email;
$subject = 'Sale! = ' .$course;
$message = $name. ' '. $email. ' '. $username. ' '. $password;
$headers = 'From: sales#my-domain.com' . "\r\n" .
'Reply-To: hi#my-domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
The line that needs to be changed I am sure is this one (and line breaks inserted somewhere here:
$message = $name. ' '. $email. ' '. $username. ' '. $password;
Thanks!
Send a HTML content by applying header Content-type:text/html
$to = $alert_email;
$subject = 'Sale! = ' .$course;
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <sales#my-domain.com>' . "\r\n";
$headers .= 'Reply-To: hi#my-domain.com' . "\r\n";
$message = $name. '<br /> '. $email. '<br /> '. $username. '<br /> '. $password;
mail($to, $subject, $message, $headers);
\n should work. Be sure to use a \ instead of a forward slash.
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
}
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,
);