Adding CC in PHP mail function - php

I want to send mail with CC. The email is sent successfully, but CC is not sent.
$to = 'abc#xyz.com';
$subject = 'Order Details of Order Number:'.$orderID;
$headers = "From: webmaster#xyz.com\r\nReply-To: webmaster#xyz.com";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
$message = "Test Message";
$headers .= 'Cc: def#xyz.com' . "\r\n";
mail($to, $subject, $message, $headers);
I am sending this mail as HTML.

Add the header as following,
$to = 'toemailaddress#testcom';
$from = 'fromemail#from.com';
$fromName = 'FromName';
$subject = "Email Subject";
$htmlContent = "<h> content of the email </h>";
$headers = "From: $fromName" . " <" . $from . ">" . "\r\n";
$headers .= "Cc: cc#cc.com ";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" ."Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
$returnpath = "-f" . $from;
$message .= "--{$mime_boundary}--";
$mail = #mail($to, $subject, $message, $headers, $returnpath);

Try add this code in the following order:
$headers = "From: $from_email\r\nReply-To: $from_email";
$headers .= 'Cc: test#test.com\r\n';
$headers .= 'Bcc: test#test.com\r\n';
$headers .= "Return-Path: <info#premierinspectiontn.com>\r\n";
//add boundary string and mime type specification
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
Resource

Related

Multiple or malformed newlines found in additional_header using mail() in php

trying to send mail using native mail() but its showing error in the header part
"Message: mail(): Multiple or malformed newlines found in
additional_header".
It is showing an error in the last line of the code which is:
$mail = mail($email, $subject, $message, $headers, $returnpath);
code:
$email = $id;
$subject = $mydetails_name.' had sent Contact Details';
$message = $html;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: CloudBigTechnology' . "\r\n";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
if(!empty($file_element_name['name'])) {
$filesCount = count($explodedString);
for($x=0;$x<count($explodedString);$x++) {
$file_namex = basename($file_element_name['name'][$x]);
$file_sizex = filesize($file_element_name['tmp_name'][$x]);
$message .= "--{$mime_boundary}\n";
$fp = #fopen($file_element_name['tmp_name'][$x], "rb");
$datam = #fread($fp, $file_sizex);
#fclose($fp);
$datam = chunk_split(base64_encode($datam));
$message .= "Content-Type: application/octet-stream; name=\"".$file_namex."\"\n" .
"Content-Description: ".$file_namex."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".$file_namex."\"; size=".$file_sizex.";\n" .
"Content-Transfer-Encoding: base64\n\n" . $datam . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $email;
$mail = mail($email, $subject, $message, $headers, $returnpath);
How to fix it?

how to send multipart/alternative email with PHP correctly

I'm trying to use the built-in PHP mail function to send multipart messages that contain a html and a plain text version of my message. I've been playing around with different encoding types but, I keep running into problems. Originally I set Content-Transfer-Encoding to Binary but, that resulted in exclamation points being placed every 78 characters. I also tried base64 but I believe that base64 is overkill for what I am doing.
All I'm doing is sending basic HTML, no encoded images, files, or attachments. I'd prefer an encoding method that would still allow the source code to be human readable.
I heard that Quoted-Printable is what I'm looking for but, when I attempted to send messages using that encoding type the result ending up looking really weird. I noticed a bunch of " symbols sprinkled throughout the message source code.
Here is the code I'm using:
$to = "to#test.com";
$subject = "test subject";
$boundary = uniqid('np');
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: from-address#test.com\r\n";
$headers .= "Reply-To: reply-address#test.com\r\n";
$headers .= "Return-Path: return-path#test.com\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $HTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";
$ok = mail($to,$subject,$message,$headers);
What the heck am I doing wrong here?
Hi try the following code,
$to = "to#test.com";
$subject = "test subject";
$plainTextMessage = "Hi all";
$HTMLmessage = "<b>Hi all</b>";
//$boundary = uniqid('np');
$boundary = md5(uniqid(time()));
$headers .= "From: from-address#test.com\r\n";
$headers .= "Reply-To: reply-address#test.com\r\n";
$headers .= "Return-Path: return-path#test.com\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $HTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";
$ok = mail($to,$subject,$message,$headers);
May it will help
No need for a third party library or an external mail forwarding host. This is what I use and it works like a charm. It also fixes some potential security holes by forcing headers using the $from address that can otherwise reveal your system user :
private function sendMail($to, $from, $fromName, $subject, $text, $html)
{
$boundary = uniqid('np');
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: ".$fromName." <".$from.">" . "\r\n";
$headers .= "X-Sender: ".$fromName." <".$from.">\n";
$headers .= "Return-Path: <".$from.">\n";
$headers .= "To: ".$to."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
// Content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
// Plain text body
$message .= $text;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
// Html body
$message .= $html;
$message .= "\r\n\r\n--" . $boundary . "--";
// Send mail
mail('', $subject, $message, $headers, '-f ' . $from);
}

PHP: Sending an html plus plain text email I got mail with empty body

This is php code
$boundary = uniqid("np");
$headers = "MIME-Version: 1.0\n" ;
$headers .= "From: $from\n";
$headers .= "To: $to\n";
if (!is_null($reply_to)) $headers .= "Reply-To: $reply_to \n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\n";
//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\n\n--" . $boundary . "\n";
$message .= "Content-type: text/plain;charset=utf-8\n\n";
//Plain text body
$message .= "Hello,\nThis is a text email, the text/plain version.
\n\nRegards,\nYour Name";
$message .= "\n\n--" . $boundary . "\n";
$message .= "Content-type: text/html;charset=utf-8\n\n";
$message .= "Hello, <BR> This is a text email, the <bold>html version</bold>.";
$message .= "<BR>Regards";
$message .= "<BR>Your Name";
$message .= "\n\n--" . $boundary . "--";
$sendmail = mail($to, $title, $message, $headers);
$headers variable contains:
MIME-Version: 1.0
From: Italia S.p.a. <root#domain.it>
To: My Name <my_name#domain.it>
Reply-To: My Alt Name <my_alt_name#domain.it>
Content-Type: multipart/alternative;boundary=np536b88fc27327
$message variable contains:
This is a MIME encoded message.
--np536b88fc27327
Content-type: text/plain;charset=utf-8
Hello,
This is a text email, the text/plain version.
Regards,
Your Name
--np536b88fc27327
Content-type: text/html;charset=utf-8
Hello, <BR> This is a text email,
the <bold>html version</bold>.<BR>Regards<BR>Your Name
--np536b88fc27327--
Problem is: when sending using mail($from, $to, $message, $headers) mail is delivered, but it's empty
Header should be..
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Check PHP mail() function.
Email headers must be separated by \r\n.

PHP Mail Sending BCC not wordking

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.

Simple script to send email?

I use the code to send email:
<?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.";
?>
But I got:
sendmail: fatal: chdir /Library/Server/Mail/Data/spool: No such file or directory
And I don't know how to add attachment and importantly I need to specify the Content-Type for attachment, can anyone help me?
Thanks!
<?php
$to = "someone#example.com";
$subject = "Test mail";
$from = "someonelse#example.com <someonelse#example.com>";
$message = "Hello! This is a simple email message.";
// let's say you want to email a comma-separated-values
$tofile = "col1,col2,col3\n";
$tofile .= "val1,val1,val1\n";
$filename = "filename.csv";
$random_hash = md5(date('r', time()));
$attachment = chunk_split(base64_encode($tofile));
$headers = "From: " . $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$body = "--PHP-mixed-".$random_hash."\r\n";
$body .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n";
$body .= $message;
$body .= "\r\n\r\n";
$body .= "\r\n--PHP-mixed-" . $random_hash . "\r\n\r\n";
$body .= "--PHP-mixed-".$random_hash."\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Type: text/comma-separated-values; name=\"" . $filename . "\" charset=\"UTF-8\"\r\n";
$body .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$body .= $attachment;
$body .= "\r\n--PHP-mixed-".$random_hash."--";
mail($to, $subject, $body, $headers);
?>

Categories