【问题标题】:Sending mail with SwiftMailer in Laravel 4在 Laravel 4 中使用 SwiftMailer 发送邮件
【发布时间】:2013-08-13 17:59:32
【问题描述】:

我收到以下错误:

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

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

这是我的代码:

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('name@gmail.com' => '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');
        }
  }

}

【问题讨论】:

    标签: laravel laravel-4 swiftmailer


    【解决方案1】:

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

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

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

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

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

    所以,希望问题是配置之一。您是否有其他环境设置可能会覆盖服务器中的 app/config/mail.php 配置设置,但它不起作用?

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 'mail', 'host' => 'smtp.gmail.com', 'port' => 587, 'from' => array( 'address' => 'test@example.org', 'name' => 'Testing'), 'encryption' => 'tls', 'username' => null, 'password' => null, );跨度>
      • 您可能是正确的,但我想指出,我不知道这是否会起作用,因为 OP 在那里完全规避了很多邮件库配置/加载.. .
      【解决方案3】:

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

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

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

      【讨论】:

      • 端口 587 用于 Gmail 上的 TLS 加密
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 2020-08-04
      • 2013-08-10
      • 1970-01-01
      • 2019-04-09
      相关资源
      最近更新 更多