2014-10-07 113 views
3

我想覆蓋WordPress提供的默認SMTP服務器設置,並且必須在插件的多個位置使用相同的SMTP設置來發送郵件。Wordpress插件開發:覆蓋默認SMTP

新的SMTP服務器詳細信息將用戶的可溼性粉劑管理員

提供,通過一個形式,而谷歌搜索一下吧,我來翻過phpMailerclass-smtp.php

可能的解決方案,我可以想到
1.創建的PHPMailer類一個全局對象,並用它進行的跨插件
2.覆蓋WordPress默認SMTP服務器設置
3.保存用戶在數據庫中輸入的設置與檢索它,同時創建一個PHPmailer對象,無論我需要發送郵件。

我與上述解決方案所面臨的問題是..

1解決方案。我無法弄清楚如何實現它。
第二種解決方案:我找不到wordpress codex上的任何資源,可以解釋如何覆蓋默認的smtp設置。
第三種解決方案:不夠有效。

此外,我試圖創建一個獨立的插件,所以不能創建任何第三方插件的依賴。儘管我已經試過了wp-smtp的源代碼,但無法弄清楚如何在多個地方使用相同的設置。

爲了創建一個標準的插件文件結構,我使用了Tom Mcfarlin的Wordpress Boilerplate Plugin(link),所以如果有人能夠使用樣板向我解釋一個解決方案,那將是非常有用和高效的。

EDITS:
我正在上傳文件結構以便更好地理解。
enter image description here

這是 enter image description here

形式值是在類ATF-admin.php的成功地檢索到的形式 enter image description here

我需要創建類的全局變量-atf-admin.php,我將在這裏設置從表單收到的值,並將其用於上圖所示的文件中。

+0

爲什麼不能更改Wordpress的SMTP設置? – Synchro 2014-10-07 15:55:07

+0

我想通過我的插件更改wordpress SMTP設置,如果你能幫我實現這一點,這將是非常有幫助的。 我在wordpress codex上找不到任何資源 – anurag 2014-10-07 17:03:05

回答

2

Mkay,我將不得不在選項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 = "[email protected]"; // SMTP account username 
$mail->Password = "yourpassword";  // SMTP account password 

$mail->SetFrom('[email protected]', 'First Last'); 

$mail->AddReplyTo("[email protected]","First Last"); 

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

$mail->MsgHTML($body); 

$address = "[email protected]"; 
$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="[email protected]" password="yourpassword"]

+0

請不要鏈接到舊文檔。我修改了指向當前位置的鏈接,並稍微更新了代碼。 – Synchro 2014-10-08 16:02:43