如何在PHP中使用smtp发送邮件和密件抄送

时间:2014-03-31 01:26:16

标签: php smtp sendmail

我有一个PHP脚本,我想通过bcc属性发送消息。但是这个PHP脚本发送消息但不发送为密件抄送。密件抄送地址显示在电子邮件中。

  // Construct the email
  $recipient = 'me@mydomain.com';

  $headers = array();
  $headers['From']        = 'ME <me@mydomain.com>';
  $headers['Subject']     = $subject;
  $headers['Reply-To']    = 'no-reply@mydomain.com';
  $headers['Bcc']         = 'abc@anotherdomain.com, xyz@thatdomain.com';
  $headers['Return-Path'] = 'me@mydomain.com';
  //$params['sendmail_args'] = '-fme@mydomain.com'; // this does not work

  $body = 'message body';

  // Define SMTP Parameters
  $params = array();
  $params['host']     = 'mail.mydomain.com';
  $params['port']     = '25';
  $params['auth']     = 'LOGIN';
  $params['username'] = 'me@mydomain.com'; // this needs to be a legitimate mail account on the server and not an alias
  $params['password'] = 'abcdef';

  // Create the mail object using the Mail::factory method
  include_once('Mail.php');
  $mail_object =& Mail::factory('smtp', $params);

  // Send the message
  $mail_object->send($recipient, $headers, $body);

1 个答案:

答案 0 :(得分:1)

以下是我发现的内容:&#34;如果抄送或密件抄送行出现在邮件正文中,请确保将新的行(\ n)与标题行分开,而不是回车换行(\ r \ n)。这应该出现在标题的最后。&#34;

您可能还想尝试在其他人之前或之后移动该标头,以查看它是否修复了问题。

更好的解决方案是切换到phpMailer,这可能是通过php发送邮件的更好,更好的解决方案。