Pythonのsmtplibを使用して、ポート465 SMTP-over-SSLでメール送信です!
Python version 2.7.10です。
参考
http://docs.python.jp/2/library/email-examples.html
http://docs.python.jp/2/library/smtplib.html
# -*- coding: utf-8 -*- # Import smtplib for the actual sending function import smtplib # Import the email modules we'll need from email.mime.text import MIMEText me = 'my_email' # 送信元メールアドレス you = 'your_email' # 送信先メールアドレス # 本文 msg = MIMEText( "Hello!!" ) # 本文 # 件名、宛先 msg['Subject'] = 'Mail sending test.' #本文のタイトル msg['From'] = me msg['To'] = you # Send the message via our own SMTP server, but don't include the envelope header. s = smtplib.SMTP_SSL('host_uri', 465) # host_uriにsmtp接続先のURI s.login('username', 'password') # smtp接続先のユーザー名とパスワード s.sendmail( me, [you], msg.as_string() ) s.close()
簡単!もう少し複雑な操作(例えば添付ファイルとか、長文メールとか)はぐぐってね!
追記 2017.4.28
このままだと本文を日本語にすると文字化けしちゃう…
ということでちょっと変更しました。
# -*- coding: utf-8 -*- # Import smtplib for the actual sending function import smtplib # Import the email modules we'll need from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText me = 'my_email' you = 'your_email' msg = MIMEMultipart('alternative') # 件名、宛先 msg['Subject'] = 'メール送信テスト20170427' msg['From'] = me msg['To'] = you # 本文 body_text = 'こんにちは!\n\nようこそ!\n\n私が主役です!' msg.attach(MIMEText(body_text, 'plain', 'utf-8')) # Send the message via our own SMTP server, but don't include the envelope header. s = smtplib.SMTP_SSL('host_uri', 465) s.login('username', 'password') s.sendmail( me, [you], msg.as_string() ) s.close()
これで文字化けもしない!!
あと、改行もわかったので、使い込めそうです!