无法发送电子邮件:cakephp3中的未知Cake \ Network \ Exception \ SocketException

时间:2016-11-14 12:51:45

标签: cakephp-3.0

好的,我去过几乎所有的线程,但我找不到我想要的答案。  控制器:RegistersController

<?php
class Connection
{
private static $conn;
private $dbHost;
private $dbUser;
private $dbPass;
private $dbName;
public  $dbHandle;
public $languages;

public static function getConnection()
{
        if($conn == null)
                self::$conn = new Connection();
        return self::$conn;
}

protected function __construct()
{
        $this->dbhost = BV_DB_HOST;
        $this->dbuser = BV_DB_USER;
        $this->dbpass = BV_DB_PASS;
        $this->dbname = BV_DB_NAME;
        $this->dbConnect();
}

public function dbConnect()
{
        $this->dbHandle = mysqli_connect(BV_DB_HOST, BV_DB_USER, BV_DB_PASS);
        $tmp = mysqli_select_db($this->dbHandle, BV_DB_NAME);
        mysqli_query($this->dbHandle, "set names utf8");
        return $tmp;
}

public function languages()
{
        return explode(";", BV_LANGS);
}
}
?>

Inside Template-&gt; Email-&gt; html我有一个名为的文件  email.ctp

namespace App\Controller;

use App\Controller\AppController;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Event\Event;
use Cake\Mailer\Email;
use Cake\Routing\Router;

class RegistersController extends AppController
{
  public function add()
    {
        $this->loadModel('Tbusers');
        $tbusers = $this->Tbusers->newEntity();
        if ($this->request->is('post')) 
        {
            $tbusers = $this->Tbusers->patchEntity($tbusers, $this->request->data);
            if ($this->Tbusers->save($tbusers)) {
                $this->Flash->success(__('Yayie! You have been registered.'));
                $baseurl = Router::url('/', true);
                $email = base64_encode($this->request->data['email']);
                $first_name = $this->request->data['fname'];
                $last_name = $this->request->data['lname'];
                $username = $first_name.' '.$last_name;
                //$confirmation_link = $baseurl.'users/confirm/'.$email;
                $email = new Email();
                $email->template('email')
                        ->subject('Student Portal - Signup')
                        ->emailFormat('html')
                        ->to($this->request->data['email'])
                        //->viewVars(['confirmation_link' => $confirmation_link,'username'=>$username])
                        ->viewVars(['username'=>$username])
                        ->from('dotnetdev555@gmail.com')
                        ->send();
                if ($email->send()) 
                {
                    // Success
                    echo "mail sent";
                } 
                else 
                {
                    echo "Mail not sent";
                    // Failure
                }
                return $this->redirect(['action' => 'add']);
            }
}

app.php

Hello <?php echo $username; ?>,<br/>


Thanks,<br/>
Jaymin Sejpal Founder at Student Portal

我不知道我错过了什么。我正在考虑这个 http://book.cakephp.org/3.0/en/core-libraries/email.html暂时。

1 个答案:

答案 0 :(得分:1)

您正在使用的电子邮件主机可能正在使用smtp协议,因此在您的app.php中。尝试将className参数编辑为&#39; Smtp&#39;

'EmailTransport' => [
    'default' => [
        'className' => 'Smtp',
        // The following keys are used in SMTP transports
        'host'      =>  'smtp.elasticemail.com',
        'port'      =>  2525,
        'timeout'   =>  30,
        'username'  => 'dotnetdev555@gmail.com',
        'password'  => 'secret',
        'client'    => null,
        'tls'       => null,
        'url'       => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
   ],

],