【问题标题】:Laravel 5 and PHPMailerLaravel 5 和 PHPMailer
【发布时间】:2015-06-11 18:01:10
【问题描述】:

有没有人有一个工作示例我如何在 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 中不起作用。任何想法?谢谢!

【问题讨论】:

  • 定义“不起作用”。有什么具体的错误、异常之类的吗?
  • 为什么要首先使用 PHPMailer? Laravel 有很好的 SwiftMailer 内置实现。
  • 是的,我会使用它,但它是一个遗留代码,我必须在第一阶段从 4.2 升级到 5。之后我会将其转换为 SwiftMailer。

标签: php phpmailer laravel-5


【解决方案1】:

嗯,我认为有多个错误...... 这是一个在 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');

当然,你需要在 composer.json 添加依赖后做一个 composer update

但是,我更喜欢 SwiftMailer 中内置的 laravel。 http://laravel.com/docs/5.0/mail

【讨论】:

  • 如我所见,我的示例完全相同,只是 *@+%! root namespace 反斜杠字符... :) 我的眼睛刚刚跳过了这个,我不知道为什么... :( 所以谢谢,它现在可以工作了。是的,我会将代码更改为 Laravel 的内置邮件程序,但它是旧代码,首先我必须从 4.2 升级到 5,然后我会做一些改进。再次感谢!
  • 当我说多个错误时,我以为您使用的是 $mail->Host = "yourmailhost"; 之类的东西不是作为分配,而是作为功能 $mail->Host("yourmailhost");但是我明白了,您只是想指出您通常所做的事情,而我不应该将其视为正确的语法。无论如何,很高兴为您提供帮助;)
  • 谁能回答我如何在相同的设置中使用 NONSSL ?不使用 ssl laravel 会出错,而且我没有有效的 SSL 凭据
  • SSL 凭证?您的意思是 SMTP 凭据,对吗?
【解决方案2】:

在 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';
        }
    }
}

【讨论】:

  • 你知道邮件发送后如何返回重定向吗?
  • 而不是返回只使用header("Location: http://www.example.com/");
猜你喜欢
  • 1970-01-01
  • 2015-06-09
  • 2015-07-17
  • 2023-03-22
  • 1970-01-01
  • 2015-10-09
  • 2016-08-15
  • 2016-09-29
  • 2015-09-05
相关资源
最近更新 更多