Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to send an email in lua?
Started by fazilreyaz Jun 09 2018 11:53 AM

1 reply to this topic
email form smtp sockets textfield

Best Answer fazilreyaz , 11 June 2018 - 01:35 PM

I did it with network.request. 

 

Sharing my code. I hope it will help someone.

 

LUA FILE

local alert
local emailParams = {}
local function emailNetworkListener( event )
        if ( event.isError ) then
            alert = native.showAlert ( "Error Occurred", "Some error has occurred", {"Ok"})  
        else
         
                if(string.find(event.response, "OK")) then
                    txtName.text = "";
                    txtPhone.text = "";
                    txtEmail.text = "";
                    txtMessage.text = "";                 
                    alert = native.showAlert ( "Mail Sent", "Your Query Has Been Sent", {"Ok"})            
                 else           
                    alert = native.showAlert ( "Error Occurred", "Something Went Wrong. Try Again!", {"Ok"}) 
                 end
        end

return true 
end


function sendMailFunc()

emailParams.body = "name="..txtName.text.."&phone="..txtPhone.text.."&email=" .. txtEmail.text .. "&message="..txtMessage.text.."&securityValue=*************"
network.request("http://mywebsite.com/sendEmail.aspx", "POST", emailNetworkListener, emailParams)          

return true
end

ASPX FILE

//On Page Load

NameValueCollection collectData = Request.Form;

if (!string.IsNullOrEmpty(collectData["securityValue"]) && collectData["securityValue"] == "******")
{
      //Here you can get all the values
      string name = collectData["name"];
      string phone = collectData["phone"];

      //sendEmail is a user defined function.
      string msg = ""; 
      msg = sendEmail(collectData["email"], name, phone);
      
      if (msg == "OK") 
          Response.Write("OK");
      else
         Response.Write("ERROR");

}

That's how i achieved this task.

 

 

 

Happy Coding...  :)

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

fazilreyaz

[GLOBAL: userInfoPane.html]
fazilreyaz
  • Enthusiast

  • 60 posts
  • Corona SDK

Hello everyone,

 

I am sending an email through my app but the error happens.

 

Error Message:

Mail send failed                  timeout 

 

Here is my code:

local smtp = require 'socket.smtp'

local emailaddress = "me@fazilmir.com"

function sendMessage(subject, body)
    local msg = {
        headers = {
            to = emailaddress,
            subject = subject
        },
        body = body
    }


    local ok, err = smtp.send {
        from = emailaddress,
        rcpt = emailaddress,
        source = smtp.message(msg),
        user = emailaddress,
        password = '********',
        server = 'fazilmir.com',
        port = 25
    }
    if not ok then
        print("Mail send failed", err)
    end
end

function sendMailFunc()
   if isFormValidated then
      sendMessage(txtName.text.. " Sent A Query", "Some Message")
   end
   return true
end

Server and port number are also correct because I already have used these settings with outlook.

 

 

Please any help would be highly appreciated.

 

I am also attaching a screenshot.

 

 

 

Thanks,

 

 

Attached Files



[TOPIC: post.html]
#2

fazilreyaz

[GLOBAL: userInfoPane.html]
fazilreyaz
  • Enthusiast

  • 60 posts
  • Corona SDK

  Best Answer

I did it with network.request. 

 

Sharing my code. I hope it will help someone.

 

LUA FILE

local alert
local emailParams = {}
local function emailNetworkListener( event )
        if ( event.isError ) then
            alert = native.showAlert ( "Error Occurred", "Some error has occurred", {"Ok"})  
        else
         
                if(string.find(event.response, "OK")) then
                    txtName.text = "";
                    txtPhone.text = "";
                    txtEmail.text = "";
                    txtMessage.text = "";                 
                    alert = native.showAlert ( "Mail Sent", "Your Query Has Been Sent", {"Ok"})            
                 else           
                    alert = native.showAlert ( "Error Occurred", "Something Went Wrong. Try Again!", {"Ok"}) 
                 end
        end

return true 
end


function sendMailFunc()

emailParams.body = "name="..txtName.text.."&phone="..txtPhone.text.."&email=" .. txtEmail.text .. "&message="..txtMessage.text.."&securityValue=*************"
network.request("http://mywebsite.com/sendEmail.aspx", "POST", emailNetworkListener, emailParams)          

return true
end

ASPX FILE

//On Page Load

NameValueCollection collectData = Request.Form;

if (!string.IsNullOrEmpty(collectData["securityValue"]) && collectData["securityValue"] == "******")
{
      //Here you can get all the values
      string name = collectData["name"];
      string phone = collectData["phone"];

      //sendEmail is a user defined function.
      string msg = ""; 
      msg = sendEmail(collectData["email"], name, phone);
      
      if (msg == "OK") 
          Response.Write("OK");
      else
         Response.Write("ERROR");

}

That's how i achieved this task.

 

 

 

Happy Coding...  :)


  • roaminggamer likes this


[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: email, form, smtp, sockets, textfield