Send an Email from Nios4 script - Insights on management Software

 


One of the last programmable objects inserted in the language of Nios4 allows you to send emails.

In this example we will send a courtesy email with the copy of the order executed to our customer. The script will be inserted in the saving post of the latter.

Check if the email should be sent or not 

if dataview.getvalue("status_order") != "Confermed" or dataview.getvalue("mail_sent") == 1 or dataview.getvalue("email") == "" then
   do return end
end

We first check whether or not to send the email. The conditions must be that the order status must be confirmed, that an email has never been sent and that the email address has been filled in. If any of these conditions is false then we will not proceed with the submission.


Creating the document and the body of the mail to be sent

print_order  = dataview.createdocument("GGUID PRINT ORDER")
body_email     = dataview.createdocument("GGUID PRINT EMAIL ORDER")

At this stage we will create the print of the order to be sent using the createdocument command. Print gguid is recoverable within the editor on Windows.

The second print will create the html body to be used in the mail. This is in effect a print but optimized to be used precisely as the text of the mail. The potential lies in the fact of being able to create a qualitatively and graphically better email reporting the order data.

It is also possible not to create html bodies for the mail but to insert a simple text.

 

Email sending 

email              = program.newemail()
email.username     = "username"
email.password     = "password"
email.smtpserver   = "smtp.server"
email.portserver   = 587
email.enablessl    = true 
email.subject      = "Courtesy copy order n " .. dataview.getvalue("number_order") 
--email.body       = "Body of the mail standard"
email.bodydocument = body_email 
email.sendfrom     = "help@d-one.info"
email.sendto       = dataview.getvalue("email")
email.adddocument(print_order) 
--email.addcc("possile email cc additional")
--email.addcc("possile email cc additional") 
value = email.send()

The first block creates the email object and sets the connection parameters to the smtp server. These data are necessary to send the mail and vary from server to server. It may also be necessary to set the server to receive requests from unknown apps, such as Gmail.

We set the subject of the email and hook the email body previously created. We set our mail to identify who sends it (sendfrom) and retrieve the email in the order (sendto)

We add as an attachment the actual printing of the order and send the email.

 

Last step

if value == true then
   dataview.setvalue("mail_sent",1)
   database.setsql("UPDATE orders SET mail_sent = 1 WHERE gguid='" .. dataview.getvalue("gguid") .. "'"
else
  output.print(email.messageerror)
end

If the value is equal to true it means that the mail has been correctly sent. For this we modify the value of the mail_sent field both on the card and in the database to block the possibility of sending the same email again.

 

 

 

 

 

 

 

 

Comments