WP Mail Options V0.1.0 发布

这是我开发的第一个 WordPress 插件。直到此时,我对 PHP 仍然是一窍不通。在网上搜了几篇开发 WordPress 插件的入门文章,照猫画虎、东拼西凑地终于把这个插件发布了。虽然还比较粗糙,但它确实能正常运行了。

写这个插件是因为我所用的空间不能使用外部的 SMTP 发送邮件,而现有的插件提供的可配置的邮件参数都不能满足我的要求。比如,设置回复路径(Return-Path)。于是我就翻看 WordPress 的源代码,发现原来 WordPress 在发送邮件的 wp_mail() 函数中使用的是 PHPMailer 类。PHPMailer 类提供了丰富的可配置参数。通过 PHPMailer ,用户可以完全控制发邮件的全过程,以及邮件的所有头部信息。值得庆幸的是,WordPress 早在 V2.2 版的时候就提供了 phpmailer_init 钩子。V3.0 版本中,在 wp-includes/pluggable.php 的 461 行,如下:

do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );

// Send!
$result = @$phpmailer->Send();

利用这个钩子,我们就可以在邮件被发送出去之前,完全地控制和修改它了。

下面是 PHPMailer 类中所用的成员变量。这些变量影响着邮件的几乎所有方面,如果发送方式、头部信息、内容等等。本插件就是通过上面的钩子改变 PHPMailer 的这些成员变量来控制邮件的发送的。所以,本插件的功能和下面这些变量一样非常丰富,但也仅局限于这些。


///////////////////////////////////////////////// // PROPERTIES, PUBLIC ///////////////////////////////////////////////// /** * Email priority (1 = High, 3 = Normal, 5 = low). * @var int */ var $Priority = 3; /** * Sets the CharSet of the message. * @var string */ var $CharSet = 'iso-8859-1'; /** * Sets the Content-type of the message. * @var string */ var $ContentType = 'text/plain'; /** * Sets the Encoding of the message. Options for this are "8bit", * "7bit", "binary", "base64", and "quoted-printable". * @var string */ var $Encoding = '8bit'; /** * Holds the most recent mailer error message. * @var string */ var $ErrorInfo = ''; /** * Sets the From email address for the message. * @var string */ var $From = 'root@localhost'; /** * Sets the From name of the message. * @var string */ var $FromName = 'Root User'; /** * Sets the Sender email (Return-Path) of the message. If not empty, * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode. * @var string */ var $Sender = ''; /** * Sets the Subject of the message. * @var string */ var $Subject = ''; /** * Sets the Body of the message. This can be either an HTML or text body. * If HTML then run IsHTML(true). * @var string */ var $Body = ''; /** * Sets the text-only body of the message. This automatically sets the * email to multipart/alternative. This body can be read by mail * clients that do not have HTML email capability such as mutt. Clients * that can read HTML will view the normal Body. * @var string */ var $AltBody = ''; /** * Sets word wrapping on the body of the message to a given number of * characters. * @var int */ var $WordWrap = 0; /** * Method to send mail: ("mail", "sendmail", or "smtp"). * @var string */ var $Mailer = 'mail'; /** * Sets the path of the sendmail program. * @var string */ var $Sendmail = '/usr/sbin/sendmail'; /** * Path to PHPMailer plugins. This is now only useful if the SMTP class * is in a different directory than the PHP include path. * @var string */ var $PluginDir = ''; /** * Holds PHPMailer version. * @var string */ var $Version = "2.0.4"; /** * Sets the email address that a reading confirmation will be sent. * @var string */ var $ConfirmReadingTo = ''; /** * Sets the hostname to use in Message-Id and Received headers * and as default HELO string. If empty, the value returned * by SERVER_NAME is used or 'localhost.localdomain'. * @var string */ var $Hostname = ''; /** * Sets the message ID to be used in the Message-Id header. * If empty, a unique id will be generated. * @var string */ var $MessageID = ''; ///////////////////////////////////////////////// // PROPERTIES FOR SMTP ///////////////////////////////////////////////// /** * Sets the SMTP hosts. All hosts must be separated by a * semicolon. You can also specify a different port * for each host by using this format: [hostname:port] * (e.g. "smtp1.example.com:25;smtp2.example.com"). * Hosts will be tried in order. * @var string */ var $Host = 'localhost'; /** * Sets the default SMTP server port. * @var int */ var $Port = 25; /** * Sets the SMTP HELO of the message (Default is $Hostname). * @var string */ var $Helo = ''; /** * Sets connection prefix. * Options are "", "ssl" or "tls" * @var string */ var $SMTPSecure = ""; /** * Sets SMTP authentication. Utilizes the Username and Password variables. * @var bool */ var $SMTPAuth = false; /** * Sets SMTP username. * @var string */ var $Username = ''; /** * Sets SMTP password. * @var string */ var $Password = ''; /** * Sets the SMTP server timeout in seconds. This function will not * work with the win32 version. * @var int */ var $Timeout = 10; /** * Sets SMTP class debugging on or off. * @var bool */ var $SMTPDebug = false; /** * Prevents the SMTP connection from being closed after each mail * sending. If this is set to true then to close the connection * requires an explicit call to SmtpClose(). * @var bool */ var $SMTPKeepAlive = false; /** * Provides the ability to have the TO field process individual * emails, instead of sending to entire TO addresses * @var bool */ var $SingleTo = false; ///////////////////////////////////////////////// // PROPERTIES, PRIVATE ///////////////////////////////////////////////// var $smtp = NULL; var $to = array(); var $cc = array(); var $bcc = array(); var $ReplyTo = array(); var $attachment = array(); var $CustomHeader = array(); var $message_type = ''; var $boundary = array(); var $language = array(); var $error_count = 0; var $LE = "\n"; var $sign_cert_file = ""; var $sign_key_file = ""; var $sign_key_pass = "";

在写这个插件的过程中,我搜索了很多资料,发现了一些很有特色的插件,比如“wp mail from”这个插件就是非常的简洁,设置且仅设置邮件的 FromFrom Name 参数。界面和代码都非常的干净。以后我也会向他学习。还有一些插件做的非常炫,功能非常健全,界面优美,但也相对复杂一些。比如“Google XML Sitemaps” 。本着学习的目的,以及因为最初写插件的新鲜感,在本版本的插件中,我也加入了一些可有可无的东西。比如在配置页面的右上方加了个“关于本插件”侧边栏(遗憾的是,在 Chrome 下显示不正常。不单是 PHP 小白,也是 CSS 小白哈。)。这些东西在将来的版本中可能会删掉,还插件一个简洁的面目。

好了,来张截图吧。

WP Mail Options

怎么样?面对这么多的选项,这么自由的可定制性,是不是心动了呢?那就安装上试一下吧~!目前本插件有英文版和简体中文版哦~

插件的信息如下:

插件名:WP Mail Options

插件地址:http://wordpress.org/extend/plugins/wp-mail-options/

版本:V0.1.0

作者:Soli

作者主页:http://www.cbug.org

插件主页:http://www.cbug.org/category/wp-mail-options

下载地址:http://downloads.wordpress.org/plugin/wp-mail-options.zip

本插件欢迎各位 WordPress 爱好者的加入。也希望有能力的同学将其翻译成其他语言的版本。下载包中有 po 文件的。不胜感激!

如果在使用过程中有任何问题,或意见和建议,欢迎在下面留言。如果只是想和作者聊聊,也是欢迎的。

或微博联系:http://t.qq.com/allnullhttp://t.sina.com.cn/allnull

Creative Commons License Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 4.0 International license .