I have searched for similar duplicated about this question and nothing works.
Here is my code to automate a email. I have initiated the data and the method as follow,
<?php
$reportdate=date('Y-m-d',strtotime("-1 days"));
$To = 'abc@gmail.com';
$Cc = 'xyz@dialog.lk';
$Subject = 'Data as at '.$reportdate;
//this functin processes the server return codes and generates errors if needed
function server_parse($socket, $expected_response)
{
$server_response = '';
while (substr($server_response, 3, 1) != ' ')
{
if (!($server_response = fgets($socket, 256)))
echo 'Couldn\'t get mail server response codes. Please contact the forum administrator.', __FILE__, __LINE__;
//exit;
}
if (!(substr($server_response, 0, 3) == $expected_response))
echo 'Unable to send e-mail. Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__;
//exit;
}
//
// This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
// They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
// -------> This message is from punBB developer
//
function smtp_mail($to,$cc, $subject, $message, $headers = '')
{
$recipients = explode(',', $to);
$recipientscc = explode(',', $cc);
$user = '<your mail id>';
$pass = '<your password>';
$smtp_host = 'xxx@dy.lk';
$smtp_port = 25;
if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
echo "Could not connect to smtp host '$smtp_host' ($errno) ($errstr)", __FILE__, __LINE__;
server_parse($socket, '220');
fwrite($socket, 'EHLO '.$smtp_host."\r\n");
server_parse($socket, '250');
// fwrite($socket, 'AUTH LOGIN'."\r\n");
// server_parse($socket, '334');
// fwrite($socket, base64_encode($user)."\r\n");
// server_parse($socket, '334');
// fwrite($socket, base64_encode($pass)."\r\n");
// server_parse($socket, '235');
fwrite($socket, 'MAIL FROM: <sss@xc.lk>'."\r\n");
server_parse($socket, '250');
foreach ($recipients as $email)
{
fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
server_parse($socket, '250');
}
fwrite($socket, 'DATA'."\r\n");
server_parse($socket, '354');
// fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
//fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".'cc: <'.implode('>, <', $recipientscc).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".'cc: <'.implode('>, <', $recipientscc).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
fwrite($socket, '.'."\r\n");
server_parse($socket, '250');
fwrite($socket, 'QUIT'."\r\n");
fclose($socket);
return true;
}
$headers= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers.= "From: <cc@xyz.lk>" . "\r\n";
$messagenew='Dear All,';
$messagenew.=nl2br(" \r\n \r\n");
$messagenew.=nl2br(" \r\n \r\n");
$messagenew.='Best Regards,';
$messagenew.=nl2br(" \r\n");
$messagenew.='This is an automated email, Please do not reply ... ';
// Send the mail
if(smtp_mail($To,$Cc, $Subject, $messagenew, $headers))
{
$pieces = explode(",", $To);
$NumofMails=count($pieces);
}
else
{
echo "Some error occurred";
}
?>
This works fine. But I am not getting any email to Cc recipients. Can someone help me?I have tried by replacing the headers as well. But when I copy all the Cc recipients to $TO this works fine as well.
For the record, I am no expert on the SMTP protocol, however it seems you are missing to actually send out the CC recipients.
Try adding another foreach loop like this:
foreach ($recipients as $email)
{
fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
server_parse($socket, '250');
}
foreach ($recipientscc as $email)
{
fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
server_parse($socket, '250');
}
According to a quick google search on the matter, the difference between 'To', 'CC' and 'BCC' is actually just in the headers, just like the subject.
Please refer to https://mailtrap.io/blog/cc-bcc-in-smtp/ for the details I found. (It's not much, but it's something)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.