Reputation:
I'm on a private php heroku repo, and want to create a form that can allow users on my website to input their email address and submit the form and add the email to an existing mailing list on mailgun.
I've contacted MailGun support, they've said its possible and to look at the documentation.
The docs are vague, I can't figure it out on my own.
Is there code examples you can give me that may point me in the right direction?
Upvotes: 1
Views: 3845
Reputation: 1273
This is the line of thought: * Step 1 - Create a mailinglist that will contain your list of emails. * Step 2 - Create a form that asks a user for his email address, asks him to confirm the use of his email, sends an email address to the given address to confirm subscription. *
Step 1
# Include the Autoloader file (see documentation on how to install the php wrapper)
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client. Use your APIKey
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
# Issue the call to the client.
$result = $mgClient->post("lists", array(
'address' => '[email protected]',
'description' => 'A List of users'
));
This creates a list named 'A list of users' and is reachable internally using '[email protected]'
** Step 2 **
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Widget</title>
</head>
<body>
<form action="contact.php" method="POST" onsubmit="return check_the_box()">
<input name="email" type="text" required="yes">
<a href="#">I've read and agree to the ToS </a>
<input id="chx" type="checkbox">
<button type="submit" >Submit</button>
<span style="display: none" id="pls"> Please check the Terms of Service box</span>
</form>
<script type="text/javascript">
var box = document.getElementById("chx");
var alrt = document.getElementById("pls");
function check_the_box(){
if(!box.checked) {
alrt.setAttribute("style","display: inline");
return false
}
else {
return true
}
}
</script>
</body>
</html>
Then you need to capture the fields from this form and send an email:
# Include the Autoloader require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
$domain = "mydomain.org";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => '[email protected]',
'to' => '[email protected]',
'subject' => 'Confirmation Email',
'html' => 'Please, click this link to confirm you want to be part of my list <a href=\'http://website.com/confirmation.php?authcode=SOMEGENERATEDSTRING\'> CONFIRM </a>'
));
Create a generated string for each email address
The user will click on the link and will be redirected and finally added to the mailing list:
Step 3
Adding to mailing list
# Include the Autoloader (see "Libraries" for install instructions)
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
$listAddress = '[email protected]';
# Issue the call to the client.
$result = $mgClient->post("lists/$listAddress/members", array(
'address' => '[email protected]',
'description' => 'Signed up',
'subscribed' => true,
));
Done!
All you need to do is create the generation code that is unique to every user, did something like that:
function makeConfirmation($newguy) {
$confirmCode = md5($newguy.'somestringonlyknowntome');
return $confirmCode;
}
And finally before adding him to the mailing list I confirm the parameter sent to my server in the URL when he clicked the confirmation email
function checkConfirmation($conf,$email) {
if($conf== md5($email.'somestringonlyknowntome')) {
return true;
} else { return false; }
}
Edit accordingly..
Additionally:
Here's a good tutorial for you to follow available on Mailgun's github repository in PHP.
Good luck and let me know if you succeeded! :) I'd love to hear from you and write a blog about this on mailgun!
Upvotes: 3