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 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来