通过Python为自定义MTA生成DKIM签名

2024-05-29 00:17:42 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,所以我对DKIM并不完全迷路。我知道用您的公钥编码和设置DNS记录的一般规则,等等。我遇到的问题是合并出站电子邮件的“动态”签名并将其注入到我的头中,因为我的MTA是自定义的,用python从头开始编写的,不是开箱即用的。想知道是否有人有一个小python例子,用DKIM发送一封电子邮件,并完成所有动作。例如,使用与dns设置中的姐妹(公共)密钥匹配的私钥生成256位加密体。在


Tags: 编码dns规则电子邮件记录动态例子公钥
3条回答

这应该有帮助。在

https://launchpad.net/dkimpy

我查看了项目中包含的测试和命令行工具,以了解如何使用它。在

下面是一个代码片段,它将告诉您如何使用它。抱歉,我不能提供更多。在

    self.dkim_private = open(os.path.join(settings.PROJECT_DIR, 'private_key.pem')).read()
    ... snip ...
    msg = MIMEMultipart('alternative')
    msg['From'] = "{0} <{1}>".format(self.sendfrom_name, self.sendfrom)
    msg['To'] = self.sendto
    msg['Date'] = formatdate(localtime=True)
    msg['Message-ID'] = self.message_id
    msg['Subject'] = self.subject

    msg.attach(MIMEText(unicodedata.normalize('NFKD', self.body_text), 'plain'))
    msg.attach(MIMEText(self.body, 'html'))

    sig = dkim.sign(msg.as_string(), 'myselector',
                    from_domain, self.dkim_private,
                    include_headers=['from', 'to', 'subject', 'message-id'])
    msg['DKIM-Signature'] = sig[len("DKIM-Signature: "):]

然后使用可以使用smtplib发送电子邮件。在

在这里可以轻松生成私钥和公钥:

https://www.port25.com/support/domainkeysdkim-wizard/

我要感谢乔治齐默的上述回答。我在Python3.6.2上运行时遇到了一些困难,因为自2.x版本以来,一些“字节”/“string”项发生了变化。下面是生成MIMEMultipart(text/HTML)并用DKIM签名的代码。我使用了dkimpy-0.6.2。在

我的第一个StackOverflow帖子。希望对你有帮助。。。在

import smtplib, dkim, time, os

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


print('Content-Type: text/plain')
print('')
msg = MIMEMultipart('alternative')
msg['From'] = 'test@example.com'
msg['To'] = 'person@anotherexample.com'
msg['Subject'] = ' Test Subject'
msg['Message-ID'] = "<" + str(time.time()) + "-1234567890@example.com" + ">"

# Create the body of the message (a plain-text and an HTML version).
text = """\
Test email displayed as text only
"""

html = """\
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
    <head>
        <title>Test DKMI Email</title>
    </head>
    <body>
        HTML Body of Test DKIM
    </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

# DKIM Private Key for example.com RSA-2048bit
privateKey = open(os.path.join('C:\\dev\\python\\', '2048.example.com.priv')).read()

# Specify headers in "byte" form
headers=[b'from', b'to', b'subject', b'message-id']

# Generate message signature
sig = dkim.sign(msg.as_bytes(), b'introduction', b'example.com', privateKey.encode(), include_headers=headers)
sig = sig.decode()

# Add the DKIM-Signature
msg['DKIM-Signature'] = sig[len("DKIM-Signature: "):]

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()

在前面两个答案的基础上,我还有一些额外的建议。在

  1. 一定要跑
pip install dkimpy
  1. 生成私钥。在Unix上:
^{pr2}$
  1. 创造公众。在Unix上:
openssl rsa -in dkimprivatekey.pem -out public.pem -pubout
  1. 使用选择器“introduction”将公钥添加到您的DNS中(上面的示例就是这样使用的)。在
  2. 在上面的代码中提供私钥的路径和文件名(上面的示例使用C:\dev\python\和{})

相关问题 更多 >

    热门问题