编辑include / mail.inc,在那里添加我自己的SMTP设置

时间:2012-04-18 12:30:19

标签: drupal

以下函数包含在Drupal6的include / mail.inc中,它使用隐藏在名为“php.ini”的文件中的默认SMTP设置来发送邮件。

function drupal_mail_send($message) {
  // Allow for a custom mail backend.
  if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) {
    include_once './'. variable_get('smtp_library', '');
    return drupal_mail_wrapper($message);
  }
  else {
    $mimeheaders = array();
    foreach ($message['headers'] as $name => $value) {
      $mimeheaders[] = $name .': '. mime_header_encode($value);
    }
    return mail(
      $message['to'],
      mime_header_encode($message['subject']),
      // Note: e-mail uses CRLF for line-endings, but PHP's API requires LF.
      // They will appear correctly in the actual e-mail that is sent.
      str_replace("\r", '', $message['body']),
      // For headers, PHP's API suggests that we use CRLF normally,
      // but some MTAs incorrecly replace LF with CRLF. See #234403.
      join("\n", $mimeheaders)
    );
  }
}

但我使用共享主机,因此我无法编辑php.ini,我想编辑上面的函数“drupal_mail_send”,将下面的代码添加到该函数中,以便它可以绕过PHP mail()函数并发送直接发送电子邮件到我最喜欢的SMTP服务器。

include('Mail.php');

$recipients = array( 'someone@example.com' ); # Can be one or more emails

$headers = array (
    'From' => 'someone@example.com',
    'To' => join(', ', $recipients),
    'Subject' => 'Testing email from project web',
);

$body = "This was sent via php from project web!\n";

$mail_object =& Mail::factory('smtp',
    array(
        'host' => 'prwebmail',
        'auth' => true,
        'username' => 'YOUR_PROJECT_NAME',
        'password' => 'PASSWORD', # As set on your project's config page
        #'debug' => true, # uncomment to enable debugging
    ));

$mail_object->send($recipients, $headers, $body);

您能否记下修改后的代码供我参考?

1 个答案:

答案 0 :(得分:0)

drupal_mail_send中的代码是Drupal核心功能的一部分,不应直接更改,因为更新Drupal时可能会覆盖您的更改。

Drupal核心文件的修改通常被Drupal社区称为“黑客核心”,主要是discouraged

Drupal已经有许多可用的模块可以帮到你。见:

http://drupal.org/project/phpmailer模块:

  

使用PHPMailer库添加SMTP支持以发送电子邮件。   附带有关如何使用Google的详细配置说明   邮寄为邮件服务器。

http://drupal.org/project/smtp模块:

  

此模块允许Drupal绕过PHP mail()函数并发送   直接向SMTP服务器发送电子邮件。