簡體   English   中英

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

[英]Laravel Mail / Swift / How to globally configure "sender" address

Laravel 提供配置mail.from以允許指定全局/默認From地址。 這會在內部調用 Swift-Message 上的setFrom並設置收件人電子郵件客戶端中顯示的“ Header From ”。 但是,由於沒有設置其他選項 ( Sender/Return-Path ),由於getReversePath派生了此From值,因此消息隨后也與此值的 Return-Path/Envelope-From一起發送。

該站點在本地運行 Exim4 的多項目主機上運行,因此設置這些地址沒有限制,就像我使用 Gmail SMTP 一樣。 Laravel 配置為使用sendmail

假設機器主機名是webX.hosts.our-company-intern.com並且在其上運行的項目屬於customer-brand.com的子域。 不過,電子郵件應該從“主域”(沒有子域部分)發送,例如noreply@customer-brand.com customer-brand.com. 不會解析到托管子域項目的機器。

我想發送帶有我實際主機名的信封地址的郵件(更好:保留 Exim/Sendmail 將使用的默認信封發件人/返回路徑),例如appname@webX.hosts.our-company-intern.com和只有From: noreply@customer-brand.com

這樣做的原因是,我想要一個有效的Return-Path來指示它實際上來自哪個主機。 SPF也是一個原因,但不是主要的; 我們控制着customer-brand.com域,可以只添加我們的主機地址,如果可能的話,我仍然想避免它,並使用我們已經在其 SPF 記錄中包含所有主機的域。

當我將以下行放在Laravel 供應商類方法Mailer::send中時:

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

這會產生我想要的結果,但當然我不能只在供應商代碼中編輯它。 我在哪里可以正確配置它(可能通過為每條消息執行的回調)? 還是沒有這樣的選項,我應該在 Laravel/Mail 包中寫一個問題?

我還嘗試將-f放在 sendmail 命令中: /usr/sbin/sendmail -bs -f"appname@webX.hosts.our-company-intern.com" - 但是這已經在_establishProcessConnection()失敗了。 在 CLI 中調用錯誤是:

exim:不兼容的命令行選項或參數


版本:

  • 拉維爾 5.4.36
  • Swiftmailer 5.4.9
  • 進出口 4.89-2+deb9u2

配置mail.php

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

Tinker-Shell測試代碼:

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

當前郵件標題:

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

在我的頭上:您可以掛接Illuminate\\Mail\\Events\\MessageSending事件並在其中添加發件人。

考慮到公司環境,我假設您知道如何收聽事件 (如果不是,請告訴我)。 在這種情況下,您需要使用以下監聽器:

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

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

我希望這能幫到您

在 .env 文件中提及 MAIL_FROM_ADDRESS=dev@example.com

然后運行:

php artisan 配置:緩存

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=xx-xxxxxxxx-xxxx
MAIL_PASSWORD=xx-xxxxxxxx-xxxx
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=dev@example.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM