Download the PHP package hgacreative/mailgun without Composer
On this page you can find all versions of the php package hgacreative/mailgun. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hgacreative/mailgun
More information about hgacreative/mailgun
Files in hgacreative/mailgun
Package mailgun
Short Description Laravel package for sending mail via the Mailgun API
License MIT
Homepage https://github.com/Bogardo/Mailgun
Informations about the package mailgun
Bogardo/Mailgun
A package for the Laravel Framework for sending emails using the Mailgun API. The syntax for sending emails is very similar to the Laravel Mail component.
Laravel already supports sending emails via the Mailgun API out of the box but it doesn't support Mailgun specific features.
This packages fills that gap and supports most of the mail features offered by Mailgun:
- Open & Click tracking
- Campaigns
- Tags
- Scheduled delivery
- Batch sending
- Custom data/headers
This package makes use of the mailgun-php library.
Basic Example
Version Compatibility
This package currently supports Laravel 5.1 and up. For older versions of Laravel please refer to older versions of this package.
Installation
Install the package via composer
If using Laravel 5.1 to 5.4, Register the ServiceProvider and (optionally) the Facade
Next, publish the config file with the following artisan
command.
or if using Laravel 5.5
After publishing, add and fill the next values to your .env
file
You can also configure the package in your config/mailgun.php
.
HTTP Client Dependency
To remove the dependency for a specific HTTP client library (e.g. Guzzle) the mailgun-php library has a dependency on the virtual package php-http/client-implementation which allows you to install any supported client adapter, it does not care which one. Please refer to the documentation for more information.
This gives you the freedom to use any (supported) client for communicating with the Mailgun API.
To register your driver you must register it in the Service Container with the mailgun.client
key.
The registration must occur before the MailgunServiceProvider
is being registered.
Guzzle 6 example implementation
Install the dependencies:
Add the following to your AppServiceProvider
register()
method.
Usage
The Mailgun package offers most of the functionality as the Laravel Mail component.
The Mailgun::send()
method may be used to send an e-mail message:
Views
The first argument passed to the send
method is the name of the view that should be used as the e-mail body.
Mailgun supports 2 types of bodies: text
and html
.
You can specify the type of body like so:
If you have an html
body as well as a text
body then you don't need to specify the type, you can just pass an array where the first item is the html
view and the second item the text
view.
When you only want to send an html
body you can just pass a string
.
When only sending a text
body, just must pass an array and specify the type.
Raw
If you do not want to use a template you can use the raw()
method.
Data
The second argument passed to the send
method is the $data
array
that is passed to the view.
Note: A
$message
variable is always passed to e-mail views, and allows the inline embedding of attachments. So, it is best to avoid passing a (custom)message
variable in your view payload.
You can access the values from the $data
array as variables using the array key.
Example:
View emails.welcome
:
Which renders:
Mail options
You can specify the mail options within the closure.
Recipients
The to
method
The cc
method
The bcc
method
Batch Sending
Mailgun supports the ability send to a group of recipients through a single API call. This is achieved by specifying multiple recipient email addresses as to parameters and using Recipient Variables.
Recipient Variables are custom variables that you define, which you can then reference in the message body. They give you the ability to send a custom message to each recipient while still using a single API Call.
To access a recipient variable within your email, simply reference %recipient.yourkey%
.
Warning: It is important when using Batch Sending to also use Recipient Variables. This tells Mailgun to send each recipient an individual email with only their email in the to field. If they are not used, all recipients’ email addresses will show up in the to field for each recipient.
Examples
Note: Mailgun limits the number of recipients per message to 1000
Sender
In the Mailgun config file you have specified the from
address. If you would like, you can override this using the from
method. It accepts two arguments: email
and name
where the name
field is optional.
Subject
Setting the email subject
Reply-to
Setting a reply-to address
If the reply_to config setting is set, the reply-to will be automatically set for all messages You can overwrite this value by adding it to the message as displayed in the example.
Attachments
To add an attachment to the email you can use the attach
method. You can add multiple attachments.
The attach
method accepts 2 arguments:
$path
| The path to the image$name
| (optional) The remote name of the file (attachment is renamed server side)
Embedding Inline Images
Embedding inline images into your e-mails is very easy.
In your view you can use the embed
method and pass it the path to the file. This will return a CID (Content-ID) which will be used as the source
for the image. You can add multiple inline images to your message.
The embed
method accepts 2 arguments:
- $path | The path to the image
- $name | (optional) The remote name of the file (attachment is renamed server side)
Example
Input
Output
The $message variable is always passed to e-mail views by the Mailgun class.
Scheduling
Mailgun provides the ability to set a delivery time for emails up to 3 days in the future.
To do this you can make use of the later
method.
While messages are not guaranteed to arrive at exactly at the requested time due to the dynamic nature of the queue, Mailgun will do it's best.
The later
method works the same as the (default) send
method but it accepts 1 extra argument.
The extra argument is the amount of seconds (minutes, hours or days) from now the message should be send.
If the specified time exceeds the 3 day limit it will set the delivery time to the maximum of 3 days.
To send an email in 60 seconds from now you can do the following:
When passing a string or integer as the first argument, it will interpret it as seconds
. You can also specify the time in minutes
, hours
or days
by passing an array where the key is the type and the value is the amount.
For example, sending in 5 hours from now:
You can also pass a DateTime
or Carbon
date object.
Tagging
Sometimes it’s helpful to categorize your outgoing email traffic based on some criteria, perhaps for separate signup emails, password recovery emails or for user comments. Mailgun lets you tag each outgoing message with a custom tag. When you access the reporting page within the Mailgun control panel you can filter by those tags.
Warning: A single message may be marked with up to 3 tags. Maximum tag name length is 128 characters.
Mailgun allows you to have only limited amount of tags. You can have a total of 4000 unique tags.
To add a Tag to your email you can use the tag
method.
You can add a single tag to an email by providing a string
.
To add multiple tags to an email you can pass an array
of tags. (Max 3)
If you pass more than 3 tags to the
tag
method it will only use the first 3, the others will be ignored.
Campaigns
If you want your emails to be part of a campaign you created in Mailgun, you can add the campaign to a message with the campaign
method.
This method accepts a single ID string
or an array
of ID's (with a maximum of 3)
Tracking Clicks
Toggle clicks tracking on a per-message basis. Has higher priority than domain-level setting.
Tracking Opens
Toggle opens tracking on a per-message basis. Has higher priority than domain-level setting.
DKIM
Enable/disable DKIM signatures on per-message basis. (see Mailgun Docs)
Testmode
You can send messages in test mode. When you do this, Mailgun will accept the message but will not send it. This is useful for testing purposes.
Note: You are charged for messages sent in test mode.
To enabled testmode for all emails set the testmode
option in the config file to true
.
To enabled/disable testmode on a per message basis:
Alternative
Set the endpoint to Mailgun's Postbin. A Postbin is a web service that allows you to post data, which is then displayed through a browser. This allows you to quickly determine what is actually being transmitted to Mailgun's API.
Step 1 - Create a new Postbin.
Go to http://bin.mailgun.net. The Postbin will generate a special URL. Save that URL.
Step 2 - Configure the Mailgun client for using Postbin.
Tip: The bin id will be the URL part after bin.mailgun.net. It will be random generated letters and numbers. For example, the bin id in this URL, http://bin.mailgun.net/aecf68de, is "aecf68de".
In your config/mailgun.php
, change the following
to:
Now, all requests will be posted to the specified Postbin where you can review its contents.
Header
Add a custom header to your message
Data
Add custom data to your message
Dependency Injection
All the examples in this document are using the Mailgun
facade.
The Mailgun service is registered in the Container as mailgun
but you can also use the Interface Bogardo\Mailgun\Contracts\Mailgun
for dependency injection in your app.
Example
Mailing lists
You can programmatically create mailing lists using Mailgun Mailing List API. A mailing list is a group of members (recipients) which itself has an email address, like [email protected]. This address becomes an ID for this mailing list.
When you send a message to [email protected], all members of the list will receive a copy of it.
Complete support of the Mailing List API is not included in this package. Though, you can communicate with the API using this package which should give you all the flexibility you need.
Some examples
For a full overview of all the available endpoints and the accepted parameters
please review the Official API Documentation
Get all lists (paginated)
Get a list by address
Create a new list
Update a member of a list
Again, for a full overview of all the available endpoints and the accepted parameters
please review the Official API Documentation
OptInHandler
Utility for generating and validating an OptIn hash.
The typical flow for using this utility would be as follows:
Registration
- Recipient Requests Subscription
- Generate OptIn Link (with
OptInHandler
) - Email Recipient OptIn Link
Validation
- Recipient Clicks OptIn Link
- Validate OptIn Link (with
OptInHandler
) - Subscribe User
Example
Registration
Validation
Email Validation
Mailgun offers an email validation service which checks an email address on the following:
- Syntax checks (RFC defined grammar)
- DNS validation
- Spell checks
- Email Service Provider (ESP) specific local-part grammar (if available).
Single address
Validation a single address:
The validate
method returns the following object:
It will also try to correct typo's:
returns:
Multiple addresses
To validate multiple addresses you can use the parse
method.
This parses a delimiter separated list of email addresses into two lists: parsed addresses and unparsable portions. The parsed addresses are a list of addresses that are syntactically valid (and optionally have DNS and ESP specific grammar checks) the unparsable list is a list of characters sequences that the parser was not able to understand. These often align with invalid email addresses, but not always. Delimiter characters are comma (,) and semicolon (;).
The parse
method accepts two arguments:
addresses
: An array of addresses or a delimiter separated string of addressessyntaxOnly
: Perform only syntax checks or DNS and ESP specific validation as well. (true by default)
Syntax only validation:
returns:
Validation including DNS and ESP validation:
returns:
License
The MIT License (MIT). Please see License File for more information.
All versions of mailgun with dependencies
illuminate/support Version ^7.0
mailgun/mailgun-php Version ^3.0
nesbot/carbon Version ^2.0
php-http/message Version ^1.3