【问题标题】:Wordpress Plugin Development: Override Default SMTPWordpress 插件开发:覆盖默认 SMTP
【发布时间】:2014-10-07 14:01:55
【问题描述】:

我想覆盖 wordpress 提供的默认 SMTP 服务器设置,并且必须在插件中的多个位置使用相同的 SMTP 设置来发送邮件。

新的 SMTP 服务器详细信息将由用户通过 wp-admin 上的表单提供

在谷歌搜索时,我遇到了 phpMailerclass-smtp.php

我能想到的可能解决方案
1.为phpmailer类创建一个全局对象,并在插件中使用它
2.覆盖默认的wordpress SMTP服务器设置
3. 将用户输入的设置保存在数据库中,并在我必须发送邮件的任何地方创建 PHPmailer 对象时检索它。

上述解决方案我面临的问题是..

第一个解决方案。我无法弄清楚如何实现它。
第二种解决方案:我在 wordpress codex 上找不到任何资源,可以解释如何覆盖默认 smtp 设置。
第三种解决方案:效率不够。

另外,我正在尝试创建一个独立插件,因此无法创建对任何第三方插件的依赖项。虽然我尝试过查看wp-smtp 的源代码,但无法弄清楚如何在多个地方使用相同的设置。

我正在使用 Tom Mcfarlin 的 Wordpress 样板插件 (link),以创建标准的插件文件结构,所以如果有人可以向我解释使用样板的解决方案,那将非常有用且高效。

编辑:
我正在上传文件结构以便更好地理解。

这是表格

在 class-atf-admin.php 中成功检索到表单值

我需要在class-atf-admin.php中创建一个全局变量,在这里我将设置从表单接收到的值,并在上图所示的文件中使用它。

【问题讨论】:

  • 为什么不能更改 Wordpress 的 SMTP 设置?
  • 我想通过我的插件更改 wordpress SMTP 设置,如果您能帮助我实现这一点,那将非常有帮助。我在 wordpress codex 上找不到任何资源

标签: php wordpress smtp phpmailer


【解决方案1】:

好吧,我将对选项 1 进行破解(虽然不是 100% 的 phpmailer 命名)

在你的主插件文件中,基于this example code

<?php    
require_once 'phpmailer/PHPMailerAutoload.php';

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
}

对于来自用户端的变量,将上述内容添加到简码中,并通过简码插入 smtp 详细信息。例如

[sendMail host="mail.yourdomain.com" port="26" username="yourname@yourdomain" password="yourpassword"]

【讨论】:

  • 请不要链接到旧文档。我已经修改了指向当前位置的链接,并稍微更新了代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
相关资源
最近更新 更多