【发布时间】:2017-11-05 07:42:06
【问题描述】:
我正在使用 Swiftmailer 发送电子邮件。我已经安装 + "guzzlehttp/guzzle": "^6.3" + "swiftmailer/swiftmailer": "^6.0".
我为它创建控制器和路由。但它有问题“调用未定义的方法 Swift_SmtpTransport::newInstance()”。请检查我的代码并指导我如何修复它?非常感谢
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 :(";
}
}
【问题讨论】: