日本語を含まない半角英数字だけの添付ファイル名であれば 受信側アウトルックで ちゃんとCSVファイルとして表示されます。
こちらの記事を参考に対処に努めたのですが、テキストファイルを送付されている事例がゆえ、自分の適用方法に問題があるのか うまくいきません(テキストファイルとして、かつ無名の添付ファイルと OUTLOOK上で表されてしまいます)
どなたか対策を ご教示頂けませんでしょうか
よろしくお願い致します。
PYTHON
1def send_Mail(self, toaddress, ccaddress, bccaddress, subject, body, filepath, parent): 2 try: 3 4 cset = 'utf-8' 5 msg = MIMEMultipart() 6 msg["Subject"] = subject 7 msg["From"] = self.mlsrv_fraddress 8 msg["To"] = toaddress 9 msg["Cc"] = ccaddress 10 msg["Bcc"] = bccaddress 11 msg["Date"] = formatdate() 12 body = MIMEText(body.encode(cset), 'plain', cset) 13 msg.attach(body) 14 15 16 if not (filepath == ""): 17 18 filename = os.path.basename(filepath) 19 with open(filepath, 'rt', encoding='Shift-JIS') as f: 20 content = f.read() 21 attachment = MIMEText(content, 'plain', 'Shift-JIS') 22 attachment.set_param('name', filename) #アウトルックのための対応 23 attachment.add_header('Content-Disposition', 'attachment', filename=('iso2022-jp', '', filename)) 24 msg.attach(attachment) 25 26 27 to_cc_bcc = [toaddress, ccaddress, bccaddress] 28 while True: 29 if ("" in to_cc_bcc): 30 delindex = to_cc_bcc.index("") 31 del to_cc_bcc[delindex] 32 else: 33 break 34 35 36 37 smtp = smtplib.SMTP(self.mlsrv, self.mlsrv_port) 38 smtp.login(self.mlsrv_id, self.mlsrv_pw) 39 40 result = smtp.sendmail(self.mlsrv_fraddress, to_cc_bcc, msg.as_string()) 41 42 except Exception as e: 43 tkmb.showerror("Abort", e, parent=parent) 44 result = 500 45 finally: 46 return result
20200628 画像差替え
20200702 03:37AM コメントに記載した画像追加
20200702 16:12PM 現況の添付ファイルに関わるコード
Python
1filename = os.path.basename(filepath) 2with open(filepath, 'rt', encoding='Shift-JIS') as f: 3 content = f.read() 4 attachment = MIMEText(content, 'plain', 'Shift-JIS', policy=policy.SMTP) 5 contentType = Header('text/comma-separated-values;', header_name='Content-Type') 6 contentType.append('name="') 7 contentType.append(filename, charset='iso-2022-jp') 8 contentType.append('"') 9 attachment['Content-Type'] = contentType 10 contentDisp = Header('attachment;', header_name='Content-Disposition') 11 contentDisp.append('filename="') 12 contentDisp.append(filename, charset='iso-2022-jp') 13 contentDisp.append('"') 14 attachment['Content-Disposition'] = contentDisp 15
20200702 16:56 del文 追加
Python
1filename = os.path.basename(filepath) 2with open(filepath, 'rt', encoding='Shift-JIS') as f: 3 content = f.read() 4 attachment = MIMEText(content, 'plain', 'Shift-JIS', policy=policy.SMTP) 5 contentType = Header('text/comma-separated-values;', header_name='Content-Type') 6 contentType.append('name="') 7 contentType.append(filename, charset='iso-2022-jp') 8 contentType.append('"') 9 del attachment['Content-Type'] 10 attachment['Content-Type'] = contentType 11 contentDisp = Header('attachment;', header_name='Content-Disposition') 12 contentDisp.append('filename="') 13 contentDisp.append(filename, charset='iso-2022-jp') 14 contentDisp.append('"') 15 attachment['Content-Disposition'] = contentDisp 16 prtin(attachment)
20200702 20:41 最新PGとその実行結果
Python
1filename = os.path.basename(filepath) 2with open(filepath, 'rt', encoding='Shift-JIS') as f: 3 content = f.read() 4 attachment = MIMEText(content, 'plain', 'Shift-JIS') 5 contentDisp = Header('attachment;', header_name='Content-Disposition') 6 contentDisp.append('filename="') 7 contentDisp.append(filename, charset='iso-2022-jp') 8 contentDisp.append('"') 9 attachment['Content-Disposition'] = contentDisp 10 print(attachment)
20200703 0829 結果添付
MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Type: text/comma-separated-values; charset="shift-jis"; name="=?iso-2022-jp?b?MjAyMDA3MDMwODI3NTcuY3N2?=" Content-Disposition: attachment; filename="=?iso-2022-jp?b?MjAyMDA3MDMwODI3NTcuY3N2?="
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/01 14:26
2020/07/01 21:54
2020/07/01 22:36
2020/07/02 00:14
2020/07/02 01:10
2020/07/02 01:17
2020/07/02 01:54
2020/07/02 02:00
2020/07/02 05:43
2020/07/02 05:52 編集
2020/07/02 06:17
2020/07/02 06:23
2020/07/02 06:31
2020/07/02 06:44 編集
2020/07/02 06:50
2020/07/02 06:59
2020/07/02 07:01
2020/07/02 07:06
2020/07/02 07:08
2020/07/02 07:29
2020/07/02 07:38
2020/07/02 08:05
2020/07/02 08:09
2020/07/02 08:15
2020/07/02 08:22
2020/07/02 08:35
2020/07/02 09:01
2020/07/02 11:45
2020/07/02 22:24
2020/07/02 22:44
2020/07/02 23:39
2020/07/03 01:47
2020/07/03 02:08
2020/07/03 02:14
2020/07/03 02:32