更新理由を理解しました。Gmailはポート465と587でSMTPサーバーを提供し、465はSSL、587はTLSであり、ESMTPはTLSを必要としているため、sslサーバーである465では機能しません...作品私は、誰かが理由を説明できるのであれば、なぜそれを行うのか明確ではありません:))
SMTPとTwistedを使用して電子メールを送信しようとしていますが、何も起こらず、エラーも出力も電子メールもありません。
出力
MacBookPro:EmailSender User$ python send_mime_email.py
What is your email address? [REMOVED]@gmail.com
What is your username (typically same as email address)? [REMOVED]@gmail.com
What is your password? [REMOVED]
What is your smtp server details? smtp.gmail.com:465
-------Email Details-------
Subject: Hello
Body: Hi, how are you?
Attachment: FTP_Commands.txt
-------Logging-------
2013-01-20 20:43:44-0500 [-] Log opened.
2013-01-20 20:43:44-0500 [-] Creating email
2013-01-20 20:43:44-0500 [-] Created Message.
2013-01-20 20:43:44-0500 [-] Starting factory <twisted.mail.smtp.ESMTPSenderFactory instance at 0xa3c508>
2013-01-20 20:43:44-0500 [-] Sending Email
2013-01-20 20:53:44-0500 [ESMTPSender,client] SMTP Client retrying server. Retry: 5
^C2013-01-20 20:54:47-0500 [-] Received SIGINT, shutting down.
2013-01-20 20:54:47-0500 [ESMTPSender,client] SMTP Client retrying server. Retry: 4
2013-01-20 20:54:47-0500 [-] Main loop terminated.
コード
import sys
import email
import email.mime.application
import sys
from OpenSSL.SSL import SSLv3_METHOD
from twisted.mail.smtp import ESMTPSenderFactory
from twisted.internet.ssl import ClientContextFactory
from twisted.internet.defer import Deferred
from twisted.internet import reactor
from twisted.python import log
try:
from cStringIO import cStringIO as StringIO
except ImportError:
from StringIO import StringIO
def create_email(address, subject, body, data):
print "Creating email"
# email block
# text body
msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = subject
# send it to ourselves to make it simple
msg['From'] = address
msg['To'] = address
# body
body = email.mime.Text.MIMEText(body)
msg.attach(body)
att = email.mime.application.MIMEApplication(data, _subtype="binary")
att.add_header('Content-Disposition','attachment',filename="data.bin")
msg.attach(att)
print "Created Message."
# Create a context factory which only allows SSLv3 and does not verify
# the peer's certificate.
return str(msg)
def send_email(smtp_server, smtp_port, username, password, from_, to, msg):
contextFactory = ClientContextFactory()
contextFactory.method = SSLv3_METHOD
resultDeferred = Deferred()
mime_obj = StringIO(str(msg))
senderFactory = ESMTPSenderFactory(
username,
password,
from_,
to,
mime_obj,
resultDeferred,
contextFactory=contextFactory)
reactor.connectTCP(smtp_server, smtp_port, senderFactory)
print "Sending Email"
return resultDeferred
if __name__ == '__main__':
email_address = raw_input("What is your email address? ")
username = raw_input("What is your username (typically same as email address)? ")
password = raw_input("What is your password? ")
sd = raw_input("What is your smtp server details? ")
ss, sp = sd.split(":")
sp = int(sp)
print "-------Email Details-------"
subject = raw_input("Subject: ")
body = raw_input("Body: ")
attachment_file = raw_input("Attachment: ")
o = open(attachment_file, "rb")
data = o.read()
o.close()
print "-------Logging-------"
log.startLogging(sys.stdout)
email_data = create_email(email_address, subject, body, data)
send_email(ss, sp, username, password, email_address, email_address, email_data)
reactor.run()