I got a strange behavior from the mail function in php
here is the code :
$header = "From: aa#aa.com\n";
$header .= "Reply-To: bb#bb.com\n";
$header .= "Content-Type: multipart/alternative; boundary=$alt_boundary\n";
$header .= "Mime-Version: 1.0\n";
$header .= "X-Mailer: PHP/".phpversion()."\n";
$header .= "Content-Type: text/plain;charset=utf-8\n";
$send = mail($to,$subject,$message,$headers);
but the email i receive have a from address from the main admin of the server like : user123#s12panelboxmanage.com
why ?
Maybe it's because you set a variable $header, but pass to mail() variable $headers. If it's not the cause, try inserting \r\n instead of \n.
You should use the -f option in the mail function too to set the (valid) sender:
$header = 'MIME-Version: 1.0'."\n";
$header .= 'Content-type: text/'.$contentType.'; charset=iso-8859-1'."\n";
$header .= 'From: '.$from."\n";
$header .= 'Reply-To: '.$mailFrom."\n";
$header .= 'X-Mailer: PHP '.phpversion()."\n";
$header .= 'X-Sender-IP: '.$_SERVER['REMOTE_ADDR']."\n";
mail($to,$subject,$message,$header, "-f aa#aa.com");
<?php
$to = 'user#domain.com';
$subject = 'Subject';
$message = 'This is a test';
$headers = 'From: webmaster#yourdot.com' . "\r\n" .
'Reply-To: webmaster#yourdot.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Related
I need help getting the FROM field in the email to be a specific email address. As of now it is coming out something like this. Looks like its grabbing info off my hosting companies server.
ipw.xxxxxxxxx#boscustweb1302.eigbox.net
All other features of email work great. My $headers are as follows:
$to = $email;
$subject = "ORDER # $tranid";
$headers = "From: info#xxxxxx.com";
$headers = "MIME-Version: 1.0";
$headers = "Content-type: text/html; charset=iso-8859-1";
PHP manual sugest this. Which is pretty much what I am doing, I think.
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
They do use implode, but I don't think I need it the way I have mine setup:
mail($to, $subject, $message, implode("\r\n", $headers));
Tried this with no success:
$to = $email;
$subject = "ORDER # $tranid";
$headers .= "From: info#xxxxxx.com";
$headers .= "MIME-Version: 1.0";
$headers .= "Content-type: text/html; charset=iso-8859-1";
Found the answer with the help of #chris85
$headers = "From: info#xxxxx.com\r\n".
"MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
Keeps HTML format in tack and shows FROM field correctly.
I have a local instance of sendmail running on my machine, and am using a php script in order to send custom messages in order to run an internal anonymous suggestions application.
I currently use the following script:
<?php
$to = '*to*';
$subject = '*subject*';
$message = '*message*';
$headers = 'From: *from*' . "\r\n" .
'Reply-To: *replyto*' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I would like to be able to support HTML, which I would assume was the following:
<?php
$to = '*to*';
$subject = '*subject*';
$message = '<html>here</html>';
$headers = 'From: *from*' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Contesnt-type:text/html;charset=UTF-8' . "\r\n";
$headers .= 'Reply-To: *replyto*' . "\r\n";
mail($to, $subject, $message, $headers);
?>
I do this via these functions:
private function plaintext_version($text) {
return "--PHP-nextpart-".$this->random_hash
."\nContent-Type: text/plain; charset=\"iso-8859-1\""
."\nContent-Transfer-Encoding: 7bit"
."\n$text";
} // EOF plaintext_version
private function HTML_version($html) {
return "--PHP-nextpart-".$this->random_hash."
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$html
";
} // EOF HTML_version
Here's the "Send" function. Note that the Content-Type is "multipart/alternative" and a "boundary" is specified; I create the boundary by concatenating "--PHP-nextpart-" with the output of a hashing function.
public function Send() {
if (strlen($this->cc_to)) $this->headers .='Cc: '.$this->cc_to . "\r\n";
if (strlen($this->bcc_to)) $this->headers .='Bcc: '.$this->bcc_to . "\r\n";
if (strlen($this->reply_to)) $this->reply_to = $this->mail_from;
$l_headers = 'From: '.$this->mail_from . "\r\n"
.'Reply-To: '.$this->reply_to. "\r\n"
.'X-Mailer: PHP/' . phpversion()."\r\n";
if (strlen($this->headers)) $l_headers .= $this->headers;
$l_headers .= "Content-Type: multipart/alternative; boundary=\"PHP-nextpart-".$this->random_hash."\"";
$mess = $this->plaintext_version($this->msg_text) . "\n\n". $this->HTML_version($this->html_version);
if (strlen($this->to_name)) {
$to = "\"".$this->to_name."\" <".$this->mail_to.">";
} else {
$to = $this->mail_to;
}
$sent = mail($to,$this->subject,$mess,$l_headers);
if ($sent) return true;
return false;
} //EOF Send()
I was trying to send mail in PHP with mail() function. The email sent successfull and it's exist in the inbox. The problem is the headers printed in the email header. I've tried the various code for the headers.
1.The first goes like this:
$headers = "From: My Example Email".'\r\n'.
"MIME-Version: 1.0".'\r\n'.
"Content-Type: text/html; charset=ISO-8859-1".'\r\n'.
'X-Mailer: PHP/' . phpversion();
Result: My Example EmailrnMIME-Version
2.Second headers code:
$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();
Result: The email didn't sent
3.Third headers code:
$headers = "From: My Example Email".'"\r\n"'.
"MIME-Version: 1.0".'"\r\n"'.
"Content-Type: text/html; charset=ISO-8859-1".'"\r\n"'.
'X-Mailer: PHP/' . phpversion();
Result: My Example Emailrn
I use PHP 5.4.19. Any answer will really help.
UPDATE
This is my whole code:
class User{
function callname(){
$user = $_SESSION['id'];
$query = ("SELECT * FROM user WHERE user.id='$user'");
while ($result=mysql_fetch_array($query)){
echo ($result['username']);}}}
$user = new User;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = mysql_real_escape_string(trim($_POST['username']));
$check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
if ($check==TRUE){
$name = $user->callname();
$to = "myemail#domain.com";
$subject = "Example Subject";
$headers = "From: My Example Email".'"\r\n"'.
"MIME-Version: 1.0".'"\r\n"'.
"Content-Type: text/html; charset=ISO-8859-1".'"\r\n"'.
'X-Mailer: PHP/' . phpversion();
$message = "Hai $name, this is the new message.";
mail($to, $subject, $message, $headers);
} else {
?>
<script type="text/javascript">
alert("Sorry, username not exist !");
</script>
<?php }}
New UPDATE:
After a long trial and help with everyone here, finally found the solution. But maybe it's unusual.
$headers = 'From: My Example Email'.'""'.
'MIME-Version: 1.0'.'""'.
'Content-Type: text/html; charset=ISO-8859-1'.'""'.
'X-Mailer: PHP/' . phpversion();
but I'm not understand yet. Some literature said that every part shoul be glue by "\r\n", but that's not work in my code.
Thanks for every help. Thanks a lot. Thats all really helpful.
You could try my function for php mailing. This function will generete RFC compatible body and header part for your email.
function buildMime($msg){
$num = md5(time());
$num = "_001_".$num."_";
$headers = "From: SenderName<senderEmail#adress.com>\n";
$headers .= "Return-Path: <senderEmail#adress.com>\n";
$headers .= "Reply-To: <senderEmail#adress.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative;\n";
$headers .= " boundary=\"".$num."\"\n";
$headers .= "X-Mailer: PHP v".phpversion()."\n";
$body = "This is a multi-part message in MIME format.\n\n";
$body1 = "--".$num."\n";
$body1 .= "Content-Type: text/plain; charset=utf-8\n";
$body1 .= "Content-Transfer-Encoding: 8bit\n\n";
$body1 .= trim(strip_tags($msg))."\n";
$body1 .= "\n";
$body1 .= "--".$num."\n";
$body1 .= "Content-Type: text/html; charset=utf-8\n";
$body1 .= "Content-Transfer-Encoding: 8bit\n\n";
$body1 .= $msg;
$body1 .= "\n";
$bodyx = "--".$num."--\n";
return array('body' => $body.$body1.$bodyx, 'headers' => $headers);
}
$mime = buildMime("<h1>Hello</h1><p>this is my firs test message</p>");
mail('whereToSend#email.com', 'Your subject', $mime[body], $mime[headers]);
?>
$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."\r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();
Try this
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: ' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
You are not properly encapsulating the quotes.
The right way to do...
$headers = 'From: My Example Email'."\r\n".
'MIME-Version: 1.0'."\r\n".
'Content-Type: text/html; charset=ISO-8859-1'."\r\n".
'X-Mailer: PHP/' . phpversion();
EDITED CODE
<?php
class User{
function callname(){
$user = $_SESSION['id'];
$query = ("SELECT * FROM user WHERE user.id='$user'");
while ($result=mysql_fetch_array($query)){
return $result['username'];}}}
$user = new User;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = mysql_real_escape_string(trim($_POST['username']));
$check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
if ($check==TRUE){
$name = $user->callname();
$to = "myemail#domain.com";
$subject = "Example Subject";
$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."\r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
"X-Mailer: PHP/" . phpversion();
$message = "Hai $name, this is the new message.";
mail($to, $subject, $message, $headers);
} else {
?>
<script type="text/javascript">
alert("Sorry, username not exist !");
</script>
<?php }}
I am sending email using php script and i am setting from address as
$headers = "MIME-Version: 1.0" . "$from" . "\r\n";
$headers.= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
So, the mail coming to my mail address as from support#mysite.com, But i need to set from address as support#gmail.com.But it is not coming with the modified from address. So, please help to solve this problem..
You try this...
<?php
$to = 'nobody#example.com';
$subject = 'Mail Subject';
$message = 'Test';
$headers = 'From: support#gmail.com' . "\r\n" .
'Reply-To: support#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Hop this will works...
Replace header text,
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= "From: YourName <support#gmail.com>";
To get more information about mail function
may this help you.
I am using the mail function
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <>' . "\r\n";
$headers .= 'Cc: ' . "\r\n";
$mail_sent = #mail( $to, $subject, $message, $headers );
I want to pass the email-value for "From:" as a variable. Can this be done? And how can I pass other values like mobile number on to the mail?
Pass the email address($fromAddress) as a header
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <'.$fromAddress.'>' . "\r\n".
'Reply-To:'.$fromAddress. "\r\n" .
$headers .= 'Cc: ' . "\r\n";
$mail_sent = #mail( $to, $subject, $message, $headers );
From example2 in the docs..
<?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);
?>