简体   繁体   中英

Laravel 5.2 - Send Email Through SparkPost Using Dedicated IP

I'm working on a project written in Laravel 5.2 that requires me to send email through a dedicated IP address so that we can whitelist the IP address of the sender (in this case, SparkPost) to allow deliverability within a corporate environment with very strict anti-spam rules.

I have setup my SparkPost account with a dedicated IP address and I can send emails, but is sending email via the default IP pool instead of using my dedicated IP address. As a result, the emails are marked as spam.

Here is a reference from SparkPost about how to specify a specific IP pool: https://support.sparkpost.com/customer/portal/articles/2002977

My question is this: How can I specify to use the dedicated IP when using Laravel's Mail::send method?

in the document it specifies that you have to add an additional header so it can route it through the specific ip

X-MSYS-API: { "options" : { "ip_pool" : "my_new_ip_pool" } }

otherwise the default is used.

Mail::send doesn't offer any shortcuts for adding additional headers so you have to use the underlying Swift Message. Not quite sure about the value of the header.. Try testing with mail driver set to log .

    Mail::send("template.blade",$data,function($message){
$message->getHeaders()->addTextHeader('X-MSYS-API', '{ "options" : { "ip_pool" : "my_new_ip_pool" } }')
});

Mail::Send Laravel 5.2 SwiftMailer Headers documentation

Mr. Phoenix's response might work if you're using the SMTP protocol to communicate with SparkPost, but my application is using SparkPost's API.

After a little bit of help from Mr.Phoenix (thank you, btw), I was able to trace how the message was being sent and found that that the createSparkPostDriver method in the TransportManager has a way to configure options based on what you indicate in your services.php file in the config folder.

In the end, all I had to do was add the following to my services.php file:

'sparkpost' => [
    'secret' => env('SPARKPOST_SECRET'),
    'options' => [
        "ip_pool" => env('SPARKPOST_IP_POOL_ID'),
    ]
],

And then add the variables to my .env file:

SPARKPOST_SECRET=abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd
SPARKPOST_IP_POOL_ID=dedicated_ip_pool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM