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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵