duan20081202 2015-10-30 17:08
浏览 44
已采纳

CakePHP - 发送邮件

I hav run into a problem - I am not that familiar with cakephp - but I have taken over a project (A webshop project) - and now I have a bit of a problem - I have a field (tracking id for the package) which writes to DB - that works great - but here is the trouble - when I click the button and save the tracking number to the DB - I wan't the system to send an email to the customer with the tracking id. I can get the system to send a mail - but there is no tracking number on so I guess the email is send before the DB i updated. Hope someone can point me in the right direction

 public function send($order_id = null, $is_resolved = true)
{
    if($this->request->is('post'))
    {             
        $this->Order->id = $this->request->data['Order']['id'];
        $this->Order->saveField('state', 'resolved');
        $this->Order->saveField('date_resolved', date('Y-m-d H:i:s'));
        $this->Order->saveField('delivery_label', $this->request->data['Order']['delivery_label']);
        $this->Session->setFlash('Ordren er nu sat til "Sendt".'.$this->Session->read('Message.success.message'), null, array(), 'success');
        $this->redirect(array('controller' => 'orders', 'action' => 'index'));
    } 

    $order = $this->Order->find('first', array('conditions' => array('Order.id' => $order_id, 'Order.state' => 'packed'), 'recursive' => 4));
    // Does the item exist?
    if(empty($order))
    {
        // Tell the user this item did not exist
        $this->Session->setFlash('Ordren findes ikke.'.$this->Session->read('Message.warning.message'), null, array(), 'warning');
        return $this->redirect($this->referer());
    }

    if($is_resolved) {
        // Send the email receipt
        $Email = new CakeEmail('receipt');
        $Email->to($order['Customer']['email_address']);
        $Email->template('receipt_send')->viewVars( array('order' => $order));
        $Email->send(); 
    }

    $this->set('order', $order);
}

Here is the receipt_send code

    <table style="width:100%; margin-bottom:30px; border-collapse: collapse; color:#777;">
    <tr style="vertical-align:middle; border-bottom:1px solid #777;">
        <td colspan="3" style="width:45%; padding:10px 0;"><?php echo $this->Html->image('logo.png', array('style' => 'height:50px; margin-top:-15px; vertical-align:middle;', 'fullBase' => true)); ?></td>
    </tr>
    <tr style="font-size:1em; border-bottom:1px solid #777;">
        <td style="padding: 5px 0;"><strong>Kvittering</strong> fra Bundgaards Garn</td>
        <td style="padding: 5px 0;">Ordre nr. <strong><?php echo $order['Order']['id']; ?></strong></td>
    </tr>

<p style="width:50%; float:right;">Din ordre er afsendt og har fået trackingnummer: <strong><?php echo $order['Order']['delivery_label']; ?></strong></p>
<table style="text-align: left; width:100%; border-collapse: collapse;">
    <tr style="border-bottom:2px solid #dddddd;">
        <th style="width:35%; padding: 10px 0;">Vare</th>
        <th style="width:25%; padding: 10px 0;">Antal</th>
        <th style="width:20%; padding: 10px 0; text-align:right;">Pris</th>
        <th style="width:20%; padding: 10px 0; text-align:right;">Total</th>
    </tr>
    <?php foreach ($order['OrderItem'] as $key => $order_item): ?>
    <tr style="border-bottom:1px solid #dddddd;">
        <!-- PRODCUT LINK -->
        <td style="padding: 10px 0; vertical-align: middle;">
        <?php if($order_item['yarn_batch_id']  != 0) : ?>
            <?php echo $order_item['YarnBatch']['YarnVariant']['Yarn']['name'] . ' - ' . $order_item['YarnBatch']['YarnVariant']['color_code'] . '-' . $order_item['YarnBatch']['batch_code']; ?>
        <?php elseif($order_item['needle_variant_id']  != 0) : ?>
            <?php echo $order_item['NeedleVariant']['Needle']['name'] . ' - ' . $order_item['NeedleVariant']['product_code']; ?>
        <?php elseif($order_item['recipe_id']  != 0) : ?>
            Print af <?php echo $order_item['Recipe']['name']; ?>
        <?php elseif($order_item['color_sample_id']  != 0) : ?>
            Farveprøve af <?php echo $order_item['ColorSample']['name']; ?>
        <?php endif; ?>
        </td>
        <!-- PRODCUT LINK COLLAPSE -->

        <td style="padding: 10px 0; vertical-align: middle;">
            <?php echo $order_item['amount']; ?>
        </td>

        <td style="padding: 10px 0; vertical-align: middle; text-align:right;">
        <?php if($order_item['yarn_batch_id']  != 0) : ?>
            <?php echo $this->Number->currency($order_item['YarnBatch']['price'], 'DKK');?>
        <?php elseif($order_item['needle_variant_id']  != 0) : ?>
            <?php echo $this->Number->currency($order_item['NeedleVariant']['price'], 'DKK');?>
        <?php elseif($order_item['recipe_id']  != 0) : ?>
            <?php echo $this->Number->currency($order_item['Recipe']['price'], 'DKK');?>
        <?php elseif($order_item['color_sample_id']  != 0) : ?>
            <?php echo $this->Number->currency($order_item['ColorSample']['price'], 'DKK');?>
        <?php endif; ?>
        </td>

        <td style="padding: 10px 0; vertical-align: middle; text-align:right;" >
            <?php echo $this->Number->currency($order_item['price'], 'DKK');?>
            <?php if($order_item['saving'] > 0) : ?>
                <br/>
                <span style="color:#5cb85c;"><?php echo $this->Number->currency($order_item['saving'], 'DKK');?>´</span>
            <?php endif; ?>
        </td>
    </tr>

<?php endforeach; ?>
    <tr>
        <td colspan="2"/>
        <td style="text-align:right;">
            <p>Levering</p>
            <?php if($order['Order']['saving'] > 0) : ?>
                <p>Rabat</p>
            <?php endif; ?>
            <p><strong>Subtotal</strong></p> 
            <p><strong>Moms</strong></p>
        </td>
        <td style="text-align:right;">
            <p><?php echo $this->Number->currency($order['Order']['shipping_price'], 'DKK');?></p>
            <?php if($order['Order']['saving'] > 0) : ?>
                <p style="color:#5cb85c;"><strong><?php echo $this->Number->currency($order['Order']['saving'], 'DKK');?></strong></p>
            <?php endif; ?>
            <p><strong><?php echo $this->Number->currency($order['Order']['sub_total'], 'DKK');?></strong></p>
            <p><strong><?php echo $this->Number->currency($order['Order']['tax'], 'DKK');?></strong></p>
        </td>
    </tr>
    <tr>
        <td colspan="2"/>
        <td style="text-align:right;">
            <h4>Total:</h4> 
        </td>
        <td style="text-align:right; color:#900000;">
            <h4><strong><?php echo $this->Number->currency($order['Order']['price'], 'DKK');?></strong></h4>
        </td>
    </tr>
</table>

    <div style="width:50%; float:left;">
        <h4>Ordren sendes til:</h4>
        <p>
          <strong><?php echo $order['Customer']['first_name'] . ' ' . $order['Customer']['last_name']; ?></strong><br>
          <?php echo $order['Customer']['ShippingAddress']['street'] . ' ' .
                     $order['Customer']['ShippingAddress']['zip_code'] . ' ' .
                     $order['Customer']['ShippingAddress']['city_name']?><br>
      <?php echo $order['Customer']['email_address']; ?><br>
      <?php echo $order['Customer']['phone_number']; ?><br>
    </p>
</div>

<div style="width:50%; float:left;">
<?php if(!empty($order['Order']['customer_note'])) : ?>
    <h4>Note til forhandleren:</h4>
    <p>
      <?php echo $order['Order']['customer_note']; ?>
    </p>
<?php endif; ?>
</div>

<hr style="width:100%; border: 0; border-bottom: 1px solid #ddd;">

<p style="color:#999; font-size:0.8em;">
<strong>Bundggards Garn</strong> <br/>
<a style="color:#777;" href="https://bundgaardsgarn.dk">bundgaardsgarn.dk</a> <br/>
<a href="mailto:kontakt@bundgaardsgarn.dk" style="color:#777;"> kontakt@bundgaardsgarn.dk</a> <br/>
Saltumvej 46 9700 Brønderslev <br/>
30369522 <br/>
<i>CVR:</i> 34526826 <br/>
</p>
  • 写回答

1条回答 默认 最新

  • douli4852 2016-01-08 07:12
    关注

    Got it to work by creating a controller and make a button to the information.... Works like a charm.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型