最土团购系统下单后自动发送邮件

今天在猪八戒上,看到有人发布任务,要求二次开发最土团购系统,用户下单支付成功后,需要发送通知邮件,闲的没事,简单看了一下,感觉很好实现。在这里把基本思路说一下。

首先,编写邮件内容函数和模板。这个可以参考订阅邮件的发送,在/include/fuction/mailer.php这个文件里,照猫画虎,添加一个处理改邮件的函数。

function mail_order($city, $team, $partner, $order,$user){}

然后再新建一个模板,mail_order.html,具体表现形式,通过css控制即可。

函数和模板搞定,只需要调用函数,就可以将邮件发送出去了。

每一订单付款后,系统都会调用统一的一个方法,static public function BuyOne($order),这个方法在ZTeam.class这个类里,只需要在这个方法的最后调用mail_order即可在订单付款之后,将邮件发送出去。

补充:有朋友建议我再写详细一点,那我就把代码贴出来好了。

在/include/fuction/mailer.php最后面,添加如下代码

//订单付款后,发送邮件通知 
function mail_order($team$partner$order)  

    global $INI
    $encoding = $INI['mail']['encoding'] ? $INI['mail']['encoding'] : 'UTF-8'; 
    $week = array('日','一','二','三','四','五','六'); 
    $today = date('Y年n月j日 星期') . $week[date('w')]; 
    $user = Table::Fetch('user',$order['user_id']);//获取用户信息,主要是为了得到Email 
    /*获取优惠券*/
    $condition = array(  
            'order_id' => $order['id'], 
            ); 
    $coupons = DB::LimitQuery('coupon', array
                'condition' => $condition
                )); 
    /*end*/
    $vars = array
        'today' => $today
        'team' => $team
        'city' => $city
        'order' => $order
        'partner' => $partner
        'coupons'=>$coupons
        'help_email' => $INI['mail']['helpemail'], 
        'help_mobile' => $INI['mail']['helpphone'], 
        'notice_email' => $INI['mail']['reply'], 
    ); 
    $message = render('mail_order', $vars); 
    $options = array
        'contentType' => 'text/html', 
        'encoding' => $encoding
    ); 
    $from = $INI['mail']['from']; 
    $to = $user['email']; 
    $subject = '您在'.$INI['system']['sitename'].'的订单信息'; 
  
    if ($INI['mail']['mail']=='mail') { 
        Mailer::SendMail($from$to$subject$message$options); 
    } else { 
        Mailer::SmtpMail($from$to$subject$message$options); 
    } 
}

然后在include/template/目录下,新建一个模板文件mail_order.html。这个文件里,大家需要自己修改一下css代码,以便显示风格,和自己的网站相匹配。我随便写了一下,比较难看,只是把内容订单内容列出来了。代码比较多,我放到附件里,大家自行下载。

mail_order.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="robots" content="noindex, nofollow">
</head>
<body bgcolor="#ffffff" style='padding:10px; 720px; margin:auto;'>
<style type='text/css'>
body 
{font-family:Helvetica, Arial, sans-serif; color:#6C6C6C;}
{ color: #000; text-decoration:none;}
a:hover 
{ color: #EA0886; text-decoration:none; }
{ line-height: 1.5em; }
</style>

<table cellpadding='0' cellspacing='0' width='720px'>
    <tr>
        <td>
            <style='margin:0; padding:0; font-size:14px; color:#000; font-family: Helvetica, Arial, sans-serif;text-align:center;font-size:12px; color:#929292; '>请把 <href="mailto:{$notice_email}" style="" title="">{$notice_email}</a> 加到您的邮箱联系人中,以确保能正确接收此邮件。</p>
        </td>
    </tr>
</table>

<!-- content -->
<div style='670px; margin:5px 0; padding: 20px 20px 20px 20px; background-color:#fff; border-5px; border-style: solid; border-color:#deedcc;-moz-border-radius:10px;-webkit-border-radius:10px;font-size:14px;'>

<table>
<tr>
    <th width="200">订单编号:{$order['id']}</th><th width="200">支付单号:{$order['pay_id']}</th><th width="200">订单选项:{$order['condbuy']}</th>
</tr>
<tr style="margin-top:20px;">
    <th width="200" >下单时间:${date('Y-m-d H:i',$order['create_time'])}</th><th width="200">付款时间:${date('Y-m-d H:i',$order['pay_time'])}</th><th width="200">幸运号码:{$order['luky_id']}</th>
</tr>
</table>
<table style="margin-top:20px;margin-bottom:20px;">
<tr>
    <th width="200">项目名称</th><th>单价</th><th>数量</th><th>总价</th><th>状态</th>
</tr>
<tr>
    <th><href="/team.php?id={$team['id']}" target="_blank">{$team['title']}</a></th><th>{$team['team_price']}</th><th>{$order['quantity']}</th><th>{$team['team_price']*$order['quantity']}</th><th><!--{if $order['state']=='pay'}-->成功<!--{else}-->失败<!--{/if}--></th>
</tr>
</table>
<!--{if $team['delivery']=='express'}-->
<table>
  <tr>
    <th>收货地址:{$order['address']}</th>
  </tr>
   <tr>
    <th>卖家留言:{$order['remark']}</th>
  </tr>
</table>
<!--{elseif $team['delivery']=='coupon'}-->
<table>
<!--{loop $coupons $index $one}-->
  <tr>
    <th>优惠券编码:{$one['id']}</th><th>密码:{$one['secret']}</th>
  </tr>
<!--{/loop}-->

</table>
<!--{/if}-->
<table style='background-color:#deedcc; margin-top:2px; -moz-border-radius-bottomleft:8px; -moz-border-radius-bottomright:8px; -webkit-border-bottom-left-radius:8px; -webkit-border-bottom-right-radius:8px;' width='100%'>
    <tr>
        <td style='font-family: Helvetica, Arial, sans-serif; color:#545454; font-size:12px; text-align:center; line-height:16px; padding:10px;'>电子邮箱:<href="mailto:{$help_email}" style="" title="">{$help_email}</a>&nbsp;&nbsp;&nbsp; 客服电话:123456&nbsp;&nbsp;<span style='font-weight:normal; font-size:12px;'>星期一至星期五 9:30-18:00</span></td>
    </tr>
</table>

</div>
</body>
</html>

最后,在include/classes/ZTeam.class.php中找到static public function BuyOne这个函数,在ZCredit::Buy($order['user_id'], $order);这行代码后面添加:

$partner = Table::FetchForce(‘partner’, $team['partner_id']);

mail_order($team, $partner, $order) ;

这样,购买之后,就可以自动发送邮件通知了。

其实可以做进一步的升级,比如,用户下单之后,马上发一个通知邮件,付款成功之后,再发一个通知邮件。做一些设置,某个项目是否自动发送邮件,等等。

原文地址:https://www.cnblogs.com/jifeng/p/2625735.html