python mail basic souce
You are currently viewing a revision titled "python mail basic souce", saved on 2022年12月1日 7:04 PM by fittingmind | |
---|---|
タイトル | python mail basic souce |
コンテンツ | 各自自分のメールアドレスに変更すること
index.html
<html> <head><title>python mail test</title></head> <body> <center> <form method = "post" action="mail.py"> name<br> <input type="text" name="name"> <br> address<br> <input type="text" name="add"> <br> Tel<br> <input type="text" name="tel"> <br> contents<br> <textarea name="bunsyou" rows="4" cols="40"></textarea><br> <br> <input type="submit" value="send"><input type="reset" value="reset"> </form> </center> </body> </html> mail.py #!/usr/bin/python3.6 print ('Content-Type: text/html\n\n') import cgi import sys import io #htmlのformからの入力しinputと言う関数に代入########################### form = cgi.FieldStorage() name = form.getvalue('name') add = form.getvalue('add') tel = form.getvalue('tel') bunsyou = form.getvalue('bunsyou') ####################################################################### sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') print("[Python CGI input]",file=sys.stderr) print('Content-Type: text/html; charset=UTF-8\n') ####################################################################### html_header = """ <!DOCTYPE html> <html> <head> </haed> <body> """ contents = """ <center> <h1>python mail 見本</h1> <br> 入力された内容は<br><br> """ html_footer = """ </center> </body> </html> """ ####################################################################### #変数代入されたものをprint############# print(html_header) print(contents) print(name) print("<br><br>") print(add) print("<br><br>") print(tel) print("<br><br>") print(bunsyou) print("<form method =\"post\" action=\"mailsend.py\">") print("<input type=\"hidden\" name=\"name\" value=\" ") print(name) print("\"> ") print("<input type=\"hidden\" name=\"add\" value=\" ") print(add) print("\"> ") print("<input type=\"hidden\" name=\"tel\" value=\" ") print(tel) print("\"> ") print("<input type=\"hidden\" name=\"bunsyou\" value=\" ") print(bunsyou) print("\"> ") print("<input type=\"submit\" value=\"send\">") print("<button type=\"button\" onclick=\"location.href=\'index.html\'\">reset</button>") print("</form>") print("<br><hr>") print(html_footer) ################################mailsend.py #!/usr/bin/python3.6 # -*- coding: utf-8 -*- from email import message import smtplib import ssl import cgi import sys import io from email.mime.text import MIMEText from email.header import Header from email.utils import formatdate ##################################################################### smtp_mode = 'normal' smtp_host = 's309.xrea.com' smtp_account_id = '001@ibaraki-school.net' smtp_account_pass = '1234qwer' from_mail = '001@ibaraki-school.net' to_mail = 'waterspring7@gmail.com' charset = 'ISO-2022-JP' ###################################################################### ####################################################################### sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') print("[Python CGI input]",file=sys.stderr) print('Content-Type: text/html; charset=UTF-8\n') ####################################################################### form = cgi.FieldStorage() name = form.getvalue('name') add = form.getvalue('add') tel = form.getvalue('tel') bunsyou = form.getvalue('bunsyou') #bunsyou = "test" ####################################################################### all = ('お名前:')+(name)+('\n')+('住所:')+(add)+('\n')+('電話:')+(tel)+('\n')+('内容:')+('\n')+(bunsyou)+('\r\n') #all = ('name')+(name)+('\r\n')+('address')+(add)+('\r\n')+('tel')+(tel)+('\r\n')+('contents')+('\r\n')+(bunsyou)+('\r\n') #all = ('お名前:')+(name)+('\r\n')+('住所:')+(add)+('\r\n')+('電話:')+(tel)+('\r\n')+('内容:') #all = "お名前" #all += name #all += "\n" #all = "住所" #all += add #all += "\n" #all = "電話" #all += tel #all += "\n" #all = "内容" #all += bunsyou #all += "\n" #jis='iso-2022-jp' # # ************************* # 本文 # ************************* #text = "Python\n日本語\nUTF-8\n" #text += "Python文字列操作マスター\nhttps://qiita.com/tomotaka_ito/items/594ee1396cf982ba9887" #text += "https://qiita.com/tomotaka_ito/items/594ee1396cf982ba9887" #text = text.encode(jis) # iso-2022-jp でテキスト全体をエンコード #typrint(text) #msg = MIMEText(text, 'plain', jis) # メール送信用データのベースを作成 msg = message.EmailMessage() msg.set_content(all); ####################################################################### msg = MIMEText(all, 'plain', charset) msg['Subject'] = 'pythonメールフォーム' msg['From'] = from_mail msg['To'] = to_mail if smtp_mode == 'starttls': server = smtplib.SMTP(smtp_host, 587, timeout=10) server.ehlo() server.starttls() server.ehlo() elif smtp_mode == 'ssl': context = ssl.create_default_context() server = smtplib.SMTP_SSL(smtp_host, 465, timeout=10, context=context) else: server = smtplib.SMTP(smtp_host, 25, timeout=10) server.login(smtp_account_id, smtp_account_pass) server.send_message(msg) server.quit() ####################################################################### html_header = """ <!DOCTYPE html> <html> <head> </haed> <body> """ contents = """ <center> <h1>メール送信完了</h1> <br> おくったよ~<br><br> """ html_footer = """ </center> </body> </html> """ ####################################################################### #print('Content-Type: text/html; charset=utf-8') print() print(html_header) print(contents) print(html_footer) .htaccess # BEGIN WordPress <IfModule mod_rewrite.c> AddHandler cgi-script .py RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress |
抜粋 | |
脚注 |