If you want to send emails to a specified list of subscribers to a specific date or time, “Drip Emailing”  is a solution.

In this blog, we are explaining how to configure a drip email campaign through Mailgun API in our PHP program.

At Email composing time we decide, which time Email should reach to customer inbox. Email Drips are nothing but just a schedule email. Which will automatically be sent on the scheduled time.

Mailgun provides credentials which help you for easy API/SMTP configuration in your application to send drip emails.

If you don’t know how to configure Mailgun API in your PHP program then first see this link mailgun send email. Now, let’s dive into the ocean of Email Drip.


Watch the live demo or download code from the link given below

email drip in mailgun using php


MailGet is an automated email marketing tool in which you can integrate via Mailgun SMTP/ API and send Schedule Emails or Drip emails.

I hope you configure Mailgun API in your program by the link provided above .

To make Email Drip’s or Schedule Email, you need to follow the below  steps:-
1. Include mailgun.php in your main PHP page.

require 'vendor/autoload.php';

use Mailgun\Mailgun; 

2. Create an object of Mailgun through API key.

$mgClient = new Mailgun('<-- API KEY -->');

3. In the domain information page, you will get “default SMTP  login”.  Copy string after @ symbol and initialize your domain variable with this string.

4. Finally, send Email by calling sendMessage() method of Mailgun object with parameters such as domain,  sender, receiver, subject, text, and o:deliverytime.

o:deliverytime: On this time the receiver gets the message in his inbox.

According to mailgun doc: ” Messages can be scheduled for a maximum of 3 days in the future “. so,  Think twice before setting o:deliverytime.

Tutorial Scripts in detail

Full code, which is used for Email Drip’s is listed below with proper explanation :

index.php

It is the main program file . We have configured Mailgun API  in this file and accessed its features, like creating object of Mailgun, to sent mail with the help of sendMessage()method . we also set  o:deliverytime to drip email to receiver’s inbox at a specific time .

<?php
require 'vendor/autoload.php';
use Mailgun\Mailgun;
?>
<html>
<head>
<title>
Drip Email In Mailgun Using PHP
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/moment.js" type="text/javascript"></script>
<script src="js/bootstrap.js" type="text/javascript"></script>
<script src="js/bootstrap-datetimepicker.min.js" type="text/javascript"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" type="text/css"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<script>
function checkdate() {
var values = $(".datetime").val();
if (values == "") {
$(".datetime").css("background-color", "rgba(229, 87, 87, 0.33)");
$(".datetime").val("select date and time");
return false;
}
var selected_date_time = values.split(" ");
var selected_date = selected_date_time[0];
var selected_time = selected_date_time[1];
var selected_date_split = selected_date.split("/");
var day = selected_date_split[0];
var month = selected_date_split[1];
var year = selected_date_split[2];
var selected_time_split = selected_time.split(":");
var hour = selected_time_split[0];
var minutes = selected_time_split[1];
var seconds = selected_time_split[2];
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var mon = today.getUTCMonth() + 1;
var d = today.getUTCDate();
var y = today.getUTCFullYear();
var your_date = new Date(year, (month - 1), day, hour, minutes, seconds);
var your_date_milliseconds = your_date.getTime();
var today_date = new Date(y, (mon - 1), d, h, m, s);
var today_date_milliseconds = today_date.getTime();
if (your_date_milliseconds < today_date_milliseconds) {
$(".datetime").css("background-color", "rgba(229, 87, 87, 0.33)");
$(".datetime").val(" Date / Time should not be less than current date.");
return false;
}
Date.prototype.addDays = function(days)
{
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}
var after_3_days = today_date.addDays(3);
var after_3_days_milliseconds = after_3_days.getTime();
if (your_date_milliseconds > after_3_days_milliseconds) {
$(".datetime").css("background-color", "rgba(229, 87, 87, 0.33)");
$(".datetime").val("Date /Time should not be more than 3 days ");
return false;
}
$(".dr").val(values);
return true;
}
function normal() {
$(".datetime").css("background-color", " ");
$(".datetime").val(" ");
}
</script>
</head>
<body>
<div class="container">
<div class="row">
</div>
<div class="row">
<div class="col-md-12">
<div id="main">
<h1>Drip Email in Mailgun Using PHP</h1>
</div>
</div>
<div class="col-md-12">
<div class="matter">
<div id="login">
<h2>Send Email</h2>
<hr/>
<form action="index.php" onsubmit=" return checkdate()" method="post">
<label class="lab">Sender's Name :</label>
<input type="text" name="sname" id="to" placeholder="Senders Name" required />
<label class="lab">Receiver's Email Address :</label>
<input type="email" name="to" id="to" placeholder="Receiver's email address" required />
<label class="lab">Drip Time:</label><br><div class="clr"></div>
<div id="datetimepicker2" class="input-append">
<input class="datetime" style="width:90%;" data-format="dd/MM/yyyy hh:mm:ss" type="text" required disabled>
<span onclick="normal()" style="margin-top:8px; padding-bottom:16px;" class="add-on"><i onclick="normal()" class="glyphicon glyphicon-calendar"></i>
</span>
</div>
<input class="dr" name="mydrip" type="hidden" >
<script type="text/javascript">
$(function() {
$('#datetimepicker2').datetimepicker({
language: 'en',
pick12HourFormat: false,
defaultTime: 'value',
minuteStep: 1,
disableFocus: true,
template: 'dropdown',
showMeridian: false
});
});
</script>
<label class="lab">Email type:</label><div class="clr"></div>
<div class="lab">
<input style="vertical-align: middle;" type="radio" value="def" name="etype" checked>Default &nbsp;&nbsp;
<input type="radio" value="cc" name="etype" >cc&nbsp;&nbsp;
<input type="radio" value="bcc" name="etype" >bcc&nbsp;&nbsp; </div>
<div class="clr"></div>
<label class="lab">Subject :</label>
<input type="text" name="subject" id="subject" placeholder="subject" required />
<label class="lab">Message body :</label><div class="clr"></div>
<div class="lab">
<input type="radio" value="text" name="msgtype" checked>Text &nbsp;&nbsp;
<input type="radio" value="html" name="msgtype" >HTML</div>&nbsp;&nbsp;
<textarea type="text" name="msg" id="msg" placeholder="Enter your message here.." required ></textarea><br><br>
<input type="submit" value=" Send " name="submit"/>
<span></span>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<?php
if (isset($_POST['sname'])) {
$sname = $_POST['sname'];
$to = $_POST['to'];
$subject = $_POST['subject'];
$msg = $_POST['msg'];
$msgtype = $_POST['msgtype'];
$mydrip = $_POST['mydrip'];
$hello = explode(" ", $mydrip);
for ($x = 0; $x < count($hello); $x++) {
if ($x == 0) {
$spliting = explode("/", $hello[$x]);
$day = $spliting[0];
$month = $spliting[1];
$year = $spliting[2];
}
if ($x == 1) {
$time = $hello[$x];
}
}
$timestamp = strtotime("$year-$month-$day");
$daychars = date('D', $timestamp);
$monchars = date('F', $timestamp);
$dt = new DateTime("$monchars $day $year $time UTC");
$dripday = $dt->format(DATE_RFC822);
$date_rfc = explode(" ", $dripday);
if ($msgtype == 'text') {
$html = '';
} else {
$msg = htmlentities($msg);
$html = $msg;
$msg = '';
}
for ($x = 0; $x < 6; $x++) {
switch ($x) {
case 0:
$date_rfc_day = $date_rfc[$x];
break;
case 1:
$date_rfc_date = $date_rfc[$x];
break;
case 2:
$date_rfc_month = $date_rfc[$x];
break;
case 3:
$date_rfc_year = $year;
break;
case 4:
$date_rfc_time = $date_rfc[$x];
break;
case 5:
$date_rfc_zone = $date_rfc[$x];
break;
}
}
$mgClient = new Mailgun('<-- API KEY -->');
$domain = "<-- DEFAULT SMTP LOGIN DOMAIN -->";
$result = $mgClient->sendMessage($domain, array(
"from" => "$sname < <---YOU@YOUR_DOMAIN_NAME---> >",
"to" => "Baz <$to>",
"subject" => "$subject",
"text" => "$msg!",
'o:deliverytime' => "$date_rfc_day $date_rfc_date $date_rfc_month $date_rfc_year $date_rfc_time +0530"
));
echo "<script>alert('Email Sent Successfully!!');</script>";
}
?>

Style.css

Includes all  basic styling of HTML elements.

@import url(http://fonts.googleapis.com/css?family=Raleway);
#main{
margin:50px auto;
font-family:raleway;
}
span{
color:red;
}
h2{
font-weight: 600;
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 15px;
}
hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}
#login{
width:40%;
margin:0 auto;
display:inline-block;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
}
textarea{
margin-top: 8px;
font-size: 16px;
font-family:raleway;
}
input[type=text],[type=email],input[type=password]{
height:35px;
width:99.5%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
}
input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: 15px;
}
#profile{
padding:50px;
border:1px dashed grey;
font-size:20px;
background-color:#DCE6F7;
}
#logout{
float:right;
padding:5px;
border:dashed 1px gray;
}
a{
text-decoration:none;
color: cornflowerblue;
}
i{
color: cornflowerblue;
}
#note {
clear: left;
padding-top: 20px;
margin-left: 20px;
font-size: 18px;
}
#formget{
float:right;
}
h1{
font-weight: 600;
text-align: center;
display:inlne;
alignment-adjust: center;
margin:0 auto;
width:100%;
}
textarea[type=text]{
width:100%;
height:200px;
}
.matter{
alignment-adjust: central;
margin:0 auto;
text-align: center;
}
.clr{
clear:left;
}
.lab{
font-size: 110%;
float:left;
}
@media screen and (max-width: 800px) {
#login{
width:330px;
}
}
.input-append .add-on, .input-prepend .add-on {
height:35px;
}
#to{
height:35px;
}
input[type=radio]{
margin:0;
}

Conclusion :

That’s all about how to send Drip Emails in Mailgun Using PHP. I hope you like the article, and I am sure you will use this script in some of your programs as well.

Feel free to visit our blog. You can give the comment in the space provided below .