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()
Related
I want to send an arabic email using php, but it gives me others characters. How to send it with the arabic characters? This is my code:
<?php
if( isset($_POST['name']) )
{
$to = 'support#alkramlaundry.qa'; // Replace with your email
$subject = $_POST['subject'];
$message = $_POST['message'] . "\n\n" . 'Regards, ' . $_POST['name'] . '.';
$headers = 'From: ' . $_POST['name'] . "\r\n" . 'Reply-To: ' . $_POST['email'] . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
if( $_POST['copy'] == 'on' )
{
mail($_POST['email'], $subject, $message, $headers);
}
}
?>
you aren't specifying any Content-Type header so the recipient will use some default encoding
values appended to the $additional_headers parameter of the mail() function need to be properly sanitized (or users may inject additional headers)
Modified code (I assume the encoding is UTF-8):
$filterHeaderValue = function ($value) {
return str_replace(array("\r", "\n"), '', trim($value));
};
$subject = $_POST['subject'];
$message = $_POST['message'] . "\n\n" . 'Regards, ' . $_POST['name'] . '.';
$headers =
"Content-Type: text/plain; charset=UTF-8\r\n"
. 'From: ' . $filterHeaderValue($_POST['name']) . "\r\n"
. 'Reply-To: ' . $filterHeaderValue($_POST['email']) . "\r\n"
. 'X-Mailer: PHP/' . phpversion()
;
mail($to, $subject, $message, $headers);
if ($_POST['copy'] == 'on') {
mail($_POST['email'], $subject, $message, $headers);
}
By default, email (or the smtp protocol) only accepts the first 7 bits in ascii. To enable support for other languages add the following field to your headers:
Content-Type: text/plain; charset=UTF-8
Here is the RFC backing:
rfc5335
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=UTF-8\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
First thing try:
ini_set('default_charset', 'UTF-8');
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 }}
For some reason my php mail() function does not send html,
Instead of link it shows just shows link as plain text.
Any ideas?
this is the headers I used:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
// Additional headers
$headers .= 'From: My Automated Message <robot#mysite.cc>' . "\r\n";
And invoked as follows:
try {
if (#mail($to_email, $subject, $message, $headers)){
echo "<span style=\"color:#0D0; font:10pt Tahoma;font-weight:bold;\">{$SENT_MESSAGE}</span><br><br>";
return true;
} else {
$tmp=error_get_last();
throw new Exception($tmp['message']);
}
} catch (Exception $e) {
echo "<span style=\"color:red; font:10pt Tahoma;font-weight:bold;\">Error: ".$e->getMessage()."</span><br><br>";
}
I also tried to send the mail without headers, but then again it shows the link as plain text like this: link
Try this header
$headers = 'From: Name<address#domain.com>' . "\r\n" .
'Content-Type: text/html; charset=UTF-8' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Transfer-Encoding: base64' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Check this
$from='robot#mysite.cc';
$headers ='';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from.' '. "\r\n";
mail($to_email, $subject, $message, $headers);
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);
?>
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);
?>