【问题标题】:CakePHP send emailCakePHP 发送电子邮件
【发布时间】:2014-01-17 09:07:58
【问题描述】:

我在使用 CakePHP 发送邮件时遇到问题。一切都很好,但我没有收到任何一封邮件,我厌倦了发送到 2 个不同的电子邮件。

//WebsitesController.php

App::uses('AppController','Controller');
App::uses('CakeEmail','Network/Email');
class WebsitesController extends AppController
{
    public $helpers = array('Html','Form','Session');
    public $components = array('Email','Session');

public function contact()
{

    $this->set('dane',  $this->Website->findById(4));        
}
public function contact_email()
{ /* all data is taken from contact.ctp, I debuged all data below and it's correct */
    $useremail = $this->data['Website']['useremail'];
    $usertopic = $this->data['Website']['usertopic'];
    $usermessage = $this->data['Website']['usermessage'];
    $Email = new CakeEmail();
    $Email->from(array($useremail => ' My Site'));
    $Email->to('wigan@mail.com');
    $Email->subject($usertopic); // all data is correct i checked several times
    $Email->send($usermessage);
    if($Email->send($usermessage))
    {
        $this->Session->setFlash('Mail sent','default',array('class'=>'alert alert-success'));
        return $this->redirect(array('controller'=>'websites','action'=>'contact'));
    }
    $this->Session->setFlash('Problem during sending email','default',array('class'=>'alert alert-warning'));
}
}

//contact.ctp

 <fieldset>
        <?php
            echo $this->Form->create('Website',array('controller'=>'websites','action'=>'contact_email'));
            echo $this->Form->input('useremail',array('class'=>'form-control'));
            echo $this->Form->input('usertopic',array('class'=>'form-control'));
            echo $this->Form->input('usermessage',array('class'=>'form-control'));
            echo $this->Form->submit('Send',array('class'=>'btn btn-default')); 
            echo $this->Form->end(); 
        ?>
    </fieldset>

一切似乎都很好,即使函数 contact_email 中的声明被批准。

配置(我正在使用 localhost、xampp、netbeans 7.4)

public $smtp = array(
    'transport' => 'Smtp',
    'from' => array('site@localhost' => 'My Site'),
    'host' => 'localhost',
    'port' => 25,
    'timeout' => 30,
    'username' => 'user',
    'password' => 'secret',
    'client' => null,
    'log' => false,
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
);

【问题讨论】:

  • 您是否收到“邮件已发送”消息或“发送电子邮件时出现问题”消息?您的服务器是否配置为发送电子邮件?你检查过你的垃圾邮件文件夹吗?
  • 我每次都收到“邮件已发送”通知,我多次检查垃圾邮件,但这里什么都没有。而我的配置在帖子底部
  • 我没有看到像 $Email-&gt;config('smtp'); 这样告诉 cake 使用该配置数组。
  • 这个 conf 文件位于 app/config/email.php 我认为没有必要指向那个文件
  • 否,但它可以包含许多配置数组,所以你必须告诉 cake 你要使用哪个数组

标签: php email cakephp


【解决方案1】:

试试这个,你没有设置配置

public function contact_email()
{ /* all data is taken from contact.ctp, I debuged all data below and it's correct */
    $useremail = $this->data['Website']['useremail'];
    $usertopic = $this->data['Website']['usertopic'];
    $usermessage = $this->data['Website']['usermessage'];
    $Email = new CakeEmail();
    $Email->config('smtp')
          ->emailFormat('html')
          ->from($useremail)        
          ->to('wigan@mail.com')
          ->subject($usertopic); // all data is correct i checked several times
    if($Email->send($usermessage))
    {
        $this->Session->setFlash('Mail sent','default',array('class'=>'alert alert-success'));
        return $this->redirect(array('controller'=>'websites','action'=>'contact'));
    } else  {
        $this->Session->setFlash('Problem during sending email','default',array('class'=>'alert alert-warning'));
    }
}

【讨论】:

    【解决方案2】:

    请按以下步骤操作:

    第 1 步:在此文件中 (app\Config\email.php) 添加这个:

    public $gmail = array(
            'transport' => 'Smtp',
            'from' => array('site@localhost' => 'Any Text...'),
            'host' => 'ssl://smtp.gmail.com',
                    'port' => 465,
            'timeout' => 30,
            'username' => 'youremail@example.com',
            'password' => 'yourPassword',
            'client' => null,
            'log' => false,
            //'charset' => 'utf-8',
            //'headerCharset' => 'utf-8',
        );
    

    第 2 步:添加电子邮件模板 (app\View\Emails\html\sample.ctp)

    <body>
    <h1>Email Testing: <?php echo $first_name?></h1>
    </body>
    

    第 3 步:更改方法中的代码,如下所示:

    public function send_my_email() {
            App::uses('CakeEmail', 'Network/Email');
            $Email = new CakeEmail();
            $Email->config('gmail'); //configuration
            $Email->emailFormat('html'); //email format
            $Email->to('receiveremail@ex.com');
            $Email->subject('Testing the emails');
            $Email->template('sample');//created in above step
            $Email->viewVars(array('first_name'=>'John Doe' ));//variable will be replaced from template
            if ($Email->send('Hi did you receive the mail')) {
                $this->Flash->success(__('Email successfully send on receiveremail@ex.com'));
            } else {
                $this->Flash->error(__('Could not send the mail. Please try again'));
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-11-12
      • 2013-04-08
      • 2013-04-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      • 2014-05-06
      相关资源
      最近更新 更多