-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail_test.py
executable file
·70 lines (55 loc) · 1.87 KB
/
mail_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# from flask import Flask
# from flask_mail import Mail
# import smtplib
# import email.utils
# from email.mime.text import MIMEText
# app = Flask(__name__)
# mail = Mail(app)
# from flask_mail import Message
# @app.route("/")
# def index():
# msg = Message("Hello",
# sender="[email protected]",
# recipients=["[email protected]"])
# mail.send(msg)
# return "<h1>Msg sent</h1>"
# msg = MIMEText('This is the body of the message.')
# msg['To'] = email.utils.formataddr(('Recipient', '[email protected]'))
# msg['From'] = email.utils.formataddr(('Author', '[email protected]'))
# msg['Subject'] = 'Simple test message'
# server = smtplib.SMTP('localhost')
# server.set_debuglevel(True) # show communication with the server
# try:
# server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
# finally:
# server.quit()
#if __name__ == '__main__':
#app.run(host='0.0.0.0', port=4000, debug=True)
# msg = MIMEText("test messge")
# msg['Subject'] = "New Card Request"
# msg['From'] = "[email protected]"
# msg['To'] = "[email protected]"
# mail_server = smtplib.SMTP('localhost',port=1025)
# mail_server.send_message(msg)
# mail_server.quit()
# Create the message
import sys
import smtplib
import dns.resolver
email = "[email protected]"
url = email.split("@")[1]
answers = dns.resolver.query(url, 'MX')
if len(answers) <= 0:
sys.stderr.write('No mail servers found for destination\n')
sys.exit(1)
# Just pick the first answer
server = str(answers[0].exchange)
# Add the From: and To: headers
fromaddr = '[email protected]'
toaddr = email
body = 'some email body'
msg = "From: {}\r\nTo: {}\r\n\r\n{}".format(fromaddr, toaddr, body)
server = smtplib.SMTP(server)
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddr, msg)
server.quit()