Jumat, 02 Desember 2016

Tutorial NodeMCU: Mengirim email pada NodeMCU Lua via smtp.gmail.com dengan fitur Less secure apps (ESP8266, WeMos, NodeMCU, Lua)

Tulisan ini saya buat setelah melakukan browsing beberapa solusi pengiriman email melalui smtp.gmail.com, dan akhirnya menemukan kode berikut ini yang berhasil mengirim email, dengan catatan bahwa anda perlu menurunkan tingkat keamanan pada Google Account anda (Less secure apps) sebagai suatu fitur yang disediakan oleh Google untuk kompatibilitas dengan software client legacy yang dapat menyebabkan kelemahan pada account anda.

Sebelum anda mencoba koding berikut ini, maka anda perlu melakukan kustomisasi terhadap firmware NodeMCU dengan memasukan beberapa module sebagai berikut yang ditandai dengan warna merah.
NodeMCU custom build by frightanic.com
    branch: master
    commit: ec265a6c21db22640795f190bdcb8a4f014cdced
    SSL: true
    modules: crypto,dht,file,gpio,http,net,node,rtctime,sntp,tmr,uart,wifi
 build     built on: 2016-12-02 13:05
 powered by Lua 5.1.4 on SDK 1.5.4.1(39cb9a32)
Kemudian anda perlu menurunkan tingkat keamanan account Google anda (Less secure apps) yang dapat diakses melalui link.

Gambar 1. Mengaktifkan Less secure apps

Catatan:
Fitur Less secure apps disediakan oleh Google untuk memungkinkan akses ke GMail melalui aplikasi legacy yang berpotensi terhadap serangan Man in the Middle Attack, karena aplikasi klien tidak memeriksa sertifikat keamanan.

Adapun koding yang dapat digunakan untuk pengiriman email melalui stmp.gmail.com adalah sebagai berikut ini:
 -- Modifications for GMAIL by Andreas "Andy" Reischle: www.AReResearch.net 
 -- See https://support.google.com/a/answer/176600?hl=de for details on smtp with gmail 
 -- Now that NodeMCU has working SSL support, we can also talk to email services that 
 -- require encryption.  
 -- Caveat: I have not looked into the SSL implementation, but I suspect it is vulnerable 
 -- to man-in-the-middle attacks as the client doesn't check the server's certificate. 
 -- 20160415 ARe 
 --------Original Credits: 
 -------- 
 ------- Working Example: https://www.youtube.com/watch?v=CcRbFIJ8aeU 
 ------- @description a basic SMTP email example. You must use an account which can provide unencrypted authenticated access. 
 ------- This example was tested with an AOL and Time Warner email accounts. GMail does not offer unecrypted authenticated access. 
 ------- To obtain your email's SMTP server and port simply Google it e.g. [my email domain] SMTP settings 
 ------- For example for timewarner you'll get to this page http://www.timewarnercable.com/en/support/faqs/faqs-internet/e-mailacco/incoming-outgoing-server-addresses.html 
 ------- To Learn more about SMTP email visit: 
 ------- SMTP Commands Reference - http://www.samlogic.net/articles/smtp-commands-reference.htm 
 ------- See "SMTP transport example" in this page http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol 
 ------- @author Miguel 
 --no longer required because it is part of the crypto module: require("base64") 
 -- The email and password from the account you want to send emails from 
 local MY_EMAIL = "*******@gmail.com" 
 local EMAIL_PASSWORD = "e********" 
 -- The SMTP server and port of your email provider. 
 -- If you don't know it google [my email provider] SMTP settings 
 local SMTP_SERVER = "smtp.gmail.com" 
 local SMTP_PORT = "465" 
 -- The account you want to send email to 
 local mail_to = "hendra-**@*****.co.id" 
 -- These are global variables. Don't change their values 
 -- they will be changed in the functions below 
 local email_subject = "" 
 local email_body = "" 
 local count = 0 
 local smtp_socket = nil -- will be used as socket to email server 
 -- The display() function will be used to print the SMTP server's response 
 function display(sck,response) 
    print("Got a response: ") 
    print(response) 
 end 
 -- The do_next() function is used to send the SMTP commands to the SMTP server in the required sequence. 
 -- I was going to use socket callbacks but the code would not run callbacks after the first 3. 
 function do_next() 
       if(count == 0)then 

         local IP_ADDRESS = wifi.sta.getip() 
         print ("Send my IP: " .. IP_ADDRESS) 
         smtp_socket:send("HELO "..IP_ADDRESS.."\r\n") 
       elseif(count==1) then 

         smtp_socket:send("AUTH LOGIN\r\n") 
       elseif(count == 2) then 

         smtp_socket:send(crypto.toBase64(MY_EMAIL).."\r\n") 
       elseif(count == 3) then 

         smtp_socket:send(crypto.toBase64(EMAIL_PASSWORD).."\r\n") 
       elseif(count==4) then 

         smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n") 
       elseif(count==5) then 

         smtp_socket:send("RCPT TO:<" .. mail_to ..">\r\n") 
       elseif(count==6) then 

         smtp_socket:send("DATA\r\n") 
       elseif(count==7) then 

         local message = string.gsub( 
         "From: \"".. MY_EMAIL .."\"<"..MY_EMAIL..">\r\n" .. 
         "To: \"".. mail_to .. "\"<".. mail_to..">\r\n".. 
         "Subject: ".. email_subject .. "\r\n\r\n" .. 
         email_body,"\r\n.\r\n","") 
         smtp_socket:send(message.."\r\n.\r\n") 
       elseif(count==8) then 

          tmr.stop(0) 
          smtp_socket:send("QUIT\r\n") 
       else 
         smtp_socket:close() 
       end
       count=count+1

 end 
 -- The connectted() function is executed when the SMTP socket is connected to the SMTP server. 
 -- This function will create a timer to call the do_next function which will send the SMTP commands 
 -- in sequence, one by one, every 5000 seconds.  
 -- You can change the time to be smaller if that works for you, I used 5000ms just because. 
 function connected(sck) 
   print("Connected - Starting Timer") 
   tmr.alarm(0,5000,1,do_next) 
 end 
 -- @name send_email 
 -- @description Will initiated a socket connection to the SMTP server and trigger the connected() function 
 -- @param subject The email's subject 
 -- @param body The email's body 
 function send_email(subject,body) 
    count = 0 
    email_subject = subject 
    email_body = body 
    print ("Open Connection") 
    smtp_socket = net.createConnection(net.TCP,1) 
    smtp_socket:on("connection",connected) 
    smtp_socket:on("receive",display) 
    smtp_socket:connect(SMTP_PORT,SMTP_SERVER) 
 end 
 -- Send an email 
 print ("Sending started...") 
 send_email("ESP8266-GMailSender","Hi there!") 
Jika anda mencoba menjalankan aplikasi diatas, tanpa melakukan setting Less secure apps, maka Google akan mengirimkan email pemberitahuan bahwa adanya upaya Sign-in ke account anda dengan kondisi Less secure apps.

  Gambar 2. Pemberitahuan Upaya Sign-in Less secure apps

Maka anda dapat melakukan klik pada allowing access to less secure apps sesuai dengan kondisi yang disyaratkan oleh contoh diatas.

Are you the one who tried signing in?
Google will continue to block sign-in attempts from the app you're using because it has known security problems or is out of date. You can continue to use this app by allowing access to less secure apps, but this may leave your account vulnerable.

Jika setting Less secure app berhasil diaktifkan, maka akan dikirimkan email konfirmasi sebagai berikut:

 Gambar 3. Pemberitahuan pengaktifan Less secure apps

Hasil output aplikasi kalau email berhasil dikirim:

Open Connection
Connected - Starting Timer
Got a response:
220 smtp.gmail.com ESMTP t184sm10153815pgt.36 - gsmtp

Send my IP: 172.21.12.106
Got a response:
250 smtp.gmail.com at your service

Got a response:
334 VXNlcm5hbWU6

Got a response:
334 UGFzc3dvcmQ6

Got a response:
235 2.7.0 Accepted

Got a response:
250 2.1.0 OK t184sm10153815pgt.36 - gsmtp

Got a response:
250 2.1.5 OK t184sm10153815pgt.36 - gsmtp

Got a response:
354  Go ahead t184sm10153815pgt.36 - gsmtp

Got a response:
250 2.0.0 OK 1480722979 t184sm10153815pgt.36 - gsmtp

Got a response:
221 2.0.0 closing connection t184sm10153815pgt.36 - gsmtp
Hasil Output kalau aplikasi dijalankan tetapi setting Less secure apps belum dilakukan:
Open Connection
Connected - Starting Timer
Got a response:
220 smtp.gmail.com ESMTP f132sm10155943pfa.72 - gsmtp

Send my IP: 172.21.12.106
Got a response:
250 smtp.gmail.com at your service

Got a response:
334 VXNlcm5hbWU6

Got a response:
334 UGFzc3dvcmQ6

Got a response:
534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsC
534-5.7.14 tAI07EFTWQ94dXI6x41TyfjIFDp_NIYPEzYsY3XzS04a7E3blJJsFKjG73SKKcBX0dyrSX
534-5.7.14 2JHBhmdVqZJjGZVUMOmHoEKaLbxvktU8REWZrutoRNO-WU3a1VZ2r0jl49OWir6nt-stsf
534-5.7.14 zMidIGW5QGmbdlK3bnQRUflcL3XlCTL0M2MjM5os1xQJVeZuG3Fsd65l-zc9T364YtYMVk
534-5.7.14 2N1LRsEQoIYeXU4IxmNOmC_G-jzwk> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 f132sm10155943pfa.72 - gsmtp

Got a response:
530-5.5.1 Authentication Required. Learn more at
530 5.5.1  https://support.google.com/mail/?p=WantAuthError f132sm10155943pfa.72 - gsmtp

Got a response:
530-5.5.1 Authentication Required. Learn more at
530 5.5.1  https://support.google.com/mail/?p=WantAuthError f132sm10155943pfa.72 - gsmtp

Got a response:
530-5.5.1 Authentication Required. Learn more at
530 5.5.1  https://support.google.com/mail/?p=WantAuthError f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
221 2.0.0 closing connection f132sm10155943pfa.72 - gsmtp
 Hasil Output kalau aplikasi dijalankan tetapi username atau password salah:
Open Connection
Connected - Starting Timer
Got a response:
220 smtp.gmail.com ESMTP f132sm10155943pfa.72 - gsmtp

Send my IP: 172.21.12.106
Got a response:
250 smtp.gmail.com at your service

Got a response:
334 VXNlcm5hbWU6

Got a response:
334 UGFzc3dvcmQ6

Got a response:
534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsC
534-5.7.14 tAI07EFTWQ94dXI6x41TyfjIFDp_NIYPEzYsY3XzS04a7E3blJJsFKjG73SKKcBX0dyrSX
534-5.7.14 2JHBhmdVqZJjGZVUMOmHoEKaLbxvktU8REWZrutoRNO-WU3a1VZ2r0jl49OWir6nt-stsf
534-5.7.14 zMidIGW5QGmbdlK3bnQRUflcL3XlCTL0M2MjM5os1xQJVeZuG3Fsd65l-zc9T364YtYMVk
534-5.7.14 2N1LRsEQoIYeXU4IxmNOmC_G-jzwk> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 f132sm10155943pfa.72 - gsmtp

Got a response:
530-5.5.1 Authentication Required. Learn more at
530 5.5.1  https://support.google.com/mail/?p=WantAuthError f132sm10155943pfa.72 - gsmtp

Got a response:
530-5.5.1 Authentication Required. Learn more at
530 5.5.1  https://support.google.com/mail/?p=WantAuthError f132sm10155943pfa.72 - gsmtp

Got a response:
530-5.5.1 Authentication Required. Learn more at
530 5.5.1  https://support.google.com/mail/?p=WantAuthError f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
502 5.5.1 Unrecognized command. f132sm10155943pfa.72 - gsmtp

Got a response:
221 2.0.0 closing connection f132sm10155943pfa.72 - gsmtp

Tidak ada komentar:

Posting Komentar