NodeMCU SMTP Email - email

I'm working with NodeMCU (docs here http://nodemcu.readthedocs.io/) trying to send an email through SMTP. I got this script off the net and everything seems to be working okay as there are no errors but I don't see any emails in my box, so something must be going wrong. Using the display function as the send callback prints nil unfortunately.
I was able to send smtp email through simple curl so I know that google will accept smtp requests through command line and these settings are correct. Also according to this thread it is possible, they are doing the same thing sending raw strings to gmail's smtp service (http://www.esp8266.com/viewtopic.php?f=24&t=1231&start=8).
-- The email and password from the account you want to send emails from
MY_EMAIL = "REDACTED"
EMAIL_PASSWORD = "REDACTED"
-- The SMTP server and port of your email provider.
-- If you don't know it google [my email provider] SMTP settings
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = "465"
-- The account you want to send email to
mail_to = "REDACTED"
-- Your access point's SSID and password
SSID = "REDACTED"
SSID_PASSWORD = "REDACTED"
-- configure ESP as a station
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
wifi.sta.autoconnect(1)
email_subject = ""
email_body = ""
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(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
count = count+1
IP_ADDRESS = wifi.sta.getip()
smtp_socket:send("HELO "..IP_ADDRESS.."\r\n")
elseif(count==1) then
count = count+1
smtp_socket:send("AUTH LOGIN\r\n")
elseif(count == 2) then
count = count + 1
smtp_socket:send("REDACTED".."\r\n")
elseif(count == 3) then
count = count + 1
smtp_socket:send("REDACTED".."\r\n")
elseif(count==4) then
count = count+1
smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n")
elseif(count==5) then
count = count+1
smtp_socket:send("RCPT TO:<" .. mail_to ..">\r\n")
elseif(count==6) then
count = count+1
smtp_socket:send("DATA\r\n")
elseif(count==7) then
count = count+1
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
count = count+1
tmr.stop(0)
smtp_socket:send("QUIT\r\n")
print("msg sent")
else
smtp_socket:close()
end
print(count)
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)
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
smtp_socket = net.createConnection(net.TCP,0)
smtp_socket:on("connection",connected)
smtp_socket:on("receive",display)
smtp_socket:connect(SMTP_PORT, SMTP_SERVER)
end
-- Send an email
send_email("ESP8266", "[[Hi, How are your IoT projects coming along? Best Wishes,ESP8266]]")
I am available to answer questions regarding this.

A couple of points:
The old example from esp8266.com you mentioned uses consecutive socket:send calls which doesn't work anymore in recent firmwares, see http://nodemcu.readthedocs.io/en/dev/en/modules/net/#netsocketsend. It was a "bug" in the SDK this ever worked.
SMTP port 465 usually implies SSL/TLS connections. You need a firmware that supports that. Otherwise use port 25.
There's an example right in the NodeMCU repo at https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/email/send_email_smtp.lua
I assume you did replace the placeholder at do_next count 2/3 with the base64 encoded username and password? Contrary to the example script which requires a Lua encoder module you may want to use the corresponding NodeMCU module, see http://nodemcu.readthedocs.io/en/dev/en/modules/encoder/#encodertobase64.
I took your script, a recent NodeMCU firmware, changed port to 25, added base64 encode user/pw and was able to successfully deliver an email to myself.
Update 2016-07-05
Full script including WiFi init.
MY_EMAIL = "myself#my-TLD.com"
MY_EMAIL_B64 = "base64-encoded-email"
EMAIL_PASSWORD_B64 = "base64-encoded-password"
SMTP_SERVER = "my-ISPs-server"
SMTP_PORT = 25
mail_to = "myself#my-TLD.com"
email_subject = ""
email_body = ""
count = 0
smtp_socket = nil
function display(sck,response)
print(response)
end
function do_next()
if(count == 0)then
count = count+1
local IP_ADDRESS = wifi.sta.getip()
smtp_socket:send("HELO "..IP_ADDRESS.."\r\n")
elseif(count==1) then
count = count+1
smtp_socket:send("AUTH LOGIN\r\n")
elseif(count == 2) then
count = count + 1
smtp_socket:send(MY_EMAIL_B64.."\r\n")
elseif(count == 3) then
count = count + 1
smtp_socket:send(EMAIL_PASSWORD_B64.."\r\n")
elseif(count==4) then
count = count+1
smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n")
elseif(count==5) then
count = count+1
smtp_socket:send("RCPT TO:<" .. mail_to ..">\r\n")
elseif(count==6) then
count = count+1
smtp_socket:send("DATA\r\n")
elseif(count==7) then
count = count+1
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
count = count+1
tmr.stop(0)
smtp_socket:send("QUIT\r\n")
else
smtp_socket:close()
end
end
function connected(sck)
tmr.alarm(0,3000,1,do_next)
end
function send_email(subject,body)
count = 0
email_subject = subject
email_body = body
smtp_socket = net.createConnection(net.TCP,0)
smtp_socket:on("connection",connected)
smtp_socket:on("receive",display)
smtp_socket:connect(SMTP_PORT,SMTP_SERVER)
end
SSID = "my-SSID"
SSID_PASSWORD = "my-WiFi-password"
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
send_email(
"ESP8266",
[[Hi,
How are your IoT projects coming along?
Best Wishes,
ESP8266]])
end
end)

Related

Text Emailing using VBscript

I have tried the following code, No errors thrown. and no emails have been sent either.
Dim i, objEmail
' Use custom error handling
On Error Resume Next
Set objEmail = CreateObject( "CDO.Message" )
' Fill in the field values
With objEmail
.From = "xyz#abc.com"
.To = "pqr#abc.com"
.Subject = "Test"
.TextBody = "Email from VB Script"
If mySMTPPort = "" Then
mySMTPPort = 25
End If
With .Configuration.Fields
.Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ) = 2
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ) = "HostNameHere"
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = 25
.Update
End With
.Send
End With
' Return status message
If Err Then
EMail = "ERROR " & Err.Number & ": " & Err.Description
Err.Clear
Else
EMail = "Message sent ok"
End If
' Release the e-mail message object
Set objEmail = Nothing
When I execute this VBS file, It just does nothing at all. Please help me out. I have to send simple text emails from my domain to another domain. Or is there a work around for the task i want to perform?
You're probably getting an error when trying to send mail. However, you have error handling enabled (On Error Resume Next), but your error handler just puts the error information in a variable without actually doing anything with that variable. Adding a line that echoes the variable should provide you with more information about what's going on:
If Err Then
EMail = "ERROR " & Err.Number & ": " & Err.Description
Err.Clear
Else
EMail = "Message sent ok"
End If
WScript.Echo EMail
should provide you with more information about what's going on.
Edit: Apparently you're getting a connection error. There are several possible causes for that. First check that your name resolution works correctly:
nslookup HostNameHere
If the name doesn't resolve either use the IP address in your script or get the name resolution fixed.
Next check if you can connect to port 25 on the remote host, both via name and IP address:
telnet HostNameHere 25
telnet a.b.c.d 25
If you get a result like this:
C:\>telnet HostNameHere 25
Connecting To HostNameHere...Could not open connection to the host, on port 25:
Connect failed
something is blocking your connection. Could be a network firewall, a host-based firewall on the remote host, or the service not listening on port 25 in the first place. Check with your network admins and/or the admin(s) of the mail server.
Your code hides errors. err default property is number. So You say if err.number = 0 (same as false) then error, if err.number not 0 (ie error) everything fine.

Send email using CDO no longer working

since last week, I am no longer able to send email via gmail's smtp server. I get the error " -2147220973 the transport failed to connect to the server" whenever I try to send email. However, if I try using my another network (ex. my mobile broadband) it works.
Below is my code for sending email.
Set cdomsg = CreateObject("CDO.Message")
Set cdoconf = CreateObject("CDO.Configuration")
cdoconf.Load -1 ' CDO Source Defaults
Set cdoFields = cdoconf.Fields
With cdoFields
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "example#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "abc"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Update
End With
What is the possible problem? Thank you.

CDO.Message delivery notification failed

Here is my environment: Windows Server 2003, Microsoft Access 2003, Microsoft VB 6.5
Trying to send email from Access using CDO.Message. Here is the portion of my code:
Private Sub btnTestEmail_Click()
On Error GoTo SendMail_Error:
Dim Mailmsg As Object
Dim mailconf As Object
Dim McFields As Object
Dim strSchemas As String
Set Mailmsg = CreateObject("CDO.Message")
Set mailconf = CreateObject("CDO.Configuration")
Set McFields = mailconf.Fields
strSchemas = "http://schemas.microsoft.com/cdo/configuration/"
With McFields
.Item(strSchemas & "sendusing") = 2
.Item(strSchemas & "smtpserver") = "smtp.gmail.com"
.Item(strSchemas & "smtpserverport") = 465
.Item(strSchemas & "smtpauthenticate") = 1
.Item(strSchemas & "sendusername") = "my_email#gmail.com"
.Item(strSchemas & "sendpassword") = "my_gmail_password"
.Item(strSchemas & "smtpconnectiontimeout") = 60
.Item(strSchemas & "smtpusessl") = 1
.Update
End With
Set Mailmsg.Configuration = mailconf
With Mailmsg
.TextBody = "Test email body text"
.Subject = "Test email subject"
.To = "target_email#gmail.com"
.from = "my_email#gmail.com"
'.AddAttachment "D:\test.pdf"
.Fields("urn:schemas:mailheader:disposition-notification-to") = "my_email#gmail.com"
.Fields("urn:schemas:mailheader:return-receipt-to") = "my_email#gmail.com"
' Set delivery status notification (DSN)
' Name Value Description
' cdoDSNDefault 0 No DSN commands are issued.
' cdoDSNNever 1 No DSN commands are issued.
' cdoDSNFailure 2 Return a DSN if delivery fails.
' cdoDSNSuccess 4 Return a DSN if delivery succeeds.
' cdoDSNDelay 8 Return a DSN if delivery is delayed.
' cdoDSNSuccessFailOrDelay 14 Return a DSN if delivery succeeds, fails, or is
.DSNOptions = 0
.Fields.Update
.Send
End With
MsgBox "Message Sent", vbOKOnly
Set Mailmsg = Nothing
Set mailconf = Nothing
Set McFields = Nothing
Exit Sub
SendMail_Error:
MsgBox Err.Description, vbOKOnly
End Sub
With DSNOptions = 0 works great but I would like to get delivery notification (not read notification).
If I set DSNOptions to any allowed non-zero value the email doesn't even arrives to the target email and I don't get any notification to my email.
Strange thing if I set unexisting target email (on purpose) I get delivery unsuccessful notification even with DSNOptions = 0.
Am I missing something in the code? Found on multiple other web site people claims this code works but using other smtp servers. Any help appreciated.

to Sending Email from an ASP classic Page with credentials

I need to send users credentials by email to the users of my app, that is build in asp
classic, this is my issue, from a email account, sent information to an user, but
doesn´t send username and pass from that user, my code is this, how can manage the
user/password? what the best way to do it? thanks in advance.
t1 = time()
estado=""
email="soporte.web#ipsos.com"
mail_from = "Soporte <soporte.web#myenterprise.com>"
mail_destino = "Soporte Web Ipsos" & " <" & email &">"
mail_asunto = "Acceso BCI Satisfaccion " & user
ruta=request.ServerVariables("APPL_PHYSICAL_PATH")
on error resume next
mail_cc=""
Dim myMail
Set myMail = CreateObject("CDONTS.NewMail")
myMail.MailFormat = 0 '0 (Mime format), 1 (default Plain Text format)'
myMail.BodyFormat = 0 '0 html, 1 texto'
myMail.Importance = 1 '0 Low, 1 Normal, 2 High '
myMail.From = mail_from
myMail.To = mail_destino
myMail.Cc = mail_cc
'myMail.Cco = mail_cco'
myMail.Subject = mail_asunto
myMail.Body = texto_email
'myMail.AttachFile ruta&"Carta2006.pdf"'
myMail.Send
Set myMail = Nothing
If err.num <> 0 Then
Response.Write nombre & " / " & email & " / " & "CDONTS Error: " & err.num & " - " &
err.description
estado="no"
End If
end sub
In classic ASP there is no standard way (that I can remember) to deal with username/password, other than a using login page for authentication and session variables for maintaining state.
If you go to the code of the login page in your application and follow the logic you should see the login happen (where username and password are checked). You can then pop those values into a session variable (if this isn't done already) and pull them out later to send in the email.
As an aside, sending a user their username and password in a plain text email is generally discouraged as the email can be read in transit = an opportunity for hackers. But that's up to you...

Looking for mailing solution

I'm using ASP code and AspEmail component to send emails to our clinets, but I have some problmes...
I have more then 1000 email address that I need to send them an email, becuase of my SMTP provider limitation, I can't add them all as BCC in one email but I need to send each email seperatly, therefor looping on +1000 times witch takes forever and fires the server timeout error.
I need to send those emails about 20 times a day.
This is my script:
on error resume next
msg = "SOME TEXT HERE"
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "SMPT.HOST.ADDRESS"
Mail.Port = 25
Mail.CharSet = "UTF-8"
Mail.ContentTransferEncoding = "Quoted-Printable"
Mail.From = "noreply#mydomain.co.il"
MailSubject = "email subject"
Mail.Subject = Mail.EncodeHeader(MailSubject, "utf-8")
Mail.Body = msg
Mail.IsHTML = True
zBcc = split(zBcc, ";") '1000 emails here
for i=0 to Ubound(zBcc)
zBcc(i) = trim(zBcc(i))
if len(zBcc(i))>0 then
if inStr(zBcc(i), " ")>0 then
else
if (Mail.ValidateAddress(zBcc(i)) = 0) then
Mail.Reset()
Mail.AddAddress zBcc(i)
Mail.Send
end if
end if
end if
next
set Mail=nothing
why dont you do it using a pagination type logic i.e.
limit by 100,
loop thru that batch,
once that has completed,
reload the page with the next offset in mind like send-email.asp?offset=100, send-email.asp?offset=200, etc.
use that offset value to get next batch
repeat process until end of recordset.
At least you have less chance of it timing out altho you can increase it: server.ScriptTimeout = 180
First of all I'd maximize the number of BCC's per cycle. Let's say you can email 50 BCC's in one go; you should: especially when you need this page about 20 times a day.
Anyway. Before you start; maximize the scripttimeout
Server.ScriptTimeout = 2147483647