Send custom email to selected phone contact

Hi, I’m trying to send a custom email to the contacts registered in my phone using native.showPopup, but after picking the contact the email popup doesn’t open.

Testing on the Xcode simulator only says that email isn’t supported.

This is my code, can someone help me?

local function inviteContactsEmail( event ) if event.phase=="ended" or event.phase=="cancelled" then local pickContactOptions = { option = "pickContact", hideDetails = true, performDefaultAction = false, filter = { "email" }, listener = onPopupDismiss } native.showPopup( "addressbook", pickContactOptions ) end return true end local function onPopupDismiss( event ) local email="" -- if there is 'event.data', print its key/value pairs if ( event.data ) then for k,v in pairs( event.data ) do if k=="homeEmail" then print( k, ":", v ) email=v end end if email=="" then print("Email doesn't exist") else timer.performWithDelay(1000, sendEmail(email)); --system.openURL( "mailto:"..email.."?subject="..subject.."&body="..body ) end end end local function sendEmail(email) local options = { to = email, subject="Join", body=myApp.userData.username.." Invite you" } native.showPopup("mail", options) end

Hi @gabrielmoralesmagnus,

This is probably an issue in the Lua scope. You are asking the “inviteContactsEmail()” function to use the “onPopupDismiss()” function as its listener, but that “onPopupDismiss()” function is declared as a local function below the function calling it. So, Lua does not recognize what “onPopupDismiss()” is, and it will silently fail. You’re doing the same thing by trying to call “sendEmail()” from a timer within “onPopupDismiss()”, and once again, “sendEmail()” is declared as local below the calling function, so Lua will have no concept of what function it’s looking for.

Scope is such an essential aspect of programming in Lua, I suggest you read the following tutorial carefully and become very familiar with the concept:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Take care,

Brent

thanks for quick response this solved my problem

Hi, again I have a problem in iOS 8.4 my code was working fine but in the new iOS 9 doesn’t work can someone help me?, I checked the docs and it’s the same here is my code.

local function inviteContactsEmail( event ) if event.phase=="ended" or event.phase=="cancelled" then local pickContactOptions = { option = "pickContact", hideDetails = true, performDefaultAction = true, filter = { "email" }, listener = function ( event ) local email="" local subject=translations["About1"][myApp.userPreferences.language] local body=myApp.userData.username..translations["InviteToDownload"][myApp.userPreferences.language] if ( event.data ) then for k,v in pairs( event.data ) do if k=="homeEmail" then print( k, ":", v ) email=v end end if email=="" then timer.performWithDelay(2000, function( ) native.showAlert( "Contact", translations["contact\_no\_email"][myApp.userPreferences.language], {"OK"}) end); else timer.performWithDelay(2000, function( ) local options = { to = email, subject =translations["About1"][myApp.userPreferences.language], body=myApp.userData.username..translations["InviteToDownload"][myApp.userPreferences.language], } native.showPopup("mail", options) end); end end end } native.showPopup( "addressbook", pickContactOptions ) end return true end

Hi @gabrielmoralesmagnus,

Can you be more specific about what doesn’t work?

Thanks,

Brent

Thanks for reply, this is the problem when the contact address book show and you click in a contact then crash the app, after several test in a device(even comment all code in the onDismiss listener) I found this error on the device logs

“contact and its snapshot should both be non-unified.”

 

in the iOS forum I found a solution to this error in iOS 9 beta

https://forums.developer.apple.com/thread/11354

 

it looks like it’s a permission error and the new iOS 9 require permission for address book explicit

Hi @gabrielmoralesmagnus,

This is probably an issue in the Lua scope. You are asking the “inviteContactsEmail()” function to use the “onPopupDismiss()” function as its listener, but that “onPopupDismiss()” function is declared as a local function below the function calling it. So, Lua does not recognize what “onPopupDismiss()” is, and it will silently fail. You’re doing the same thing by trying to call “sendEmail()” from a timer within “onPopupDismiss()”, and once again, “sendEmail()” is declared as local below the calling function, so Lua will have no concept of what function it’s looking for.

Scope is such an essential aspect of programming in Lua, I suggest you read the following tutorial carefully and become very familiar with the concept:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Take care,

Brent

thanks for quick response this solved my problem

Hi, again I have a problem in iOS 8.4 my code was working fine but in the new iOS 9 doesn’t work can someone help me?, I checked the docs and it’s the same here is my code.

local function inviteContactsEmail( event ) if event.phase=="ended" or event.phase=="cancelled" then local pickContactOptions = { option = "pickContact", hideDetails = true, performDefaultAction = true, filter = { "email" }, listener = function ( event ) local email="" local subject=translations["About1"][myApp.userPreferences.language] local body=myApp.userData.username..translations["InviteToDownload"][myApp.userPreferences.language] if ( event.data ) then for k,v in pairs( event.data ) do if k=="homeEmail" then print( k, ":", v ) email=v end end if email=="" then timer.performWithDelay(2000, function( ) native.showAlert( "Contact", translations["contact\_no\_email"][myApp.userPreferences.language], {"OK"}) end); else timer.performWithDelay(2000, function( ) local options = { to = email, subject =translations["About1"][myApp.userPreferences.language], body=myApp.userData.username..translations["InviteToDownload"][myApp.userPreferences.language], } native.showPopup("mail", options) end); end end end } native.showPopup( "addressbook", pickContactOptions ) end return true end

Hi @gabrielmoralesmagnus,

Can you be more specific about what doesn’t work?

Thanks,

Brent

Thanks for reply, this is the problem when the contact address book show and you click in a contact then crash the app, after several test in a device(even comment all code in the onDismiss listener) I found this error on the device logs

“contact and its snapshot should both be non-unified.”

 

in the iOS forum I found a solution to this error in iOS 9 beta

https://forums.developer.apple.com/thread/11354

 

it looks like it’s a permission error and the new iOS 9 require permission for address book explicit