How to integrate mailgun in any framework
Many at times when you send email using server via SMTP, emails does not get delivered. Really, I was facing this issue when emails were not delivered to Yahoo server. This is a very common problem and to solve this problem, Mailgun is one of the best tools. If you using mailgun, then you can track every email from their dashboard. Believe me, it is very user-friendly and highly useful. I can’t explain my feeling when I was able to penetrate into Yahoo server. I am sure if you are facing this problem, then surely Mailgun would give you a sigh of relief.
How to integrate Mailgun?
- First of all, you will have to check whether SMTP is installed on your server or not. If not then install SMTP.
- Then, verify your domain in Mailgun dashboard. Next, add your domain. You can find SPF record and DKIM record in the dashboard itself. Add these two in your domain account as a DNS TXT record. Click here to know how to verify your domain.
- Now, download Mailgun library from this link and upload to your root directory.
- Copy the following code and paste it to your page.
$to = "anupam@tcatechnology.com"; //Where you want to send
$subject = "Enter email subject here.";
$message = '<table><tr><td>Test body</td></tr></table>
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mailgun.org'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'ENTER YOUR MAILGUN SMTP USER NAME'; // SMTP username
$mail->Password = 'ENTER YOUR MAILGUN SMTP PASSWORD'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, only 'tls' is accepted
$mail->From = 'info@yourdomainname.com';
$mail->FromName = 'Your company name';
$mail->addAddress($to); // Add a recipient
$mail->isHTML(true); // For HTML mail format
$mail->Subject = $subject;
$mail->Body = $message;
if(!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo "Success";
}
You are done. You can now check email log and complete details from this link. Screenshot of one such data is presented below.

That’s all for now. In the last few days, received many emails regarding configuring SSL Certificate. So, my next topic would be how to move your website to https. Subscribe to our mailing list to remain tuned and receive our update directly in your mailbox.
Bye and happy developing folks.
Anupam Maity, August 11, 2016