0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

f5: Pythonista でメール送信

Last updated at Posted at 2019-09-23

.
iphone でメールアプリが、
(iosのバージョンが古いためもあるけど)
「不安定」「懐疑」「不明通信で処理が遅い」
など問題が多いため、いまさらですが、即送信用に
Pythonista でメール送信をテスト

(1) メールの入力

 xxx.txt 保存ができるテキストエディタアプリ
 (Textforce や Textastic など)で、以下を入力

 1行目:送信先メールアドレス
 2行目:件名
 3行目以降:本文

(2) Open in -> Pythonista のスクリプト呼び出し

(3) コンソール上にて実行

[ 1. キッチン ]

Textforce (テキストエディタアプリ)

Pythonista v2.1.1

iphone SE / ios 9.3.5
iphone 5s / ios 12.1.4

[ 2. 仕込 ]

text-open-mail.py

# coding: utf-8

import os
import appex
import shutil
import console
import datetime

import smtplib
from email.MIMEText import MIMEText
from email.Utils import formatdate
from email.Header import Header

now1 = datetime.datetime.now()
now2 = '{0:%Y/%m/%d %H:%M:%S}'.format(now1)

cpedf1 = ''
cpedf3 = ''
cpedf4 = ''
cpedf5 = ''

# テキストアプリ -> open in -> pythonista /Shared/
# に テキストファイルをコピー後、ファイル名を取得(cpedf1)
def main():
	FILE = appex.get_file_paths()
	PYPATH = os.path.abspath(os.path.dirname(__file__))
	while os.path.basename(PYPATH) != 'Documents':
		PYPATH = os.path.abspath(os.path.join(PYPATH, os.pardir))
	PYPATH = os.path.join(PYPATH, 'Shared')
	if not os.path.isdir(PYPATH):
		os.mkdir(PYPATH)

	for i in FILE:
		if os.path.isfile(i):
			NAME = os.path.basename(i)
			shutil.copyfile(i, os.path.join(PYPATH, NAME))
			global cpedf1
			cpedf1 = NAME
		if os.path.isdir(i):
			shutil.copytree(i, PYPATH)

if __name__ == '__main__':
	if appex.is_running_extension():
		main()
	else:
		console.alert('Error Occured', 'Please launch from extension', 'ok', hide_cancel_button=True)

iu1 = 0
iu2 = ''
iu4 = 0
iu5 = ''
iu6 = 0
iu7 = ''
iu8 = ''
iu9 = ''

# テキストファイルを読み込み
# 1行目 TOアドレス、2行目 件名
# 1行目 @ が無い、または 空の場合はアドレス入力エラー
# 1行目 措定簡易入力(例 taro) は フルアドレスに変換
# (iu2: TO, iu7: 件名, iu5: 本文)
cpedf3 = './Shared/'
with open(cpedf3 + cpedf1) as linef:
	for linm in linef:
		iu8 = iu8 + linm
		iu3 = linm.strip()
		if iu1 == 0:
			if iu3 != '':
				iu6 = linm.find("@")
				if iu6 != -1:
					iu4 = 1
					iu9 = 'OK: アドレス'
					iu2 = iu3
				else:
					if iu3 == 'taro':
						iu4 = 1
						iu2 = 'taro2019@mailaddress.com'
					else:
						iu4 = 2
						iu9 = 'NG: アドレス @ 無し'
						iu2 = iu3
			else:
				iu4 = 3
				iu9 = 'NG: アドレス 空'
				iu2 = iu3
		elif iu1 == 1:
			iu7 = iu3
		else:
			iu5 = iu5 + linm
		iu1 = iu1 + 1
linef.close()

# コンソール上確認用(iu2: TO, iu7: 件名, iu5: 本文)
print ''
print iu9
print 'iu2: ' + iu2
print iu4
print 'iu7: ' + iu7
print ''
print 'iu5: ' + '\n' + iu5
print '本文ここまで'
print ''

# if: 通常送信、else: アラート送信
if iu4 == 1:
	TO = iu2
	cpedf4 = iu7
	cpedf5 = iu5 + '\n'
	BCC = 'backup@mailaddress.com'
else:
	TO = 'alert@mailaddress.com'
	cpedf4 = 'ALERT: アドレス入力エラー - ' + now2
	cpedf5 = iu9 + '\n\n' + iu8 + '\n' 
	BCC = ''

FROM = 'from@mailaddress.com'
CC = ['', '']
CC_ADD = ', '.join(CC) 
ENCOD = 'utf-8'

message = MIMEText(cpedf5.encode(ENCOD),'plain',ENCOD,)
message['Subject'] = str(Header(cpedf4, ENCOD))
message['From'] = FROM
message['To'] = TO
message['Cc'] = CC_ADD
message['Date'] = formatdate()

# SMTP (gmail の場合)
s = smtplib.SMTP('smtp.gmail.com', 587)

s.ehlo()
s.starttls()
s.ehlo()

# 送信元のアドレス、パスワード
s.login('from@mailaddress.com', 'password')

s.sendmail(FROM, [TO, CC_ADD, BCC], message.as_string())
s.close()

print '処理完了'
print ''


[ 3. デザート ]

※ 簡易的ざっくり作成なので
 プロセスエラーや細かなエラーは拾ってません

※ Pythonista v3 未確認(多分いけると思います)

※ gmail の場合は
「安全性の低いアプリの許可」-> 有効 が必要です

.

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?