php Laravel 5 和 PHPMailer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29486522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 and PHPMailer
提问by Sas Sam
Does anybody have a working example how I can work with PHPMailerin Laravel 5? In Laravel 4 it was quiet simple to use but the same method doesn't work in L5. Here it is what I did in L4:
有没有人有一个如何在 Laravel 5 中使用PHPMailer的工作示例?在 Laravel 4 中,它使用起来非常简单,但相同的方法在 L5 中不起作用。这是我在 L4 中所做的:
Added in composer.json
:
加入composer.json
:
"phpmailer/phpmailer": "dev-master",
And in the controller
I've used it like this:
在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) {
.
.
}
But it doesn't work in L5. Any idea? Thanks!
但它在 L5 中不起作用。任何的想法?谢谢!
采纳答案by shock_gone_wild
Well there are multiple mistakes i think... This is a working example of sending mail with PhpMailer in Laravel 5. Just tested it.
好吧,我认为有多个错误......这是在 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("[email protected]", "Firstname Lastname");
$mail->Subject = "Test";
$mail->MsgHTML("This is a test");
$mail->addAddress("[email protected]", "Recipient Name");
$mail->send();
} catch (phpmailerException $e) {
dd($e);
} catch (Exception $e) {
dd($e);
}
die('success');
And of course, you need to do a composer update after adding the depency to composer.json
当然,您需要在将依赖项添加到 composer.json 后进行 Composer 更新
However, i would prefer the laravel built in SwiftMailer. http://laravel.com/docs/5.0/mail
但是,我更喜欢 SwiftMailer 中内置的 Laravel。 http://laravel.com/docs/5.0/mail
回答by NaveenDA
In Laravel 5.5 or Above, you need to do the following steps
在 Laravel 5.5 或更高版本中,需要进行以下步骤
Install the PHPMailer on your laravel Application.
在你的 Laravel 应用程序上安装 PHPMailer。
composer require phpmailer/phpmailer
Then goto your controller where you want to use 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 = "[email protected]";
$mail->Password = "testpass";
$mail->SetFrom("[email protected]", 'Sender Name');
$mail->Subject = "Test Subject";
$mail->Body = $text;
$mail->AddAddress("[email protected]", "Receiver Name");
if ($mail->Send()) {
return 'Email Sended Successfully';
} else {
return 'Failed to Send Email';
}
}
}