我试图通过PHP脚本发送电子邮件。我可以成功发送,但没有电子邮件到达Cc字段下的收件人..标题是否有问题?
function send_email($to,$subject,$body,$cc = ''){
require_once "Mail.php";
$from = "<removed>";
$host = "smtp.domain.com";
$username = "<removed>";
$password = "<removed>";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Cc' => $cc);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$response["success"] = 0;
$response["message"] = $mail->getMessage();
echo json_encode($response);
}else {
$response["success"] = 1;
$response["message"] = "Email sent to " . $to;
echo json_encode($response);
}
}
答案 0 :(得分:0)
如果您使用PHPMailer(来自您的评论),请添加如下地址:
$mail->addCC('somebodyelse@bla.com', 'somebody else');
$mail->addCC('otherguy@blabla.com', 'other somebody');
或者为方便起见,在循环中调用addCC
方法。
答案 1 :(得分:0)
我转移到PHPMailer,现在正在工作..
这是工作代码
drawInContext:
可在此处找到所需的课程 - https://github.com/romelemperado/PHPMailer
答案 2 :(得分:0)
这是我使用PEAR的Mail包发送邮件的方式。 请注意,您还需要安装Mail / Mime软件包 - 这很简单。
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
$from = "from@example.com";
$msg = "this is a test";
$to = 'to@example.com';
$cc = 'cc@example.com';
$sbj = "Testing";
$message = new Mail_mime();
$message->setTXTBody($msg);
$message->setHTMLBody("<html><body>$msg</body></html>");
$body = $message->get();
$extraheaders = [
"From" => $from,
"Cc" => $cc,
"To" => $to,
"Subject" => $sbj
];
$mail = Mail::factory("mail");
$headers = $message->headers($extraheaders);
// In case you have more than one address in the $to variable.
$addresses = implode(",", [$to]);
if ($mail->send($addresses, $headers, $body)) {
echo "Successfully Sent";
} else {
echo "Message Failed";
}