2015-11-04 80 views
0

我想通过PHP脚本发送电子邮件。我可以发送成功,但没有电子邮件到达收件人的抄送字段..是否有问题的标题?电子邮件抄送不工作

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); 
    } 
} 
+1

沉默是因为没有人使用梨邮寄论文的日子。 – 2015-11-04 21:34:05

+0

我现在使用这个 - https://github.com/Synchro/PHPMailer – mboy

回答

0

如果您使用的PHPMailer(从您的评论),添加地址,像这样:

$mail->addCC('[email protected]', 'somebody else'); 
$mail->addCC('[email protected]', 'other somebody'); 

,或电话为您提供方便环路addCC方法。

+0

是的..谢谢。我在迁移到PHPmailer后立即工作... – mboy

+0

太棒了!所有冰雹PHPMailer! ;)干杯 –

0

我搬到PHPMailer的,这是现在的工作..

这是工作的代码

<?php 
 

 
// array for JSON response 
 
$response = array(); 
 

 
$to = $_POST['rcpt']; 
 
$subject = $_POST['subject']; 
 
$body = $_POST['msgBody']; 
 
$cc = $_POST['cc']; 
 

 
send_email($to,$subject,$body,$cc); 
 

 
function send_email($to,$subject,$body,$cc = ''){ 
 
    require_once "Mail.php"; 
 

 
    $from = "monitor<[email protected]>"; 
 

 
    $host = "smtp.domain.com"; 
 
    $username = "[email protected]"; 
 
    $password = "gUZfE6SVLV"; 
 

 
// $headers = array ('From' => $from, 
 
// 'To' => $to, 
 
// 'Subject' => $subject, 
 
// 'Cc' => $cc); 
 
    $headers['From'] = $from; 
 
    $headers['To']  = $to.", ".$cc; 
 
    $headers['Subject'] = $subject; 
 

 

 
    $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); 
 
    } 
 
} 
 
?>

所需的类可以在这里找到 - https://github.com/romelemperado/PHPMailer

0

这是我如何使用PEAR的邮件包发送邮件。 请注意,您还需要安装Mail/Mime包 - 这是微不足道的。

<?php 
require_once 'Mail.php'; 
require_once 'Mail/mime.php'; 

$from = "[email protected]"; 
$msg = "this is a test"; 
$to = '[email protected]'; 
$cc = '[email protected]'; 
$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"; 
} 
相关问题