douqiaotong8682 2018-10-20 11:33
浏览 231
已采纳

Laravel Mail / Swift /如何全局配置“发件人”地址

Laravel offers the config mail.from to allow specifying a global/default From address. This calls setFrom on the Swift-Message internally and sets the "Header From" that shows in the recipients email client. However, the message is then also sent with Return-Path/Envelope-From of this value, because of getReversePath, that derives this From value as no other option (Sender/Return-Path) has been set.

The site is running on a multi-project host with Exim4 running locally, so there are no restrictions to set these addresses like if I was using something like Gmail SMTP. Laravel is configured to use sendmail.

Let's assume the machines hostname is webX.hosts.our-company-intern.com and the project that runs on it belongs to a subdomain of customer-brand.com. E-Mails should be sent from the "main-domain" (without the subdomain part), though, like noreply@customer-brand.com. customer-brand.com. does not resolve to the machine the subdomain project is hosted.

I would like to send the mail with an Envelope address of my actual hostname (better: preserve the default Envelope-From/Return-Path Exim/Sendmail would use), like appname@webX.hosts.our-company-intern.com and only have From: noreply@customer-brand.com.

Reason for that is, I'd like to have a valid Return-Path indicating from which host it actually came from. SPF is also a reason, but not the main one; we control the customer-brand.com domain and could just add our hosts address, I'd still like to avoid it if possible and use our domain that already have all our hosts in their SPF record.

When I put the following line in the Laravel vendor class-method Mailer::send:

$message->sender('appname@webX.hosts.our-company-intern.com');

This yields my desired result, but of course I cannot just edit it in vendor code. Where can I configure this properly (maybe via a callback that executes for every message)? Or isn't there any such option and I should write an issue in the Laravel/Mail package?

I also tried putting -f in the sendmail command: /usr/sbin/sendmail -bs -f"appname@webX.hosts.our-company-intern.com" - however this already fails at the _establishProcessConnection(). Called in CLI the error is:

exim: incompatible command-line options or arguments


Versions:

  • Laravel 5.4.36
  • Swiftmailer 5.4.9
  • Exim 4.89-2+deb9u2

Config mail.php:

'from' => [
    'address' => 'noreply@customer-brand.com',
    'name' => 'Customer Brand',
],

Tinker-Shell test code:

Mail::raw('Text to e-mail', function($message) {
  $message->to('my-personal-email-for-testing@domain.com');
})

Current mail headers:

Received: from webX.hosts.our-company-intern.com (xxx.xxx.xxx.xxx) by ...
Received: from appname (helo=[127.0.0.1])
    by webX.hosts.our-company-intern.com with local-smtp (Exim 4.89)
    (envelope-from <noreply@customer-brand.com>)  // this should change
    ...
From: Customer Brand <noreply@customer-brand.com>
Return-Path: noreply@customer-brand.com  // this should change
  • 写回答

2条回答 默认 最新

  • dousongxuan7507 2018-10-26 20:25
    关注

    On the top of my head: you could hook in the Illuminate\Mail\Events\MessageSending event and add the sender there.

    Given the corporate environment I'll assume you know how to listen to events (if not, let me know). In that case, here's the listener you'll need:

    class MessageSendingListener
    {
        /**
         * Handle the event.
         *
         * @param  \Illuminate\Mail\Events\MessageSending  $event
         * @return void
         */
        public function handle(MessageSending $event)
        {
            // $event->message is of type \Swift_Message
            // So you'll need the setSender() method here.
    
            $event->message->setSender('appname@webX.hosts.our-company-intern.com');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面