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个回答

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');
    }
}

展开翻译

译文



在我的脑海中:你可以挂钩 Illuminate \ Mail \ Events \ MessageSending </ code>事件和 在那里添加发件人。</ p>

鉴于企业环境我会假设您知道如何听取事件(如果没有,请告诉我)。 在这种情况下,这是你需要的监听器:</ p>

  class MessageSendingListener 
{
/ **
*处理事件。
  • @ param \ Illuminate \ Mail \ Events \ MessageSending $ event
  • @return void
  • /
    公共函数句柄(MessageSending $ event)
    {
    // $ event-&gt;消息的类型为\ Swift_Message \ n //所以你需要这里的setSender()方法。

    $ event-&gt; message-&gt; setSender('appname@webX.hosts.our-company-intern.com');
    }

    }
    </ code> </ pre>
    </ div>

duanhuan3705
duanhuan3705 似乎我在扫描Mailer类代码时错过了那个事件。 正是我在找什么,谢谢!
一年多之前 回复

As written by Sajal Soni

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class DemoEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * The demo object instance.
     *
     * @var Demo
     */
    public $demo;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($demo)
    {
        $this->demo = $demo;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('sender@example.com')
                    ->view('mails.demo')
                    ->text('mails.demo_plain')
                    ->with(
                      [
                            'testVarOne' => '1',
                            'testVarTwo' => '2',
                      ])
                      ->attach(public_path('/images').'/demo.jpg', [
                              'as' => 'demo.jpg',
                              'mime' => 'image/jpeg',
                      ]);
    }
}

I hope this will help you

展开翻译

译文



Sajal Soni </ p>

 &lt;?php 

namespace App \ Mail;

use Illuminate \ Bus \ Queueable;
use Illuminate \ Mail \ Mailable;
use Illuminate \ Queue \ SerializesModels;
use Illuminate \ Contracts \ Queue \ ShouldQueue;

class DemoEmail扩展Mailable
{
使用Queueable,SerializesModels ;

/ **
*演示对象实例。

  • @var演示
  • /
    public $ demo;

    / **
    *创建新的消息实例 。

  • @return void

  • /
    公共函数__construct($ demo)
    {
    $ this-&gt; demo = $ demo;
    }

    / **
    *构建消息。

  • @return $ this

  • /
    公共函数build()
    {
    返回$ this-&gt; from('sender@example.com')

    • &gt; view('mails.demo')
    • &gt; text('mails.demo_plain')
    • &gt; with( [ 'testVarOne'=&gt; '1', 'testVarTwo'=&gt; '2', ])
    • &gt; attach(public_path('/ images')。'/ demo.jpg',[ 'as'=&gt;'demo.jpg', 'mime' =&gt;'image / jpeg', ]); } } </ code> </ pre>

    我希望这会对您有所帮助</ p>
    < / DIV>

douxie2023
douxie2023 我不认为这个答案适用于这个问题。 你打电话导致同样的问题,我没有看到设置“信封发件人”的代码。 链接教程只是初学者解释如何发送电子邮件。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问