Gmail requires all senders to authenticate with either SPF or DKIM.

CK CK 3788 
Created at
Updated at  
683 0 0

To successfully send emails to Gmail (and many other email providers), you need to authenticate your emails using SPF (Sender Policy Framework) and/or DKIM (DomainKeys Identified Mail). These authentication methods help verify that the email is coming from a legitimate source and reduce the chances of it being marked as spam.

 

Here's how to set up SPF and DKIM for your domain:

 

Step 1: Set Up SPF

  1. Create an SPF Record:
    • An SPF record is a type of DNS TXT record that specifies which mail servers are allowed to send email on behalf of your domain.
    • The record might look something like this:

      v=spf1 a mx ip4:YOUR_SERVER_IP -all
    • Replace YOUR_SERVER_IP with the actual IP address of your mail server.
    • This record means:
      • v=spf1: This indicates it's an SPF record.
      • a: Allows the domain’s A record IP to send mail.
      • mx: Allows the domain's MX servers to send mail.
      • ip4:YOUR_SERVER_IP: Authorizes your server's IP to send emails.
      • -all: Indicates that all other sources are not allowed to send emails on behalf of your domain (hard fail).
  2. Add the SPF Record to Your DNS:
    • Log in to your domain registrar or DNS hosting provider.
    • Navigate to the DNS management section.
    • Add a new TXT record with the SPF record you created.

Note that you can add multiple ip addresses

  • Here’s how to format an SPF record with multiple IPv4 addresses:

    v=spf1 ip4:192.0.2.1 ip4:203.0.113.2 ip4:198.51.100.3 -all
    

    Breakdown of the Record:

    • v=spf1: This specifies the SPF version being used (in this case, SPF version 1).
    • ip4:192.0.2.1: Authorizes the IP address 192.0.2.1 to send emails for your domain.
    • ip4:203.0.113.2: Authorizes the IP address 203.0.113.2 to send emails.
    • ip4:198.51.100.3: Authorizes the IP address 198.51.100.3 to send emails.
    • -all: This indicates a hard fail for any IP not listed (you can use ~all for a soft fail if you want to allow some flexibility).
  • Example for IPv6

    If you also need to include IPv6 addresses, you can do it like this:

    v=spf1 ip4:192.0.2.1 ip4:203.0.113.2 ip6:2001:db8::1 ip6:2001:db8::2 -all

  • Using Subnets
    You can also specify subnets using CIDR notation. For example, to authorize an entire subnet, you can do the following:

    v=spf1 ip4:192.0.2.0/24 -all
    

    This will allow any IP address from 192.0.2.0 to 192.0.2.255 to send emails for your domain.

  • Combining with Other Mechanisms
    You can also combine IP addresses with other SPF mechanisms like mx, a, or include.
    For example:

    v=spf1 ip4:192.0.2.1 mx include:example.com -all
    

    This means:

    • Allow the IP address 192.0.2.1.
    • Allow any IP address from the domain’s MX records.
    • Include the SPF record from example.com.

 

Step 2: Set Up DKIM

  1. Generate DKIM Keys:
    • You can generate DKIM keys using various tools or libraries. If you’re using Postfix with Dovecot, you can use OpenDKIM to create DKIM keys.
    • Install OpenDKIM:

      sudo yum install opendkim opendkim-tools -y
  2. Configure OpenDKIM:
    • Open the configuration file:

      sudo nano /etc/opendkim.conf
    • Add or edit the following lines:

      Syslog          yes
      Umask           002
      Mode            sv
      Canonicalization     relaxed/simple
      SubDomains      no
      # Replace 'example.com' with your domain
      Domain          example.com
      KeyFile         /etc/opendkim/keys/example.com/default.private
      Selector        default
      
    • Create the key directory:

      sudo mkdir /etc/opendkim/keys/example.com
  3. Generate the DKIM Key Pair:

    sudo opendkim-genkey -s default -d example.com
    sudo mv default.private /etc/opendkim/keys/example.com/
    sudo mv default.txt /etc/opendkim/keys/example.com/
  4. Add DKIM Public Key to DNS:
    • Open the generated default.txt file in the /etc/opendkim/keys/example.com/ directory. This file contains the DKIM public key.
    • Copy the entire contents of this file (it starts with v=DKIM1;).
    • Go to your DNS management interface and create a new TXT record:
      • Name: default._domainkey.example.com (replace with your domain)
      • Type: TXT
      • Value: Paste the contents of the default.txt file.
  5. Configure Postfix to Use OpenDKIM:
    • Edit the Postfix main configuration file:

      sudo nano /etc/postfix/main.cf
    • Add the following lines:

      milter_protocol = 2
      smtpd_milters = unix:/var/run/opendkim/opendkim.sock
      non_smtpd_milters = unix:/var/run/opendkim/opendkim.sock
  6. Start and Enable OpenDKIM:

    sudo systemctl start opendkim
    sudo systemctl enable opendkim
  7. Restart Postfix:

    sudo systemctl restart postfix

 

Step 3: Verify SPF and DKIM

Once you’ve set up SPF and DKIM:

  1. Verify DNS Records: Use tools like MXToolbox to check if your SPF and DKIM records are correctly set up.
  2. Send a Test Email: Send an email to your Gmail account and check the email headers. You should see entries for both SPF and DKIM in the headers, indicating they are passing.

Conclusion

Setting up SPF and DKIM properly helps improve email deliverability and reduces the chances of your emails being marked as spam, especially by Gmail. If you encounter any issues during the setup process, feel free to ask for help!

Tags DKIM SPF sendmail Facebook X
Comments 0
Similar posts
  1. Bash script to install postfix and sasl for sending email
    1,200
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    49
  3. How to send email in PHP ?
    898
  4. Send An Email Using A Bean
    1,020
  5. How To Send An E-mail By Code
    1,124
  1. Upgrading to GCC 4.9.4 in CentOS 7 requires installing a newer version manually
    586
  2. Force no caching in Apache by modifying the .htaccess file using appropriate headers
    2,688
  3. Beyond your conventional memory size with swap file instead of a partition
    1,146
  4. Guide for tmux shortcuts - split current pane, toggle pane
    1,311
  5. Tistory remote blogging example based in CLI & PHP
    2,746
  6. Generate Let’s Encrypt Wildcard SSL Certificate
    628
  7. Preventing Unauthorized Image Crawling by Bots: A Clever Discovery and How to Address It
    1,731
  8. Network diagnostic tool for IPv4/IPv6 - traceroute/traceroute6
    1,924
  9. chmod by type (directory and file permission)
    736
  10. Check network port is open or not on CentOS 7
    1,196
  11. Change MySQL port number for security purpose
    817
  12. CentOS warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory on CentOS 7
    1,084
  13. Set up an SMTP server on CentOS 7
    730
  14. How to mount windows shared folder on Centos 6.8 - mount error 12 = Cannot allocate memory to the mount.cifs(8)
    921
  15. Configuring IP Address on Centos 6.7
    794
  16. Adding new cron job for automated processing on every 10 minutes on Centos 6.7
    1,336
  17. Set timezone and time on Centos 6.7
    545
  18. How to optimize MySQL automatically in CentOS Linux
    1,010
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    29
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    55
  3. How do I create animated background for Google Meet?
    22,915
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    271
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    103
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    168
  7. Array of arguments passed to script in PHP
    97
  8. Invalid Auction Storms in Ad Tech
    106
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    207
  10. The Two Clocks of Ad Server Architecture
    77
  11. Frequency Cap Architecture Overview
    526
  12. Collecting Proof-of-Play from 100,000 Screens
    152
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    302
  14. What is RedisVL?
    114
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    246
  16. TPU & GPU Cluster Management Tools
    257
  17. The Concept of Scaffolding in AI Agents
    244
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    596
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    267
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    353
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    206
  22. Machine Learning: Training vs. Ranking
    229
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    202
  25. Designing a Data Curation System Around a Vector Database
    230
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    672
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    232
  30. Understanding Data Lineage
    272