Ask Your Question
4

How to add attachments to Mailgun using Laravel?

asked 2021-06-19 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-02-01 18:00:00 +0000

woof gravatar image

To add attachments to Mailgun using Laravel, follow these steps:

  1. Install the Mailgun API client for Laravel using Composer:
composer require mailgun/mailgun-php:^3.0
  1. Set up your Mailgun credentials in the config/services.php file:
'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
],
  1. Add the MAIL_MAILER and MAIL_FROM_ADDRESS variables to your .env file:
MAIL_MAILER=mailgun
MAIL_FROM_ADDRESS=me@example.com
MAILGUN_DOMAIN=your-domain-name
MAILGUN_SECRET=your-secret-key
  1. Use the Mailgun API client to send an email with attachments in Laravel. Here is an example:
use Mailgun\Mailgun;

$mgClient = new Mailgun(config('services.mailgun.secret'));
$domain = config('services.mailgun.domain');

$mgClient->messages()->send($domain, [
    'from' => 'sender@example.com',
    'to' => 'recipient@example.com',
    'subject' => 'Your Subject',
    'html' => '<h1>Mailgun HTML email with attachments</h1>',
    'attachment' => [
        [
            'filePath' => '/path/to/attachment1.txt',
            'filename' => 'attachment1.txt'
        ],
        [
            'filePath' => '/path/to/attachment2.pdf',
            'filename' => 'attachment2.pdf'
        ]
    ]
]);

In this example, we used the Mailgun API client to send an email with two attachments: attachment1.txt and attachment2.pdf. The attachments are specified using their file paths and filenames.

That's it! You can now send emails with attachments using Mailgun in Laravel.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-19 11:00:00 +0000

Seen: 9 times

Last updated: Feb 01 '23