CakePHP电子邮件smtp没有发送

时间:2017-08-04 23:04:52

标签: php cakephp smtp cakephp-2.0

我遇到问题,CakePHP smtp没有发送任何电子邮件,而且我使用的是谷歌smtp服务。

我使用的Cake版本是CakePHP v2.4.1

用户注册后,它会调用CronJob并在ApiController中运行电子邮件功能。

这是它正在调用的函数:

    public function processEmailQueue(){ 

      ScriptLockComponent::lock(__FUNCTION__);
      $emails = $this->Email->find('all', array(
        'conditions' => array(
            'Email.status' => Email::STATUS_NOT_SEND,
            'Email.instance_id' => ''
        ),
        'recursive' => 1,
        'order' => array('Email.priority DESC'),
        'limit' => self::BATCH_LIMIT
            )
    );      
    // creating new email object
    $cakeEmail = new CakeEmail();


   $date = new DateTime();
    foreach ($emails as $emailData) {
    $cakeEmail->reset();
        $priorityValue = intval($emailData["Email"]["priority"]) + 1;
        //Getting email template from database
        $emailManagement = $this->EmailTemplate->getEmailTemplate($emailData["Email"]["email_template_id"], json_decode($emailData["Email"]["content"], TRUE));
        // setting email configurations, and sending email

        // setting unsubscribe url in the mail footer
        $templateId = $emailData["Email"]["email_template_id"];
        if (in_array($templateId, $this->emailTemplateIds)) {
            $autoLoginToken = $this->Otp->createOTP(array(
                'email' => $emailData["Email"]["to_email"]
            ));
            $email = base64_encode($emailData["Email"]["to_email"]);
            $unsubscribeUrl = Router::Url('/', TRUE) . 'unsubscribe?setting=' .$templateId. '&auto_login_token=' . $autoLoginToken .'&email=' . $email ;
        } else {
            $unsubscribeUrl = '';
        }
        try {
            $cakeEmail->config('gmail')
                    //->template('default')
                    ->viewVars(array('unsubscribe' => $unsubscribeUrl ))
                    ->emailFormat('html')
                    ->to($emailData["Email"]["to_email"])
                    ->subject($emailManagement['EmailTemplate']['template_subject'])
                    ->setHeaders(array('List-Unsubscribe' => $unsubscribeUrl))
                    ->send($emailManagement['EmailTemplate']['template_body']);
            $this->Email->set(array(
                'id' => $emailData["Email"]["id"],
                'sent_date' => $date->format('Y-m-d h:i:s'),
                'status' => Email::STATUS_SEND
            ));
            $this->Email->save();
            } catch (Exception $e) {
            $this->Email->set(array(
                'id' => $emailData["Email"]["id"],
                'priority' => $priorityValue,
                'status' => Email::STATUS_NOT_SEND
            ));
            $this->Email->save();
        }
    }

问题在于没有发送电子邮件,为什么会出现这种情况。

谢谢你, 凯文

1 个答案:

答案 0 :(得分:0)

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="dataView">
     <table>
         <thead>
              <tr>
                  <th>URL</th>
              </tr>
          </thead>
          <tbody data-bind="foreach: data">
              <tr>
              <td><span data-bind="text: urlIsValid"></span></td>
                  <!-- ko if: urlIsValid -->
                      <td>
                          <a data-bind="attr: {href: location}" target="_blank">
                              Download
                          </a>
                      </td>
                  <!-- /ko -->
                  <!-- ko ifnot: urlIsValid -->
                      <td data-bind="text: location"></td>
                  <!-- /ko -->
              </tr>
          </tbody>
      </table>
 </div>