linux/window配置php PEAR:Mail发送html邮件

介绍一下linux和windows下配置PEAR:Mail,用来发送html邮件,做记录备份。

大体上配置分为以下几步:

  • 安装pear(高版本的php里已经自带安装pear)
  • pear安装Mail,Mail_Mime,Net_SMTP
  • Yii框架组件代码测试

(一)安装pear

(1)Windows系统

本人php安装路径D:\local\PHPnow\php-5.2.14-Win32,版本5.2.14.

D:\local\PHPnow\php-5.2.14-Win32查看路径下是否有pear.bat,如果没有说明未安装,参考一下步骤。

  1. 下载go-pear,http://pear.php.net/go-pear,并重命名为go-pear.php,放到D:\local\PHPnow\php-5.2.14-Win32目录下;
  2. 打开cmd,cd到D:\local\PHPnow\php-5.2.14-Win32,运行php.exe go-pear.php,根据提示,一路回车直到安装完成;
  3. 双击生成的D:\local\PHPnow\php-5.2.14-Win32目录下的PEAR_ENV.reg,导入相关信息到注册表;
  4. 修改php.ini,include_path,如下:
    ; UNIX: "/path1:/path2"
    ;include_path = ".:/php/includes"
    ;
    ; Windows: "\path1;\path2"
    include_path = ".;D:\local\PHPnow\php-5.2.14-Win32\PEAR"
  5. 重启apache

(2)linux系统

本人测试Red Hat Enterprise Linux Server release 5.4 (Tikanga),php安装路径/usr/local/php,版本5.2.17

首先验证/usr/local/php/bin目录下是否有pear文件,如果有,则说明已经安装。php高版本已经可以自带安装pear,php5.2.17便自带安装。


如果未安装,请参考:

【以下转载自】http://blog.chinaunix.net/uid-25266990-id-3393387.html

在搭建centreon的过程中,需要pear模块支持。
什么是pear
pear是PHP扩展与应用库(the PHP Extension and Application Repository)的缩写。它是一个PHP扩展及应用的一个代码仓库,简单地说,
pear就是PHP的cpan。
在官网上有说明详细的安装信息,这里作简单说明。
http://pear.php.net/manual/en/about-pear.php
我的PHP目录为/usr/local/php5
在Linux下安装PHP的PEAR:
1)下载
#curl -o go-pear.php  http://pear.php.net/go-pear
如果提示:
PHP Warning:  PHP Startup: Invalid library (maybe not a PHP library) ‘json.so’  in Unknown on line 0
Sorry!  Your PHP version is too new (5.2.9) for this go-pear.
Instead use http://pear.php.net/go-pear.phar for a more stable and current
version of go-pear, more suited to your PHP version.
那么要从http://pear.php.net/go-pear.phar获取。
#curl -o go-pear.php http://pear.php.net/go-pear.phar
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100 3594k  100 3594k    0     0   186k      0  0:00:19  0:00:19 –:–:–  196k
会在当前目录下载go-pear.php 页面。
2)运行go-pear.php
# /usr/local/php5/bin/php go-pear.php
3)这里按回车继续安装,CTRL+C放弃安装。
Below is a suggested file layout for your new PEAR installation.  To
change individual locations, type the number in front of the
directory.  Type ‘all’ to change all of them or simply press Enter to
accept these locations.
1. Installation base ($prefix)                   : /usr/local/php5
2. Temporary directory for processing            : /tmp/pear/install
3. Temporary directory for downloads             : /tmp/pear/install
4. Binaries directory                            : /usr/local/php5/bin
5. PHP code directory ($php_dir)                 : /usr/local/php5/lib/php
6. Documentation directory                       : /usr/local/php5/docs
7. Data directory                                : /usr/local/php5/data
8. User-modifiable configuration files directory : /usr/local/php5/cfg
9. Public Web Files directory                    : /usr/local/php5/www
10. Tests directory                               : /usr/local/php5/tests
11. Name of configuration file                    : /usr/local/php5/etc/pear.conf
1-11, ‘all’ or Enter to continue:
Beginning install…
Configuration written to /usr/local/php5/etc/pear.conf…
Initialized registry…
Preparing to install…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/Archive_Tar-1.3.7.tar…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/Console_Getopt-1.3.0.tar…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/PEAR-1.9.4.tar…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/Structures_Graph-1.0.4.tar…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/XML_Util-1.2.1.tar…
install ok: channel://pear.php.net/Archive_Tar-1.3.7
install ok: channel://pear.php.net/Console_Getopt-1.3.0
install ok: channel://pear.php.net/Structures_Graph-1.0.4
install ok: channel://pear.php.net/XML_Util-1.2.1
install ok: channel://pear.php.net/PEAR-1.9.4
PEAR: Optional feature webinstaller available (PEAR’s web-based installer)
PEAR: Optional feature gtkinstaller available (PEAR’s PHP-GTK-based installer)
PEAR: Optional feature gtk2installer available (PEAR’s PHP-GTK2-based installer)
PEAR: To install optional features use “pear install pear/PEAR#featurename”
** WARNING! Old version found at /usr/local/php5/bin, please remove it or be sure to use the new /usr/local/php5/bin/pear command
The ‘pear’ command is now at your service at /usr/local/php5/bin/pear
** The ‘pear’ command is not currently in your PATH, so you need to
** use ‘/usr/local/php5/bin/pear’ until you have added
** ‘/usr/local/php5/bin’ to your PATH environment variable.
Run it without parameters to see the available actions, try ‘pear list’
to see what packages are installed, or ‘pear help’ for help.
For more information about PEAR, see:
http://pear.php.net/faq.php
http://pear.php.net/manual/
 

重启apache。


(二)pear安装pear安装Mail,Mail_Mime,Net_SMTP

            接着用pear安装插件,我们可以在http://pear.php.net/packages.php网站上查看pear可安装的包,为了发送html邮件,我们只需要安装Mail,Mail_Mime,Net_SMTP(Networking)。安装Net_SMTP的时候会自动安装Net_Socket.

           安装过程比较简单:

           Windows:用cmd执行

D:\local\PHPnow\php-5.2.14-Win32\pear.bat install Mail
D:\local\PHPnow\php-5.2.14-Win32\pear.bat install Mail_Mime
D:\local\PHPnow\php-5.2.14-Win32\pear.bat install Net_SMTP




           linux类似:

/usr/local/php/bin/pear install Mail
/usr/local/php/bin/pear install Mail_Mime
/usr/local/php/bin/pear install Net_SMTP


安装完毕重启apache,接下来便是测试了。



(三)Yii框架组件代码测试

            先可以参考官网示例:http://pear.php.net/manual/en/package.mail.mail-mime.example.php

 <?php

include 'Mail.php';
include 'Mail/mime.php' ;

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = '/home/richard/example.php';
$crlf = "\n";
$hdrs = array(
              'From'    => 'you@yourdomain.com',
              'Subject' => 'Test mime message'
              );

$mime = new Mail_mime(array('eol' => $crlf));

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('postmaster@localhost', $hdrs, $body);

?> 

             如以上成功发送邮件,说明php pear mail安装配置成功~

           

            测试成功可发送邮件以后,我们探讨一下如何在Yii框架中配置通用邮件发送模块。

             我们使用smarty模版引擎来生成html邮件中的htmlbody内容,搭建Yii框架邮件发送的通用模块,关键在于邮箱账户轻松配置、新邮件内容的smarty模版页面容易添加、邮件发送接口方便调用。Smarty模版引擎的使用这里不再赘述。

             先看看要实现的效果:           

<?php

class SiteController extends Controller
{
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = "//layouts/column2";

    public function actionTest()
    {
        try {
            $model = new FormModel();
            $model->_test = 1; //此处为FormModel里变量,用于渲染到smarty模版里

            $mailSettings = array (
                'resetPassword' => array(
                    'name'=>'wefuture',
                    'from'=>'support@qq.com',
                    'to'=>'support@qq.com',
                    'cc'=>"",
                    'vipcc'=>"",
                    'subject'=>'重置密码',
                    'template'=>'resetPassword.htm',
                    'attachments'=>array(),
                ),

            );
         
            $model->sendMail($mailSettings, 'resetPassword', "support@qq.com");
            
        } catch(Exception $e) {
            var_dump($e->getMessage());
        }

    }
}
 如上,发送一封邮件,我们只需要关心好好的填写邮件配置信息,写好“resetPassword”模版引擎内容渲染到邮件中即可,怎么样,是不是很方便。

接着我们看看FormModel里函数做了些什么:

<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class FormModel extends CFormModel
{
    public $_test;

	public function init() {
	}

	/**
	 * auto trim all
	 */
	public function setAttributes($values, $safeOnly=true)
	{
		if (is_array($values)) {
			foreach ($values as $name => $val) {
				if (is_string($val)) {
					$values[$name] = trim($val);
				}
			}
		}
		return parent::setAttributes($values,$safeOnly);
	}

	/**
	 * auto trim all
	 */
	public function beforeValidate()
	{
		if (is_array($this->attributes)) {
			foreach ($this->attributes as $name => $val) {
				if (is_string($val)) {
					$this->$name = trim($val);
				}
			}
		}
		return parent::beforeValidate();
	}

	/**
	 * 发送邮件函数
	 *
	 */
	public function sendMail($mailSettings, $settingName, $TO = null, $CC = null ,$setPage=1) {
		if(!isset($mailSettings[$settingName])){
			echo "mail setting for $settingName not found!";
			die();
		}
		$mailSetting = $mailSettings[$settingName];

		$attachments = array();
		if (isset($mailSetting['attachments']) && $setPage == 1) {
			foreach($mailSetting['attachments'] as $key=>$value) {
				$attachments[]=$value;
			}
		}

		if($TO) { //如果指定收件人
			$to = $TO;
		} else {
			$to = $mailSetting['to'];
		}

		if($CC) { //如果指定抄送人
			$cc = $CC;
		} else {
			$cc = $mailSetting['cc'];
		}

		if(isset($this->isContainVip)
				&& $this->isContainVip
				&& isset($mailSetting['vipcc'])
				&& strlen($mailSetting['vipcc']) > 0){
			$vipcc = $mailSetting['vipcc'];
			$cc = $cc . "," . $vipcc;
		}

		$subject = $mailSetting['subject'];
		$from = $mailSetting['from'];
		$name = $mailSetting['name'];
		$html = true;

		$data = array(
			'model' => $this,
		);
		$ret = Yii::app()->messenger->sendSmartyMail($to, $cc, $subject, $mailSetting['template'], $data, $from, $name, $html, $attachments);

		if ($ret == false) {
			$this->addError('email', '发送邮件失败');
			return false;
		} else {
			Yii::log('发送邮件成功, subject: ' . $subject, 'info');
		}
		return true;
	}

}
            代码里用到,Yii::app()->messenger->sendSmartyMail,我在工程里自己配置了组件messager、smarty,看看messager文件类内容:       
<?php

require_once('Mail.php');
require_once('Mail/mime.php');

/**
 * class Messenger.
 *
 * 发送邮件
 *
 * 支持html格式和纯文本格式;
 * 支持使用应用对应的smarty模版生成邮件内容;
 * 支持发送附件
 *
 * 所有的邮件内容,变量使用utf-8编码
 */
class Messenger
{
	public $mailer;
	public $options;

    public function init() {
	    if (empty($this->mailer)) {
			$this->mailer = 'mail';
		}
    }

    /**
     * 发送使用smarty模板的邮件
     *
     * 使用系统配置的smarty模板路径下的 mail/$template.html作为模板文件
     *
     * @param string $to 逗号分隔的收件人列表
     * @param string $subject 邮件的主题
     * @param string $template 模版名称
     * @param array $data 需要替换的模版变量数组($template_var => $template_value)
     * @param string $from 发件人的email
     * @param string $name 发件人的显示名
     * @param boolean $html 邮件是否是html格式,默认为true
     * @param array $attachments 附件文件路径列表,默认为空
     */
    public function sendSmartyMail($to, $cc, $subject, $template, $data, $from, $name, $html=true, $attachments=array()) {
        if (!isset($to) || ($to == "")) {
            return;
        }
        if (!isset($cc) || ($cc == "")) {
        	$cc = '';
        }

        // 可以配置将所有邮件都发到一个邮件地址或列表
        // 用于测试时,避免发出垃圾邮件骚扰大家
        $sendAllMailTo = Yii::app()->params['DEBUG_SEND_ALL_MAIL_TO'];
        if (!empty($sendAllMailTo)) {
            $to = $sendAllMailTo;
            $cc = $sendAllMailTo;
        }

        $smarty = Yii::app()->smarty;
        $smarty->assign($data);
        $content = $smarty->fetch('mail/'.$template);


        $hdrs = array(
                'Return-Path' => $from,
                'From'    => "$name <$from>",
                'To'    => $to,
                'Subject' => "=?UTF-8?B?".base64_encode($subject)."?=",
                'Date' => date("r"),
        );
        if($cc != ""){
            $ccHeader = array('Cc' => $cc);
            $hdrs = array_merge($hdrs, $ccHeader);
        }
        $param = array(
                'text_charset' => 'utf-8',
                'head_charset' => 'utf-8',
                'html_charset' => 'utf-8',
        );


        $mime = new Mail_mime();
        if ($html){
            $mime->setHtmlBody($content);
        }
        else{
            $mime->setTXTBody($content);
        }
        foreach ($attachments as $att) {
            $mime->addAttachment($att);
        }
        $body = @$mime->get($param);
        $hdrs = @$mime->headers($hdrs);

        $email = &Mail::factory($this->mailer, $this->options);

		/**
		 * 2011年3月的Net_SMTP有个bug,对timeout的处理有问题,而PEAR的Mail_smtp类并没有正确的初始化Net_SMTP
         * 导致Net_SMTP对timeout的bug传递出来。如果在smtp对象上调用connect方法后(getSMTPObject以后),
		 * 再调用setTimeout(新版Net_SMTP才有),则问题可以绕过。
		 * 该问题只在PEAR Net_SMTP v1.5.0和v1.5.1上存在
		 * see https://pear.php.net/bugs/bug.php?id=18335&edit=12&patch=Net_SMTP-bug18335.diff&revision=latest
		 */
        $result = &$email->getSMTPObject();
        if (PEAR::isError($result)) {
			Yii::log('初始化SMTP对象失败:'.$result->getMessage(), 'error');
            return $false;
        }
		// workaround Net_SMTP bug
		if (method_exists($result, 'setTimeout')) {
			$result->setTimeout(3600); // 1 hour
		}

		//只有放到recipients里,才会收到邮件。至于显示到To还是Cc由header决定。
        $recipients = isset($hdrs['Cc'])? $hdrs['To'].','.$hdrs['Cc']:$hdrs['To'];
        $ret = $email->send($recipients, $hdrs, $body);
        if (PEAR::isError($ret)) {
            Yii::log('发送邮件错误:'.$ret->getMessage(), 'error');
            return false;
        } else {
        	Yii::log('发送邮件成功,Subject: ' . $subject, 'info');
        }
        return true;
    }

    /**
     * 发送邮件
     *
     * @param string $to 逗号分隔的收件人列表
     * @param string $subject 邮件的主题
     * @param array $content 邮件内容
     * @param boolean $html 邮件是否是html格式,默认为否
     * @param string $from 发件人的email
     * @param string $name 发件人的显示名
     * @param array $attachments 附件文件路径列表
     */
    public function sendMail($to, $subject, $content, $html=false, $from="support@qq.com", $name="wefuture", $attachments=array()) {
        if (!isset($to) || ($to == "")) {
            return;
        }

        // 可以配置将所有邮件都发到一个邮件地址或列表
        // 用于测试时,避免发出垃圾邮件骚扰大家
        $sendAllMailTo = Yii::app()->params['DEBUG_SEND_ALL_MAIL_TO'];
        if (!empty($sendAllMailTo)) {
            $ori = $to;
            $to = $sendAllMailTo;
            $content  = "原邮件接收人:".$ori."\n 现接收人:".$sendAllMailTo." \n 内容是:\n ".$content;
        }
        $hdrs = array(
                'Return-Path' => $from,
                'From'    => "$name <$from>",
                'Subject' => $subject,
                );
        $param = array(
                'text_charset' => 'UTF-8',
                'head_charset' => 'UTF-8',
                'html_charset' => 'UTF-8',
                );

        $mime = new Mail_mime();
        if ($html) {
            $mime->setHtmlBody($content);
        }
        else {
            $mime->setTXTBody($content);
        }
        foreach ($attachments as $att) {
            $mime->addAttachment($att);
        }
        $body = @$mime->get($param);
        $hdrs = @$mime->headers($hdrs);

        $email = &Mail::factory($this->mailer, $this->options);
        $ret = $email->send($to, $hdrs, $body);
        if (PEAR::isError($ret)) {
        	Yii::log('发送邮件错误:'.$ret->getMessage(), 'error');
        	return false;
        }
        return true;
    }

}


上面一个类就挺简单了,用到了smarty、php pear mail模块。最后再贴出一下main.php的config配置加入一下代码:

        'smarty' => array(
                'class'=>'lib.thirdparty.CSmarty',
                'templatePath' => $WEB_BASE_DIR.'/smarty',
        ),
	
	'messenger' => array(
		'class' => 'lib.common.Messenger',
		'mailer' => 'smtp',
		'options' => array(
			'auth' => true,
			'host' => 'smtp.exmail.qq.com',
			'username' => 'support@qq.com',
			'password' => 'xxxxxxxxx',
		),
	),

 至此,基于Yii框架利用php pear mail发送html邮件的配置工作就完成啦~

             以上Yii框架适合对Yii框架经验较丰富者参考,直接复制可能不行很好运行,仅提供一种思路和核心代码。

     



  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

倾木忘忧草

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值