Mailgun using just PHP

Found myself making a simple page with just contact page, needed to send an email from the website using mailgun.

composer require mailgun/mailgun-php

Then make a contact.php

<?php 
// Start a Session
session_start();
require 'vendor/autoload.php';
use Mailgun\Mailgun;
$post = $_POST;
if (!empty($post) && !empty($post['email']) && !empty($post['name']) && !empty($post['subject']) && !empty($post['message']) && !empty($post['phone']) && !empty($post['company'])) {
    $mgClient = Mailgun::create('key');
    $domain = "mg.domain.vc";
    $message = [
    'from'    => $post['email'],
    'to'      => '[email protected]',
    'subject' => 'Contact From '.$post['company'].' - '.$post['name'].' - '.$post['subject'],
    'text'    => $post['message'].'<br><br>Phone :'.$post['phone']
    ];
    $mgClient->messages()->send($domain, $message);
    $_SESSION['message'] = 'Your message is well received';
}
header("Location: https://www.domain.vc");
?>

Subscribe to You Live What You Learn

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe