尝试在laravel中配置邮件,获得异常。为什么?

时间:2015-12-05 15:05:15

标签: email laravel contact-form

我已经配置了config / mail.php,控制器等。仍然没有工作并抛出此错误:

[2015-12-05 14:47:57] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'Swift_Mailer' not found' in /vdir/www.adfusion.ch/var/www/vhosts/www.adfusion.ch/web/laravel/vendor/laravel/framework/src/Illuminate/Mail/MailServiceProvider.php:95
Stack trace:
#0 {main}  

我的联络管理员:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    public function index()
    {
        return view('contact');
    }

    public function postSubmit(Request $request)
    {
        Mail::send('emails.contact', ['data' => $request->all()], function ($m) {
            $m->from(config('mail.from.address'), config('mail.from.name'));
            $m->to('info@xxxxx', 'xxxx')->subject('Contact Form Submitted');
        });
    }
}

路线:

Route::get('/contact', 'ContactController@index');
Route::post('/contact/submit', 'ContactController@postSubmit');

我有电子邮件/联系人视图。有什么我错过的吗?!

编辑:

邮件配置文件:

返回[

/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "ses", "log"
|
*/

'driver' => 'smtp',

/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/

'host' => 'smtp.XXXX',

/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/

'port' => 25,

/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/

'from' => ['address' => 'no-reply@XXX', 'name' => 'XX'],

/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/

'encryption' => null,

/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/

'username' => 'no-reply@XXXX',

/*
|--------------------------------------------------------------------------
| SMTP Server Password
|--------------------------------------------------------------------------
|
| Here you may set the password required by your SMTP server to send out
| messages from your application. This will be given to the server on
| connection so that the application will be able to send messages.
|
*/

'password' => 'XXXXX',

/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/

'sendmail' => '/usr/sbin/sendmail -bs',

/*
|--------------------------------------------------------------------------
| Mail "Pretend"
|--------------------------------------------------------------------------
|
| When this option is enabled, e-mail will not actually be sent over the
| web and will instead be written to your application's logs files so
| you may inspect the message. This is great for local development.
|
*/

'pretend' => false,

];

使用XXX

标记用户名和域名

为什么它使用swift?我正在尝试使用托管邮件服务器 在控制器中尝试了dd,它什么也没做!为什么没有联系控制器?

联络视图:

<form role="form" id="feedbackForm" data-toggle="validator" data-disable="false" method="POST" action="{{ url('contact/submit') }}">

另外,在本地服务器上,如果我设置假装为true,我会记录以下内容:

[2015-12-05 16:49:50] local.INFO:假装邮件到:info @ xxxx

这是服务器日志:

[Sat Dec 05 17:56:29 2015] [warn] [client 188.24.47.222] mod_fcgid:stderr:PHP致命错误:在/vdir/www.xxx.ch/var/www中找不到类'Swift_Transport_EsmtpTransport'第24行的/vhosts/www.xxx.ch/web/laravel/vendor/swiftmailer/swiftmailer/lib/classes/Swift/SmtpTransport.php,引用者:http://xxx.ch/contact

2 个答案:

答案 0 :(得分:1)

您是否在环境变量(.env)文件中更改了邮件驱动程序,主机等? 这里:

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

必须将这些设置为与邮件配置文件中的用户名和密码等相同

答案 1 :(得分:0)

是的,当然。这是我用来为laravel配置电子邮件的解决方案。

indexmail.php,根文件夹

 <?php
 if(isset($_POST['submit']))
 {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = $_POST['message'];
    $email_from = $name.'<'.$email.'>';

 $to="your-email@your-domain.com";
 $subject="Enquiry!";
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= "From: ".$email_from."\r\n";
 $message="   

         Name:
         $name     
         <br>
         Email-Id:
         $email        
         <br>
         Message:
         $query        

   ";
    if(mail($to,$subject,$message,$headers))
        header("Location:../contact.php?msg=Successful Submission! Thankyou for contacting us.");
    else
        header("Location:../contact.php?msg=Error To send Email !");
        //contact:-your-email@your-domain.com
 }
?>

routes.php

Route::post('/contact/submit', 'ContactController@postSubmit');

配置/ mail.php

'from' => ['address' => 'Sender@email.com', 'name' => 'MyName'],




'pretend' => false,

电子邮件视图。

<strong>You have a new feedback from contact page!</strong>
<p><strong>Name: </strong> {{$data['name']}}</p>
<p><strong>Email: </strong> {{$data['email']}}</p>
<p><strong>Message: </strong> {{$data['message']}}</p>

联系表格。

 <form role="form" id="feedbackForm" data-toggle="validator" data-disable="false" method="POST"
                                 action="{{ url('contact/submit') }}">
                               <input type="hidden" name="_token" value="{{ csrf_token() }}"/>

应用程序/ HTTP /请求/ ContactFormRequest.php

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class ContactFormRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required|email',
            'message' => 'required',
        ];
    }
}

应用程序/ user.php的

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

让我知道它是否有效!