A simple DMARC record policy builder for PHP

Last updated 5th September, 2023

I've been working a lot with email security lately, specifically around verifying that emails come from who they say they do.

A small part of this process was creating Dmarc records that had to be registered with the sending domain to verify their ownership. After doing a whole lot of ugly string concatenation; I ended up needing a cleaner, more readable, and testable system for building Dmarc records.

DMARC Record Builder

So, I put together this package that anyone can use to build records like these. It provides a fluent builder style syntax that makes working with these records much more practical and maintainable.

$record = new DmarcRecord();

$record->policy('none')
    ->subdomainPolicy('none')
    ->pct(100)
    ->rua('mailto:example@gmail.com')
    ->ruf('mailto:example@gmail.com')
    ->adkim('relaxed')
    ->aspf('relaxed')
    ->reporting('any')
    ->interval(604800);

The above policy, once converted to a string will output:

v=DMARC1; p=none; sp=none; pct=100; rua=mailto:example@gmail.com; ruf=mailto:example@gmail.com; fo=1; adkim=r; aspf=r; ri=604800;

dmarc.org have a great concise article that breaks down what Dmarc is, and all of the different values you can use to protect your domains if you're interested in learning more.

If you find this package useful, or just like the look of it - give it a star on GitHub, or feel free to reach out on Twitter.

No comments yet…

DevInTheWild.

Login To Add Comments.

Want More Like This?

Subscribe to occasional updates.