在 Laravel 4 中使用 SwiftMailer 发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18216024/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:16:33  来源:igfitidea点击:

Sending mail with SwiftMailer in Laravel 4

laravellaravel-4swiftmailer

提问by uleMzy

I am receiving a the following error:

我收到以下错误:

Cannot send message without a recipient

没有收件人就无法发送消息

This is while trying to send a email using swiftmailer. My code works in localhost and has all the parameters needed from sender to receiver but it throws an error saying it cannot send without recipient.

这是在尝试使用 swiftmailer 发送电子邮件时。我的代码在 localhost 中工作,并且具有从发送者到接收者所需的所有参数,但它抛出一个错误,说它不能在没有接收者的情况下发送。

Here is my code:

这是我的代码:

public function email()
{


    Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message){
        $message = Swift_Message::newInstance();
        $email = $_POST['email']; $name = $_POST['name']; $subject = $_POST['subject']; $msg = $_POST['msg']; 

        $message = Swift_Message::newInstance()
            ->setFrom(array($email => $name))
            ->setTo(array('[email protected]' => 'Name'))           
            ->setSubject($subject)
            ->setBody($msg);




        $transport = Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl');
        //$transport->setLocalDomain('[127.0.0.1]');

        $mailer = Swift_Mailer::newInstance($transport);
        //Send the message
        $result = $mailer->send($message);
        if($result){

             var_dump('worked');
        }else{
            var_dump('Did not send mail');
        }
  }

}

}

回答by fideloper

You can do this without adding your the SMTP information in your Mail::send()implementation.

您无需在Mail::send()实现中添加 SMTP 信息即可执行此操作 。

Assuming you have not already, head over to app/config/mail.phpand edit the following to suit your needs:

假设您还没有,请前往app/config/mail.php并编辑以下内容以满足您的需要:

'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your_username',
'password' => 'your_password',

Then your code shouldbe as simple as:

那么你的代码应该像这样简单:

public function email()
{
    Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message)
    {
        $message->from( Input::get('email'), Input::get('name') );

        $message->to('[email protected]', 'Name')->subject( Input::get('subject') );
    });
}

So, hopefully the issue is one of configuration. Do you have other environments setup that might be over-riding the app/config/mail.phpconfiguration settings in your server where it's not working?

所以,希望问题是配置之一。您是否有其他环境设置可能会覆盖app/config/mail.php服务器中的配置设置而无法正常工作?

回答by Abishek

Please make sure you have configured all the necessary configuration correctly on /app/config/mail.php. Ensure all the configuration is correct for the environment where the email is not working correctly.

请确保您已在 上正确配置了所有必要的配置 /app/config/mail.php。确保所有配置对于电子邮件无法正常工作的环境都是正确的。

回答by Mwirabua Tim

If you want to use your Gmail account as an SMTP server, set the following in app/config/mail.php:

如果要将 Gmail 帐户用作 SMTP 服务器,请在 app/config/mail.php 中设置以下内容:

'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => '[email protected]',
'password' => 'your-password',

When switching to online server, you want to protect this file or switch provider so as not to expose your gmail credentials. Port 587 is for mailgun not gmail.

切换到在线服务器时,您希望保护此文件或切换提供商,以免暴露您的 Gmail 凭据。端口 587 用于 mailgun 而不是 gmail。