Setting up a Mail Sending Server with Haraka
Tags: haraka, email
Published: 2024-01-08 04:16:35

---
title: Setting up a Mail Sending Server with Haraka
published: true
date: 2016-03-31 12:48:27 UTC
tags: Haraka, Email
canonical_url: https://chi.miantiao.me/post/haraka-smtp-server/
---

## Installation

```
npm install -g Haraka
```

## Initialization

```
haraka -i /etc/haraka
```

## DKIM Configuration

```
cd /etc/haraka/dkim
sh dkim_key_gen.sh miantiao.me
```

## DNS Configuration

1. SPF Related:

Add a TXT record.

If it's a top-level domain, leave the hostname empty or use @. If it's a subdomain, use mail as the hostname (modify according to your subdomain). Value: `v=spf1 ip4:49.74.15.209 ip4:192.168.199.219 ~all`

Test if SPF is deployed successfully: [http://www.openspf.org/Why?show-form=1](http://www.openspf.org/Why?show-form=1)

2. DKIM Related:

Add a TXT record.

If it's a top-level domain, use mail._domainkey as the hostname. If it's a subdomain, use mail._domainkey.mail (change the second domain to your subdomain).

Value:

```
v=DKIM1;p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoWeQnE0B4ALbs9ih9UuG8xTo0pWo8u25487bNF8gqxbl6Ca+LNFvgHc6ADdObUfIEp1LD6C/6Ufq+mVab2pUjYAKQOxxNj20nHzRsHbElwkUaGD2gus5d+HcRYHRCQXh1bGuWXvhh9v4Bk1A6I8mWIl0SnN+sm0VUAOxwWUmlEWHVt7EmxaqxmgstzIdINwgxLW0TU+C+CgUEYe75BN42JsbYvfB2pYKMh9SS57aPDXgghUKX+SdMJL3ztbhfUujr3VhWg+cqyCIj3Yku9LKzxRRtnpaYJ1alv510Z1exUzcWRjXXJQMnND3a/9wkXpHi8wIYNfj4WqAyzf74MrhRQIDAQAB”
```

## Adding Users

```
cd /etc/haraka
echo 'auth/flat_file' >> plugins
vim auth_flat_file.ini
# Change methods to:
# methods=LOGIN
```

In the file above, the left side in the `[users]` section is the account and the right side is the password (you can customize it):

```
[users]
matt=test
```

## Starting Haraka

```
haraka -c /etc/haraka
```

## Testing

```
# -*- coding: UTF-8 -*-
import smtplib  
from email.mime.text import MIMEText  

mailto_list=['test@qq.com'] # Recipient address
mail_host="127.0.0.1" # Server
mail_user="matt" # Username
mail_pass="test" # Password 
mail_postfix="miantiao.me" # Sender's domain
  
def send_mail(to_list,sub,content):  
    me="Haraka"+"<"+mail_user+"@"+mail_postfix+">"  
    msg = MIMEText(content,_subtype='plain',_charset='utf-8')
    msg['Subject'] = sub  
    msg['From'] = me  
    msg['To'] = ";".join(to_list)  
    try:  
        server = smtplib.SMTP()  
        server.connect(mail_host)  
        server.login(mail_user,mail_pass)  
        server.sendmail(me, to_list, msg.as_string())  
        server.close()  
        return True  
    except Exception, e:  
        print str(e)  
        return False  
if __name__ == ' __main__':
    for mail in mailto_list:
        if send_mail([mail],"Test Email Subject","""This is a test email"""):
            print "Sent successfully"
        else:
            print "Failed to send"
```

Email server health test:  
[http://www.mail-tester.com/](https://medium.com/r/?url=http%3A%2F%2Fwww.mail-tester.com%2F)

Check IP reputation:  
[http://anti-spam.org.cn/](https://medium.com/r/?url=http%3A%2F%2Fanti-spam.org.cn%2F)  
[http://www.justspam.org/check-an-ip](https://medium.com/r/?url=http%3A%2F%2Fwww.justspam.org%2Fcheck-an-ip)

![stat](https://c.statcounter.com/9823304/0/92a1d06c/1/)