質問編集履歴

1

コード修正 インデントを正しく

2016/08/22 22:49

投稿

horisuke3232
horisuke3232

スコア109

test CHANGED
File without changes
test CHANGED
@@ -58,11 +58,15 @@
58
58
 
59
59
  from email.MIMEText import MIMEText
60
60
 
61
+
62
+
61
63
  #Gmailアカウント
62
64
 
63
- ADDRESS = "Gmailアドレス"
65
+ ADDRESS = "アドレス"
64
-
66
+
65
- PASSWARD = "Gmailパスワード"
67
+ PASSWARD = "パスワード"
68
+
69
+
66
70
 
67
71
  #SMTPサーバの設定(Gmail用)
68
72
 
@@ -72,7 +76,7 @@
72
76
 
73
77
  def create_message(from_addr, to_addr, subject, body, mime=None, attach_file=None):
74
78
 
75
- """
79
+ """
76
80
 
77
81
  メッセージを作成する
78
82
 
@@ -92,43 +96,73 @@
92
96
 
93
97
  """
94
98
 
95
- msg = MIMEMultipart()
99
+ msg = MIMEMultipart()
96
-
100
+
97
- msg["From"] = from_addr
101
+ msg["From"] = from_addr
98
-
102
+
99
- msg["To"] = to_addr
103
+ msg["To"] = to_addr
100
-
104
+
101
- msg["Date"] = formatdate()
105
+ msg["Date"] = formatdate()
102
-
106
+
103
- msg["Subject"] = subject
107
+ msg["Subject"] = subject
104
-
108
+
105
- body = MIMEText(body)
109
+ body = MIMEText(body)
106
-
110
+
107
- msg.attach(body)
111
+ msg.attach(body)
112
+
113
+
108
114
 
109
115
  # 添付ファイル
110
116
 
111
- if mime != None and attach_file != None:
117
+ if mime != None and attach_file != None:
112
-
118
+
113
- attachment = MIMEBase(mime['type'],mime['subtype'])
119
+ attachment = MIMEBase(mime['type'],mime['subtype'])
114
-
120
+
115
- file = open(attach_file['path'])
121
+ file = open(attach_file['path'])
116
-
122
+
117
- attachment.set_payload(file.read())
123
+ attachment.set_payload(file.read())
118
-
124
+
119
- file.close()
125
+ file.close()
120
-
126
+
121
- Encoders.encode_base64(attachment)
127
+ Encoders.encode_base64(attachment)
122
-
128
+
123
- msg.attach(attachment)
129
+ msg.attach(attachment)
124
-
130
+
125
- attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
131
+ attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
126
-
132
+
133
+
134
+
127
- return msg
135
+ return msg
136
+
137
+
138
+
139
+
140
+
141
+ if mime != None and attach_file != None:
142
+
143
+ attachment = MIMEBase(mime['type'],mime['subtype'])
144
+
145
+ file = open(attach_file['path'])
146
+
147
+ attachment.set_payload(file.read())
148
+
149
+ file.close()
150
+
151
+ Encoders.encode_base64(attachment)
152
+
153
+ msg.attach(attachment)
154
+
155
+ attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
156
+
157
+
158
+
159
+ return msg
160
+
161
+
128
162
 
129
163
  def send(from_addr, to_addrs, msg):
130
164
 
131
- """
165
+ """
132
166
 
133
167
  メールを送信する
134
168
 
@@ -138,39 +172,41 @@
138
172
 
139
173
  @:param msg メッセージ
140
174
 
141
- """
175
+ """
142
-
176
+
143
- smtpobj = smtplib.SMTP(SMTP, PORT)
177
+ smtpobj = smtplib.SMTP(SMTP, PORT)
144
-
178
+
145
- smtpobj.ehlo()
179
+ smtpobj.ehlo()
146
-
180
+
147
- smtpobj.starttls()
181
+ smtpobj.starttls()
148
-
182
+
149
- smtpobj.ehlo()
183
+ smtpobj.ehlo()
150
-
184
+
151
- smtpobj.login(ADDRESS, PASSWARD)
185
+ smtpobj.login(ADDRESS, PASSWARD)
152
-
186
+
153
- smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
187
+ smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
154
-
188
+
155
- smtpobj.close()
189
+ smtpobj.close()
190
+
191
+
156
192
 
157
193
  if __name__ == '__main__':
158
194
 
159
195
  #宛先アドレス
160
196
 
161
- to_addr = "trigger@recipe.ifttt.com"
197
+ to_addr = "アドレス"
162
198
 
163
199
  #件名と本文
164
200
 
165
- subject = "侵入者あり!"
201
+ subject = "侵入者あり!"
166
-
202
+
167
- body = "たった今侵入者がありました!警戒してください!"
203
+ body = "たった今侵入者がありました!警戒してください!"
168
204
 
169
205
  #添付ファイル設定(text.txtファイルを添付)
170
206
 
171
- mime={'type':'text', 'subtype':'comma-separated-values'}
207
+ mime={'type':'text', 'subtype':'comma-separated-values'}
172
-
208
+
173
- attach_file={'name':'test.txt', 'path':'./text.txt'}
209
+ attach_file={'name':'test.txt', 'path':'./text.txt'}
174
210
 
175
211
  #メッセージの作成(添付ファイルあり)
176
212
 
@@ -180,15 +216,15 @@
180
216
 
181
217
  while True:
182
218
 
183
- inputValue = GPIO.input(25)
219
+ inputValue = GPIO.input(25)
184
220
 
185
221
  if (inputValue == True):
186
222
 
187
- msg = create_message(ADDRESS, to_addr, subject, body)
223
+ msg = create_message(ADDRESS, to_addr, subject, body)
188
-
224
+
189
- send(ADDRESS, [to_addr], msg)
225
+ send(ADDRESS, [to_addr], msg)
190
-
226
+
191
- time.sleep(1)
227
+ time.sleep(1)
192
228
 
193
229
  ```
194
230