質問編集履歴

2

プログラムコードを更新しました。

2019/10/29 04:26

投稿

raspypy
raspypy

スコア247

test CHANGED
File without changes
test CHANGED
@@ -58,108 +58,82 @@
58
58
 
59
59
 
60
60
 
61
- import os.path
62
-
63
61
  import smtplib
64
62
 
65
- import datetime
63
+ from email.mime.text import MIMEText
66
64
 
67
- from email import Encoders
65
+ from email.utils import formatdate
68
66
 
69
- from email.Utils import formatdate
70
-
71
- from email.MIMEBase import MIMEBase
67
+ import ssl
72
-
73
- from email.MIMEMultipart import MIMEMultipart
74
-
75
- from email.MIMEText import MIMEText
76
68
 
77
69
 
78
70
 
79
- # [Mail]設定
71
+ FROM_ADDRESS = 'test@gmail.com'
80
72
 
81
- subject = "データ更新"
73
+ MY_PASSWORD = '12345'
82
74
 
83
- body = "test"
75
+ TO_ADDRESS = 'test@gmail.com'
84
76
 
85
- from_addr = "test@gmail.com"
77
+ BCC = ''
86
78
 
87
- PASSWORD = "12345"
79
+ SUBJECT = 'GmailのSMTPサーバ経由'
88
80
 
89
- to_addr = "test@gmail.com"
90
-
91
- SMTP = "smtp.gmail.com"
81
+ BODY = 'pythonでメール送信'
92
-
93
- PORT = 587
94
82
 
95
83
 
96
84
 
97
- def create_message(from_addr, to_addr, subject, body, mime=None, attach_file=None):
98
-
99
- msg = MIMEMultipart()
100
-
101
- msg["From"] = from_addr
102
-
103
- msg["To"] = to_addr
104
-
105
- msg["Date"] = formatdate()
106
-
107
- msg["Subject"] = subject
108
-
109
- body = MIMEText(body)
110
-
111
- msg.attach(body)
112
85
 
113
86
 
87
+ def create_message(from_addr, to_addr, bcc_addrs, subject, body):
114
88
 
115
- if mime != None and attach_file != None:
89
+ msg = MIMEText(body)
116
90
 
117
- attachment = MIMEBase(mime['type'],mime['subtype'])
91
+ msg['Subject'] = subject
118
92
 
119
- file = open(attach_file['path'])
93
+ msg['From'] = from_addr
120
94
 
121
- attachment.set_payload(file.read())
95
+ msg['To'] = to_addr
122
96
 
123
- file.close()
97
+ msg['Bcc'] = bcc_addrs
124
98
 
125
- Encoders.encode_base64(attachment)
99
+ msg['Date'] = formatdate()
126
100
 
127
- msg.attach(attachment)
101
+ return msg
128
102
 
129
- attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
130
103
 
131
- return msg
132
104
 
133
105
 
134
106
 
135
107
  def send(from_addr, to_addrs, msg):
136
108
 
109
+ #context = ssl.create_default_context()
110
+
111
+ smtpobj = smtplib.SMTP_SSL('smtp.gmail.com', 465, timeout=10)
112
+
113
+ smtpobj.login(FROM_ADDRESS, MY_PASSWORD)
114
+
115
+ smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
116
+
117
+ smtpobj.close()
137
118
 
138
119
 
139
- smtpobj = smtplib.SMTP(SMTP, PORT)
140
-
141
- smtpobj.ehlo()
142
-
143
- smtpobj.starttls()
144
-
145
- smtpobj.ehlo()
146
-
147
-
148
-
149
- smtpobj.login(from_addr, PASSWORD)
150
-
151
- smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
152
-
153
- smtpobj.close()
154
120
 
155
121
 
156
122
 
157
123
  if __name__ == '__main__':
158
124
 
159
- body = "データを更新しました。"
160
125
 
161
- msg = create_message(from_addr, to_addr, subject, body)
162
126
 
127
+ to_addr = TO_ADDRESS
128
+
129
+ subject = SUBJECT
130
+
131
+ body = BODY
132
+
133
+
134
+
135
+ msg = create_message(FROM_ADDRESS, to_addr, BCC, subject, body)
136
+
163
- send(from_addr, [to_addr], msg)
137
+ send(FROM_ADDRESS, to_addr, msg)
164
138
 
165
139
  ```

1

プログラムコードの一部を編集しました。

2019/10/29 04:26

投稿

raspypy
raspypy

スコア247

test CHANGED
File without changes
test CHANGED
@@ -58,18 +58,6 @@
58
58
 
59
59
 
60
60
 
61
-
62
-
63
- import time
64
-
65
- import sqlite3
66
-
67
- import threading
68
-
69
- import os
70
-
71
-
72
-
73
61
  import os.path
74
62
 
75
63
  import smtplib
@@ -85,10 +73,6 @@
85
73
  from email.MIMEMultipart import MIMEMultipart
86
74
 
87
75
  from email.MIMEText import MIMEText
88
-
89
-
90
-
91
- import datetime
92
76
 
93
77
 
94
78