Php larvel-调用未定义的方法Swift\u SmtpTransport::newInstance()
我在用Swiftmailer发送电子邮件。我安装了 +“guzzle http/guzzle”:“^6.3” +“swiftmailer/swiftmailer”:“^6.0” 我为它创建控制器和路由。但它有一个问题“调用未定义的方法Swift_SmtpTransport::newInstance()。请检查我的代码并指导我如何修复它?非常感谢Php larvel-调用未定义的方法Swift\u SmtpTransport::newInstance(),php,laravel,laravel-5,Php,Laravel,Laravel 5,我在用Swiftmailer发送电子邮件。我安装了 +“guzzle http/guzzle”:“^6.3” +“swiftmailer/swiftmailer”:“^6.0” 我为它创建控制器和路由。但它有一个问题“调用未定义的方法Swift_SmtpTransport::newInstance()。请检查我的代码并指导我如何修复它?非常感谢 namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Swift_Mailer;
use Swift_MailTransport;
use Swift_Message;
class EmailController extends Controller
{
public function sendEmail(){
// Configuration
$smtpAddress = 'smtp.zoho.com';
$port = 465;
$encryption = 'ssl';
$yourEmail = 'ptc@gmail.com';
$yourPassword = '********';
// Prepare transport
$transport = \Swift_SmtpTransport::newInstance($smtpAddress, $port, $encryption)
->setUsername($yourEmail)
->setPassword($yourPassword);
$mailer = \Swift_Mailer::newInstance($transport);
// Prepare content
$view = View::make('email_template', [
'message' => '<h1>Hello World !</h1>'
]);
$html = $view->render();
// Send email
$message = \Swift_Message::newInstance('Test')
->setFrom(['ptc@gmail.com' => 'Our Code World'])
->setTo(["hik@gmail.com" => "mail@mail.com"])
// If you want plain text instead, remove the second paramter of setBody
->setBody($html, 'text/html');
if($mailer->send($message)){
echo "Check your inbox";
}
echo "Something went wrong :(";
}
}
namespace-App\Http\Controllers;
使用\Http\Request;
使用Lightning\Support\Facades\Mail;
使用Swift_-Mailer;
使用快速邮递;
使用Swift_信息;
类EmailController扩展控制器
{
公共函数sendmail(){
//配置
$smtpAddress='smtp.zoho.com';
$port=465;
$encryption='ssl';
$yourEmail=ptc@gmail.com';
$yourPassword='********';
//准备运输
$transport=\Swift\u SmtpTransport::newInstance($smtpAddress、$port、$encryption)
->setUsername($yourEmail)
->设置密码($yourPassword);
$mailer=\Swift\u mailer::newInstance($transport);
//准备内容
$view=view::make('email_template'[
“消息”=>“你好,世界!”
]);
$html=$view->render();
//发送电子邮件
$message=\Swift\u message::newInstance('Test')
->setFrom(['ptc@gmail.com“=>“我们的代码世界”])
->设置为([”hik@gmail.com" => "mail@mail.com"])
//如果需要纯文本,请删除setBody的第二个参数
->setBody($html,'text/html');
如果($mailer->send($message)){
回显“检查您的收件箱”;
}
echo“出了问题:(”;
}
}
在GitHub问题中找到,用于SwiftMailer“^6.0”
::newInstance()方法已被弃用,以及
快速邮递
尝试更改:
\Swift_SmtpTransport::newInstance()
\Swift_Mailer::newInstance()
致:
更多关于:
- GitHub
- ^6版本的文档,请阅读
- 不要把事情过分复杂化
你正在重写与拉威尔捆绑在一起的东西
无需自行设置swiftmailer,只需使用laravel类并设置smtp凭据即可
config/mail.php
它可以很容易
Mail::raw('Text to e-mail', function($message)
{
$message->from('us@example.com', 'Laravel');
$message->to('foo@example.com')->cc('bar@example.com');
});
或者,您也可以选择使用视图制作模板
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us@example.com', 'Laravel');
$message->to('foo@example.com')->cc('bar@example.com');
$message->attach($pathToFile);
});
嗨,我读过并知道Laravel有它。但我想自己控制。Exmap,mailgun免费发送10000封电子邮件,但我不知道它什么时候消失了。所以我想控制它。你不需要使用mailgun,你可以使用smtp。你甚至可以使用你的个人gmail发送邮件或这个zoho帐户。是的,有一种方法可以知道你什么时候离开了在mailgun上发送电子邮件时,它会抛出一个异常。然后你可以使用slack、bugsnag或其他工具来报告你没有电子邮件了
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us@example.com', 'Laravel');
$message->to('foo@example.com')->cc('bar@example.com');
$message->attach($pathToFile);
});