teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

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

2016/08/22 22:49

投稿

horisuke3232
horisuke3232

スコア109

title CHANGED
File without changes
body CHANGED
@@ -28,14 +28,16 @@
28
28
  from email.MIMEBase import MIMEBase
29
29
  from email.MIMEMultipart import MIMEMultipart
30
30
  from email.MIMEText import MIMEText
31
+
31
32
  #Gmailアカウント
32
- ADDRESS = "Gmailアドレス"
33
+ ADDRESS = "アドレス"
33
- PASSWARD = "Gmailパスワード"
34
+ PASSWARD = "パスワード"
35
+
34
36
  #SMTPサーバの設定(Gmail用)
35
37
  SMTP = "smtp.gmail.com"
36
38
  PORT = 587
37
39
  def create_message(from_addr, to_addr, subject, body, mime=None, attach_file=None):
38
- """
40
+ """
39
41
  メッセージを作成する
40
42
  @:param from_addr 差出人
41
43
  @:param to_addr 宛先
@@ -45,55 +47,71 @@
45
47
  @:param attach_file 添付ファイル
46
48
  @:return メッセージ
47
49
  """
48
- msg = MIMEMultipart()
50
+ msg = MIMEMultipart()
49
- msg["From"] = from_addr
51
+ msg["From"] = from_addr
50
- msg["To"] = to_addr
52
+ msg["To"] = to_addr
51
- msg["Date"] = formatdate()
53
+ msg["Date"] = formatdate()
52
- msg["Subject"] = subject
54
+ msg["Subject"] = subject
53
- body = MIMEText(body)
55
+ body = MIMEText(body)
54
- msg.attach(body)
56
+ msg.attach(body)
57
+
55
58
  # 添付ファイル
56
- if mime != None and attach_file != None:
59
+ if mime != None and attach_file != None:
57
- attachment = MIMEBase(mime['type'],mime['subtype'])
60
+ attachment = MIMEBase(mime['type'],mime['subtype'])
58
- file = open(attach_file['path'])
61
+ file = open(attach_file['path'])
59
- attachment.set_payload(file.read())
62
+ attachment.set_payload(file.read())
60
- file.close()
63
+ file.close()
61
- Encoders.encode_base64(attachment)
64
+ Encoders.encode_base64(attachment)
62
- msg.attach(attachment)
65
+ msg.attach(attachment)
63
- attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
66
+ attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
67
+
64
- return msg
68
+ return msg
69
+
70
+
71
+ if mime != None and attach_file != None:
72
+ attachment = MIMEBase(mime['type'],mime['subtype'])
73
+ file = open(attach_file['path'])
74
+ attachment.set_payload(file.read())
75
+ file.close()
76
+ Encoders.encode_base64(attachment)
77
+ msg.attach(attachment)
78
+ attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
79
+
80
+ return msg
81
+
65
82
  def send(from_addr, to_addrs, msg):
66
- """
83
+ """
67
84
  メールを送信する
68
85
  @:param from_addr 差出人
69
86
  @:param to_addr 宛先(list)
70
87
  @:param msg メッセージ
71
- """
88
+ """
72
- smtpobj = smtplib.SMTP(SMTP, PORT)
89
+ smtpobj = smtplib.SMTP(SMTP, PORT)
73
- smtpobj.ehlo()
90
+ smtpobj.ehlo()
74
- smtpobj.starttls()
91
+ smtpobj.starttls()
75
- smtpobj.ehlo()
92
+ smtpobj.ehlo()
76
- smtpobj.login(ADDRESS, PASSWARD)
93
+ smtpobj.login(ADDRESS, PASSWARD)
77
- smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
94
+ smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
78
- smtpobj.close()
95
+ smtpobj.close()
96
+
79
97
  if __name__ == '__main__':
80
98
  #宛先アドレス
81
- to_addr = "trigger@recipe.ifttt.com"
99
+ to_addr = "アドレス"
82
100
  #件名と本文
83
- subject = "侵入者あり!"
101
+ subject = "侵入者あり!"
84
- body = "たった今侵入者がありました!警戒してください!"
102
+ body = "たった今侵入者がありました!警戒してください!"
85
103
  #添付ファイル設定(text.txtファイルを添付)
86
- mime={'type':'text', 'subtype':'comma-separated-values'}
104
+ mime={'type':'text', 'subtype':'comma-separated-values'}
87
- attach_file={'name':'test.txt', 'path':'./text.txt'}
105
+ attach_file={'name':'test.txt', 'path':'./text.txt'}
88
106
  #メッセージの作成(添付ファイルあり)
89
107
  #msg = create_message(ADDRESS, to_addr, subject, body, mime, attach_file)
90
108
  #メッセージ作成(添付ファイルなし)
91
109
  while True:
92
- inputValue = GPIO.input(25)
110
+ inputValue = GPIO.input(25)
93
111
  if (inputValue == True):
94
- msg = create_message(ADDRESS, to_addr, subject, body)
112
+ msg = create_message(ADDRESS, to_addr, subject, body)
95
- send(ADDRESS, [to_addr], msg)
113
+ send(ADDRESS, [to_addr], msg)
96
- time.sleep(1)
114
+ time.sleep(1)
97
115
  ```
98
116
 
99
117
  何が原因なのでしょうか?