php 发送模版邮件

本想cakephp的emailComponent,但是由于Server 只有发送邮件功能,又不想有过多的冗余代码。所以放弃cakephp 框架
下面的方法不错。
出处 http://stackoverflow.com/questions/3706855/send-email-with-a-template-using-php
还是老外牛啊。

public function __set($key,$val)  是亮点。

http://stackoverflow.com/questions/3706855/send-email-with-a-template-using-php

class Emailer
{
    var $recipients = array();
    var $EmailTemplate;
    var $EmailContents;
 
    public function __construct($to = false)
    {
        if($to !== false)
        {
            if(is_array($to))
            {
                foreach($to as $_to){ $this->recipients[$_to] = $_to; }
            }else
            {
                $this->recipients[$to] = $to; //1 Recip
            }
        }
    }
 
    function SetTemplate(EmailTemplate $EmailTemplate)
    {
        $this->EmailTemplate = $EmailTemplate;
 
        //When you use the send( ) function you would use $EmailTemplate->compile();
    }
}

======================================================================= 

class EmailTemplate
{
    var $variables = array();
    var $path_to_file= array();
    function __construct($path_to_file)
    {
         if(!file_exists($path_to_file))
         {
             trigger_error('..blah...',E_USER_ERROR);
             return;
         }
         $this->path_to_file = $path_to_file;
    }
 
    public function __set($key,$val)
    {
        $this->variables[$key] = $val
    }
 
 
    public function compile()
    {
        ob_start();
 
        extract($this->variables);
        include $this->path_to_file;
 
 
        $content = ob_get_contents();
        ob_end_clean();
 
        return $contents;
    }
}

=======================================================
$emails = array(
    'bob@bobsite.com',
    'you@yoursite.com'
);
 
$Emailer = new Emailer($emails);
 //More code here
 
$Template = new EmailTemplate('path/to/my/email/template');
    $Template->Firstname = 'Robert';
    $Template->Lastname = 'Pitt';
    $Template->LoginUrl= 'http://stackoverflow.com/questions/3706855/send-email-with-a-template-using-php';
    //...
 
$Emailer->SetTemplate($Template); //Email runs the compile
Emailer->send();

//Thats really all there is to it, just have to know how to use objects and its pretty simple from there, ooh and the template would look a little something like this:

Welcome to my site,
 
Dear <?php echo $Firstname ?>, You have been registered on our site.
 
Please visit <a href="<?php echo $LoginUrl ?>">This Link</a> to view your upvotes
 
Regards.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值