John Davidson

php - Laravel notifications - Mailgun - "On Behalf of" in Outlook

0 comments
Message:


The sender in mails recieved in MS. Outlook, sent thru mailgun api looks like this:


[email protected] on behalf of [email protected]


Using ->from (as suggested in the documentation) does not solve it:


return (new MailMessage)->from('[email protected]', 'Domain.com')

When sending a traditional Mail::send() I solve it by...


...adding ->sender('[email protected]', 'Mr Info') to the class:


 Mail::send(['html' => 'emails.my-email'], ['data' => $this->details], function ($message) use ($details) {
$message->to($details['email']);
$message->from(config('mail.from.address'), config('mail.from.name'));
$message->sender(config('mail.from.address'), config('mail.from.name'));
});

However I can't figure out how I add the ->sender() class to notifications...?


Laravel 8



My Answer:
To send notifications using Laravel with Mailgun, you first need to set up Mailgun as your mail driver in your Laravel application. Here's how you can do it:

1. Install the Mailgun driver package using Composer:

composer require guzzlehttp/guzzle
composer require mailgun/mailgun-php


2. Update your `.env` file with your Mailgun API credentials:

MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=your-mailgun-username
MAIL_PASSWORD=your-mailgun-password
MAIL_ENCRYPTION=tls
MAILGUN_DOMAIN=your-mailgun-domain
MAILGUN_SECRET=your-mailgun-secret


3. Create a notification class in your Laravel application. For example, you can create a new notification using the `php artisan make:notification` command:

php artisan make:notification OrderShipped


4. In your notification class, you can define the notification content and how it should be sent. For example:
php
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class OrderShipped extends Notification
{
use Queueable;

public function via($notifiable)
{
return ['mail'];
}

public function toMail($notifiable)
{
return (new MailMessage)
->line('Your order has been shipped!')
->action('View Order', url('/orders/'.$this->order->id))
->line('Thank you for shopping with us!');
}
}


5. To send the notification, you can use the `notify()` method on your notifiable model. For example, if you want to send the notification to a user:
php
$user->notify(new OrderShipped($order));


That's it! Your Laravel application should now be able to send notifications using Mailgun. Make sure to test your notifications to ensure they are being sent correctly.

Rate this post

3 of 5 based on 9026 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us