How to send emails with Mandrill API

In this article I am going to show you how to launch a mailing in php using the API Mandrill. Comment, in case you didn’t know, that With Mandrill’s php API we will be able to send emails in bulk for services, for example, of newsletter, without suffering SPAM penalties from mail servers around the world.

Before getting fully into the process, I would like to say that I am going to include the complete integration code as well as explanations and other information that is necessary whether you are an experienced programmer or a neophyte in the world of PHP web development. I will deal with the following points:

  • What is the Mandrill API and what is it for?
  • What are the main features of this php API and why are they.
  • How to get the configuration data from the API.
  • And finally examples of integration with this library and how to start sending emails in our PHP backend development.

Content

  • What is Mandrill?
  • What is the Mandrill API for?
  • Installing the mandrill API
    • Using Mandrill in PHP without composer
  • Get the Mandrill API key
  • PHP Snippet to send emails with Mandrill

What is Mandrill?

I wouldn’t want to delve into the ins and outs of the API of this online service without first giving a brief introduction to what exactly it does.

Mandrill is nothing more than an online payment service to generate transactional shipments by email. You should also know that it is a service for Mailchimp subscription customers, therefore, without a subscription, you will not be able to use Mandrill (this also includes its API).

Their prices are not too high, and with only $20 we can send no more and no less than 25,000 emails.

Mandrill is usually used for e-commerce, but it can also be used for sending single emails, that is, with a single recipient.

What is the Mandrill API for?

The Mandrill API allows us to send mailings massive through our web programming. It includes functions to store and use users, email blacklists, messages and much more.

Although in this tutorial I am only going to show you a php example there are also integrations for Ruby, JSON, python and more.

Using the Mandrill API is easy and it will only require a few small steps before you start launching your integrated mailings.

Installing the mandrill API

For the installation of Mandrill I am going to proporte to make use of songwriter, If you don’t know what it is, I recommend that you visit my article on its use: how to use composer.

The composer command to install in our application is:

composer require mandrill/mandrill

How do you already know this command configures the library in our folder vendor For starting to work with the Mandrill API.

Using Mandrill in PHP without composer

If you don’t want to use composer To install the library, you can make use of it by downloading the php files from its repository and including them in your php script. Mandrill API bitbucket repository

Get the Mandrill API key

Before start sending emails with the mandrill API you must have one key For this end. You can obtain this key directly from the MailChimp website by going to the Mandrill section:

  1. Login to the MailChimp website.
  2. Access the section of your Profile.
  3. Click on the section transactional.
  4. Click on start or “get started”.
  5. go to settings and click on SMTP & API info.
  6. Click on New API Key.
  7. Copy the key into the following PHP function What do I propose?

PHP Snippet to send emails with Mandrill

If you have complied with the previous sections, all that remains is for you to see the php code needed to start mass mailing with Mandrill.

The following function is a simple example of use but fully prepared to send your first email with the API. Remember that you will have to make some changes to make it fully functional with your data.

function mail_mandrill( $to_email, $asunto, $mensaje ) {
	$apikey = 'XXXXXXXXXX'; //aquí debes indicar tu api key de mandrill
	$mandrill = new Mandrill($apikey);

	$message = new stdClass();
	$message->html = $mensaje;
	$message->text = $mensaje;
	$message->subject = $asunto;
	$message->from_email = "[email protected]";//Email del remitente
	$message->from_name  = "Sr. Código";//Nombre del remitente
	$message->to = array( array( "email" => $to_email ) );
	$message->track_opens = true;

	$response = $mandrill->messages->send($message);
}

In this function you must substitute 3 data to make it work:

  1. Job your Mandrill API key where do I indicate in line 2 of the script.
  2. Enter a valid sender email and name on lines 9 and 10.

Finally, do not forget that you must iinclude (require) the Mandrill libraryeither through autoload of composer or by requiring the main API file in the script.

Leave a Reply