【问题标题】:Laravel - Call to undefined method Swift_SmtpTransport::newInstance()Laravel - 调用未定义的方法 Swift_SmtpTransport::newInstance()
【发布时间】: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 :(";
    }
}

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    在 GitHub 问题中找到,用于 SwiftMailer "^6.0"

    ::newInstance() 方法已被弃用,以及 Swift_MailTransport。

    尝试改变:

    \Swift_SmtpTransport::newInstance() 
    \Swift_Mailer::newInstance()
    

    到:

    new \Swift_SmtpTransport();
    new \Swift_Mailer()
    

    更多信息:

    【讨论】:

      【解决方案2】:

      不要把事情复杂化。

      你正在重写与 laravel 捆绑的东西。

      https://laravel.com/docs/5.5/mail

      无需自己设置 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 或其他工具报告您的电子邮件已用完
      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 2015-05-31
      • 2016-06-05
      • 2016-09-03
      • 2015-06-10
      相关资源
      最近更新 更多