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 }}
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 can't get the reply to function to work on a contact form that submits to itself.
The BCC is working fine. Any help is appreciated.
$to = 'email#email.com';
$subject = ''.$_POST['emailsubject'].'';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
$headers .= 'Reply-To: '.$_POST['email'].'';
'X-Mailer: PHP/' . phpversion();
$headers .= 'Bcc: email#email.com' . "\r\n";
mail($to, $subject, $msg, $headers); $sendMessage = true; unset($_POST); } } ?> <?php if($invalidCaptcha) { ?>
<?php
$from_add = "test#gmail.com";
$to_add = "email#email.com";
$subject = "Test Msg";
$message="test msg.";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
$msg = "Mail sent OK";
}
else
{
$msg = "Error sending email!";
}
?>
I am using PHP mail and trying to send BCC, but for some reason since I've added the lines with //ADDED NEW on it , it's just now sending any emails at all.
Here is the full code:
$to = "me#gmail.com";
$bcc = $row['recipients']; //ADDED NEW
$subject = $row['subject'];
$message = $row['text_body'];
$headers = "From: " . strip_tags('me#gmail.com') . "\r\n";
$headers .= "Reply-To: ". strip_tags('me#gmail.com') . "\r\n";
$headers .= "Bcc: $emailList\r\n"; //ADDED NEW
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $bcc, $subject, $message, $headers); // $bcc ADDED NEW
Why is this not sending?
Your problem,
Set $bcc but in $headers does not using it
Putting invalid argument into mail function.
Try this
$to = "me#gmail.com";
$bccList = $row['recipients']; //ADDED NEW
$subject = $row['subject'];
$message = $row['text_body'];
$headers = "From: " . strip_tags('me#gmail.com') . "\r\n";
$headers .= "Reply-To: ". strip_tags('me#gmail.com') . "\r\n";
$headers .= "Bcc: $bccList\r\n"; //ADDED NEW
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers); // $bcc ADDED NEW
There's no $bcc argument to the mail() function. It should be:
mail($to, $subject, $message, $headers);
The blind recipients will be retrieved from the Bcc header in $headers.
I am attempting to send an email using the mail() PHP function. I had it working until I attempted to give it a subject of "User registration", then the mail is not sent!
Heres the code (have simplified it greatly)
$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'From: noreply#example.com' . "\r\n" ;
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
$headers .= 'From: Website <admin#example.com>';
mail($to, 'User Registration', $message, $headers);
I also attempted to use a variable containing the string of text but that didnt work.
Why is it not sending the mail when I add the subject exception?
Thanks
EDIT: updated code thats still not working
$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: Website <admin#example.com>';
mail($to, 'User Registration', $message, $headers);
On your 4th line you're using ' that handles everything inside of it as a string so change
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
To:
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
and as mentioned in the comments change chareset to charset
Edit:
if your sending a txt/html mail you have according to documentation to set mime in the headers too so try this
$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: Website <admin#example.com>' . "\r\n";
mail($to, 'User Registration', $message, $headers);
If it still doesn't work you could try to debugg your code, simply add
error_reporting(E_ALL);
ini_set('display_errors', '1');
on top of the page, and take it from there, and if you still can't solve it by yourself post it here and I'll do my best to help ya out.
I'm using this code on most of my projects:
$subject = 'subject';
$message = 'message';
$to = 'user#gmail.com';
$type = 'plain'; // or HTML
$charset = 'utf-8';
$mail = 'no-reply#'.str_replace('www.', '', $_SERVER['SERVER_NAME']);
$uniqid = md5(uniqid(time()));
$headers = 'From: '.$mail."\n";
$headers .= 'Reply-to: '.$mail."\n";
$headers .= 'Return-Path: '.$mail."\n";
$headers .= 'Message-ID: <'.$uniqid.'#'.$_SERVER['SERVER_NAME'].">\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Date: '.gmdate('D, d M Y H:i:s', time())."\n";
$headers .= 'X-Priority: 3'."\n";
$headers .= 'X-MSMail-Priority: Normal'."\n";
$headers .= 'Content-Type: multipart/mixed;boundary="----------'.$uniqid.'"'."\n";
$headers .= '------------'.$uniqid."\n";
$headers .= 'Content-type: text/'.$type.';charset='.$charset.''."\n";
$headers .= 'Content-transfer-encoding: 7bit';
mail($to, $subject, $message, $headers);
I would suggest to use PHP_EOL instead of \r\n or \n as the line break would then be determined by your environment...
$headers = 'MIME-Version: 1.0' . PHP_EOL;
etc.. hoping this might finally solve your problem!
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);
?>