dongzhong5967 2015-12-08 08:17
浏览 57
已采纳

无法在laravel 5中向电子邮件发件人发送电子邮件

My code is like this :

 public function sendMail(array $data)
    { 
        $data = explode('#', $data['id']);
        $email_from = Auth::user()->email;
        $email_to = $data[4];
        $subject = 'Send Email Test';

        $data_user = ['user_name' => $data[1], 'full_name' => $data[2].' '.$data[3] ];

        $sent = Mail::send('backend.auth.success_approved', $data_user, function ($mail) use ($email_to, $email_from, $subject)
                {
                    $mail->from($email_from)
                         ->to($email_to)
                         ->subject($subject);
                });
    }  

My configuration in mail.php :

return [
    'driver' => env('MAIL_DRIVER', 'smtp'),
    'host' => env('MAIL_HOST', 'smtp.gmail.com'),
    'port' => env('MAIL_PORT', 587),
    'from' => ['address' => 'myemail@gmail.com', 'name' => 'myname'],
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username' => env('MAIL_USERNAME', 'myemail@gmail.com'),
    'password' => env('MAIL_PASSWORD', 'mypassword'),
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,
];

It has successfully to send email. But it did not dynamic sender. It is the email sender of mail.php

How to keep its dynamic sender?

Thank you

  • 写回答

1条回答 默认 最新

  • doufei4418 2015-12-08 10:58
    关注

    This is impossible to send mail from dynamic sender if you are using Gmail server. For security purpose Gmail is converting your sender address to your default address. To prevent spamming many email server (like Gmail, Hotmail etc) don't let you do this. As an example you can send email from Barak Obama's email address.

    If it is very important to send email from dynamic address for you, you can use raw php function mail()

    $header = 'From: [FEEDBACK] <'.$email_from.'>'."
    ".'Reply-To:'.$email_from."
    ".'X-Mailer: PHP/' . phpversion();
    $sent = mail('myemail@gmail.com',$subject,$body,$header);
    

    This code will not work in localhost, unless you configure your xampp or wampp or mampp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器