How to send E-mail Templates through Mailgun?

By : kalyan
Source: Stackoverflow.com
Question!

I am using Mailgun as my e-mail Service provider for sending E-mail to my Colleagues. I have created some attractive E-mail Templates. But, I don't know how to send it through Mailgun. Kindly guide me...

By : kalyan


Answers

this is simple to do if you're using the PHP library from mailgun, available here: https://github.com/mailgun/mailgun-php

The below code will send an email to a colleague of yours, you can add as many to fields as you need! Then simply edit the array 'html' :) and execute the script!

# Include the Autoloader (see "Libraries" for install instructions)
require 'vendor/autoload.php';
use Mailgun\Mailgun;

# Instantiate the client.
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
$domain = "YOURDomain.mailgun.org";

# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
    'from'    => '[email protected]',
    'to'      => '[email protected]',
    'subject' => 'Hello',
    'html'    => 'some html code <b> goes here </b> and works <span style=\"color:red\">fine</span>'
));


Or you can do a file read of the template and store it temporarily:

$fileloc = fopen("/path/to/email_template.php", 'r');
$fileread = fread($fileloc, filesize("/path/to/email_template.php"));

# Instantiate the client.
$mg = new Mailgun('<your_api_key>');
$domain = "domain.com";

# Make the call to the client.
$mg->sendMessage($domain, array('from'    => '[email protected]', 
                            'to'      => '[email protected]', 
                            'subject' => 'Subject', 
                            'html'    => $fileread));


This video can help you solving your question :)
By: admin