Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.