iOS Question Open default email app

kozbot

Member
Licensed User
Longtime User
Hi guys,

I've tried the

B4X:
App.OpenURL("email")
but I guess I'm not using the right terminology.

I want to send an email using the default app, but I want to be able to specify the subject as well.

Ideas?

TIA
 
Last edited:

kozbot

Member
Licensed User
Longtime User
Thanks for that Erel. Do I put the Queries Schemes in the project attributes? I did try it, but I'm getting an error:

LaunchServices: ERROR: There is no registered handler for URL scheme (null)

Thanks.
 
Upvote 0

kozbot

Member
Licensed User
Longtime User
I'm still getting the same error. I would upload the whole project but it's massive.
Thing is, (I'm gonna use Android terminology) the activity I'm using it on, is not from the front activity... so does that make a difference?
 
Upvote 0

kozbot

Member
Licensed User
Longtime User
So, THIS works:
B4X:
App.OpenURL("mailto:[email protected]")
and it sends to the correct address, but I'm not sure how to put in the subject line. I've tried:
B4X:
App.OpenURL("mailto:[email protected]&Subject:App Enquiry")
And
B4X:
App.OpenURL("mailto:[email protected]"+"Subject:App Enquiry")
And
B4X:
App.OpenURL("mailto:[email protected], Subject:App Enquiry")

But none of them work... I just need to get the subject line in the email. Thanks...
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
The problem is that your subject contains spaces, and you don't encoded it.

Add the iStringUtils library to your project and use the following code:

B4X:
Sub SendEmail(Email As String,Body As String, Subject As String) As Boolean
  
    Dim SU As StringUtils
    Body = SU.EncodeUrl(Body,"UTF-8")
    Subject = SU.EncodeUrl(Subject,"UTF-8")
  
    Dim Url As String = "mailto:" & Email & "?subject=" & Subject & "&body=" & Body
  
    If App.CanOpenURL("mailto:") = False Then
        Return False
   Else
     App.OpenURL(Url)
         Return True
   End If
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use this sub and it will work (add a reference to StringUtils):
B4X:
Private Sub SendEmail(ToField As String, Subject As String) As Boolean
   If App.CanOpenURL("mailto:") = False Then
     Return False
   Else
     Dim su As StringUtils
     App.OpenURL($"mailto:?to=${ToField}&subject=${su.EncodeUrl(Subject, "utf8")}"$)
     Return True
   End If
End Sub
 
Upvote 0
Top