How to avoid your emails going to recipient's spam folder

Pranay
Sep 12, 2020  ยท  23949 views

This article focuses on steps to make sure your emails does not land as a spam in recipients mailbox by setting SPF record, DMARC record, DKIM signing and various other ways to add authenticity to your outgoing emails.

Error: "Google couldn't verify that 'yourdomain' actually sent this message"

I see this error when I first triggered emails from my 1and1 hosted website to my subscribers using C# code. I was using the 1and1 SMTP to send emails to subscribers but they landed in the spam folder marked with this error.

This occurs for multiple reasons. The Gmail or even any other email service would check the genuineness of an incoming email and then move them to spam folder if it doesn't looks genuine. In the today's world the rate of spam is increasing and it is very important to make our domain and our emails follow recommended guidelines to avoid them landing into recipient's Spam folder.

Image Text

Here are some basics that need to be take care for sure.

1. Adding SPF (Sender Policy Framework) record:

This is a new DNS record that need to be added to your domain. So go to your domain hosting provider and add the SPF records accordingly if it doesnt exist already. This is how I added for my domain on 1and1.com - https://www.ionos.com/help/domains/configuring-mail-servers-and-other-related-records/using-an-spf-record-to-prevent-spam/

Validate your SPF records here - https://mxtoolbox.com/SuperTool.aspx

2. Adding DMARC record (Domain-based Message Authentication, Reporting & Conformance)##

This is a TXT record that needs to be added in the DNS for a subdomain _dmarc.yourdomain.com.

When I tried creating a new subdomain 1and1 did not support '_' so I did not create a subdomain. Instead I directly added the TXT records with host name as _dmarc. It accepted and when I validated the domain for dmarc record, it was positive.

Image Text

Here is some external guide on DMARC and its validation - https://dmarcguide.globalcyberalliance.org/#/

3. Signing your email with DKIM (Domain Keys Identified Mail)##

This is something to do with all of your outgoing emails. If you are sending the emails using some tools like mailchimp or some other SMTP client, then they would take care of this. If you are sending emails from your code using the hosting service SMTP service then, you should have this implemented with in your code.

I was using 1and1 shared hosting service and my code is in asp.net mvc. So I have used the DKIM.NET library for signing the emails with DKIM.

DKIM implementation

  1. Generate public and private keys. An example is available here - https://dkimcore.org/tools/keys.html. It is not recommended to use keys generated online, as they might save and have access to both public and private keys. Instead generate them on your machine locally and use them.

  2. Install the DKIM.NET nuget package and update your code to include the mail message signing with DKIM. In the code we need to include the private keys as shown below. The public will be used only for the DNS TXT record.

    //rsa
    var privateKey = PrivateKeySigner.Create(@"-----BEGIN RSA PRIVATE KEY-----
    MQEAylLTSdzfY2U79Eg/z94i94JAhQ/VQ6ZKbIZLbra6qPFaH3qI
    .
    .
    .
    .
    .
    qpIeLOS36bivtvNNNIah+eXWCLwcE4jotzSDr1zaMJ2yOM/m8hAj
    -----END RSA PRIVATE KEY-----");
    
    var domainKeySigner = new DomainKeySigner(privateKey, "logicalfeed.com", "key1", new string[] { "From", "To", "Subject" });
    mail.DomainKeySign(domainKeySigner);
    
    var dkimSigner = new DkimSigner(privateKey, "logicalfeed.com", "key1", new string[] { "From", "To", "Subject" });
    mail.DkimSign(dkimSigner);
    
    SmtpServer.Send(mail);
    

    Full documentation of the library is here - https://github.com/dmcgiv/DKIM.Net/blob/master/readme.md. The selector I gave it as key1. It can be any text like key, default or anything else.

  3. Add a TXT record in the DNS with the DKIM public key.

    It is not required to created any new sub domain. Just create a new TXT record in your DNS settings and provided the required details. The host name should be in the format selector._domainkey.yourdomain.com

    Here, I used the text 'key1' as the selector hence the value 'key1._domainkey.logicalfeed.com'. Make sure the selector value (I have set it as 'key1') is same in the DNS settings and also in the code used for DKIM singing, if not you might see an error in validating the DKIM record.

Image Text

Validating SPF, DMARC and DKIM

4. Google Postmaster tools

Google Postmaster tools

Google provides this tool for Gmail spam analytics. So any emails going out to recipients Gmail account are analysed and data is displayed here. If there are lot of your emails marked as spam then they can be seen here. It shows everything about your outgoing emails to Gmail. Also, this requires you to Signup to Postmaster tools and validate your domain. This way you are verifying your domain ownership with Gmail and reducing the chances of your emails going to spam. So go ahead and signup to Google's Postmaster tools today - https://postmaster.google.com/

5. Mailing list

If you are sending emails in bulk I hope you got this email list genuinely from users who subscribed to your site. If you have bought this mailing list then there could be high chance that the recipient might not be interested in the topic being sent in your email.

In such cases, if some users do not like your email and mark it as spam, then there is a high chance that your email is marked as spam entirely. Sending multiple such emails can even decrease your domain/IP reputation which might cause permanent damage.

6. Unsubscribe options

There should be always a way for the recipient to unsubscribe from your mailing list.

  • A link in your email footer to unsubscribe from the mailing list is a good option.
  • There should also be an email header with an unsubscribe option. Having a header with a unsubscribe email will enable the this option in your email clients.

Below is the way I have added the header in my C# code.

MailMessage mail = new MailMessage();
mail.Headers.Add("List-Unsubscribe", "<mailto:admin@yourdomain.com?subject=unsubscribe>");

This is how you could see a unsubscribe link in Gmail if you have added a header. This is not just for Gmail but for most of the email clients on devices too. i.e., even the default email client on your iPhone.

Un subscribe

7. Email format and content

This is a continuous list and things keeps added as the Spam filters get updated day by day.

Any thing you include in your outgoing email to make it look spammy should be avoided. This includes the color of text/font to number of links you are including in the email body.

8. Using branded ESP:

Using famous Email service providers like Mailchimp would always does your job very easy, but of course you need to pay for their premium service. Even with using branded ESP, there are chances that your email may go in spam if you have not taken care of the format and content in the emails.

Hope this helps you. Did you avoid your emails and newsletters going to recipients spam folder, please do share. !!

AUTHOR

Pranay

A Software Engineer by profession, a part time blogger and an enthusiast programmer. You can find more about me here.


Post a comment




Thank you! You are now subscribed.

Sign up for our newsletter

Subscribe to receive updates on our latest posts.

Thank you! You are now subscribed.