Laravel 5 and PHPMailer


Laravel 5 and PHPMailer

有人有我如何在Laravel 5中使用PHPMailer的工作示例吗?在Laravel 4中,它使用起来很简单,但同样的方法在L5中不起作用。这是我在L4:中所做的

添加在composer.json:中

"phpmailer/phpmailer": "dev-master",

controller中,我这样使用它:

$mail = new PHPMailer(true);
try {
  $mail->SMTPAuth(...);
  $mail->SMTPSecure(...);
  $mail->Host(...);
  $mail->port(...);
  .
  .
  .
  $mail->MsgHTML($body);
  $mail->Send();
} catch (phpmailerException $e) {
  .
  .
} catch (Exception $e) {
  .
  .
}

但它在L5中不起作用。知道吗?谢谢

我认为有多个错误。。。这是一个使用Laravel 5中的PhpMailer发送邮件的工作示例。刚刚测试过。

        $mail = new 'PHPMailer(true); // notice the '  you have to use root namespace here
    try {
        $mail->isSMTP(); // tell to use smtp
        $mail->CharSet = "utf-8"; // set charset to utf8
        $mail->SMTPAuth = true;  // use smpt auth
        $mail->SMTPSecure = "tls"; // or ssl
        $mail->Host = "yourmailhost";
        $mail->Port = 2525; // most likely something different for you. This is the mailtrap.io port i use for testing. 
        $mail->Username = "username";
        $mail->Password = "password";
        $mail->setFrom("youremail@yourdomain.de", "Firstname Lastname");
        $mail->Subject = "Test";
        $mail->MsgHTML("This is a test");
        $mail->addAddress("recipient@anotherdomain.de", "Recipient Name");
        $mail->send();
    } catch (phpmailerException $e) {
        dd($e);
    } catch (Exception $e) {
        dd($e);
    }
    die('success');

当然,在将depency添加到composer.json 之后,您需要进行composer更新

然而,我更喜欢SwiftMailer中的laravel。http://laravel.com/docs/5.0/mail

在Laravel 5.5或更高版本中,您需要执行以下步骤

在您的laravel应用程序上安装PHPMailer。

composer require phpmailer/phpmailer

然后转到要使用phpmailer的控制器。

<?php
namespace App'Http'Controllers;
use PHPMailer'PHPMailer;
class testPHPMailer extends Controller
{
    public function index()
    {
        $text             = 'Hello Mail';
        $mail             = new PHPMailer'PHPMailer(); // create a n
        $mail->SMTPDebug  = 1; // debugging: 1 = errors and messages, 2 = messages only
        $mail->SMTPAuth   = true; // authentication enabled
        $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
        $mail->Host       = "smtp.gmail.com";
        $mail->Port       = 465; // or 587
        $mail->IsHTML(true);
        $mail->Username = "testmail@gmail.com";
        $mail->Password = "testpass";
        $mail->SetFrom("testmail@gmail.com", 'Sender Name');
        $mail->Subject = "Test Subject";
        $mail->Body    = $text;
        $mail->AddAddress("testreciver@gmail.com", "Receiver Name");
        if ($mail->Send()) {
            return 'Email Sended Successfully';
        } else {
            return 'Failed to Send Email';
        }
    }
}