Mailto example not working (or better say I am unable to have them work)

Hello everybody.
I need to send a very simple email with an attachment.
I need to have it sent from a webapp residing on Xojo Cloud.
I use xojo 2022. (web 2.0)
I tried the examples in xojo (although I don’t need SMTP) and it doesn’t work.
The fields that I can gather from the page are:
Sender email address
Recipient email address
Copy to email address
Bcc email address
Subject
Message
Attachment (a very small .html file)
Is there a simple way to accomplish this?
Please consider that I am relatively new to xojo.
Had it been PHP, I’d have already solved it with a simple mailto:

Is there a simple way to do this?
To be frank, I have tried already the various examples that I read on this Forum but either I am dumber than the average (very much possible) or there must be something that I did not catch at all.

e.g. if I type ShowURL(mailto: etc) I get an unrecognised item…

Thanks in advance for any help or guidance you’ll be able to submit.
Best,
Carlo

What is the mail server you are going to use?

I do not know about webapps, but the mailtos below work for desktop apps.
BTW, you may first try mailto in a desktopapp and see if at least it works there.
And sometime you may have to use urlencode.
In the snippets, content may refer to different sources like listbox, parameters and the like.
Sorry I cannot find instances of attachments, although I’m sure I used them too.

ShowURL “mailto:somename@gmail.com?subject=mySubject&body=” + label3.text + EndOfLine + me.cell(idx, 0) + EndOfLine + mref
ShowURL “mailto:” + t + “?subject=” + subj + “&body=” + mbody
ShowURL “mailto:” + t + “?cc=” + result
ShowURL “mailto:” + t + “?bcc=” + result

You may need to replace EndOfLine with myMessage.ReplaceAll(EndOfLine, “%0A”) for Web and Windows, but macOS copes with EndOfLine OK.

1 Like

All my Web apps, both Web 1.0 and Web 2.0 send email to various places, some for support as in srrors and some to notify users that tasks have been done. In all cases I have to send email using an SMTP server to forward the email to the recipients domain. This is pretty simple to do with plain text, but there are various things you need to do if you want the email in HTML format. For now you should probably focus on getting text email to work. In my case I can use a local Mail Server to send local email without any server authentication, but I also have another mail setup for an external SMTP service called SendGrid https://sendgrid.com/go/email-brand-signup-sales-1 which does require secure authentication and I have never gotten Xojo’s native email to work with that and I instead use MBS CURL to handle sending email securely.

Here is how I handle email that doesn’t require authentication and can send an attachment.

I have a class called EmailSocket whose Super is SMTPSocket. In that class is a Boolean property called Mail Sent. There are also 3 events with the following code that was something I got from another XJ user years ago. (Note: I use a method called LogExceptions and anther called LogTransactions to log errors and activity to text files. You could use a MessageBox or roll your own logging.)

Sub ServerError(ErrorID as integer, ErrorMessage as string, Email as EmailMessage) Handles ServerError
  LogExceptions("Email Error ID: " + Str(ErrorID))
  LogExceptions("Email Error Message: " + ErrorMessage + EndOfLine + EndOfLine + Email.ToAddress + EndOfLine + EndOfLine + Email.Subject + EndOfLine + EndOfLine + Email.BodyHTML)
  
  MailSent = True
  Me.Disconnect
  
End Sub


Sub MailSent() Handles MailSent
  LogTransactions("Email Send Event Complete")
End Sub

Sub Error(err As RuntimeException) Handles Error
  // we get error 102 for just a regular disconnect
  // but handle others
  If Self.LastErrorCode = 102  Then
    If MailSent = False Then
      
    End If
    MailSent = True
    Me.Disconnect
  Else
    LogExceptions("Email Error: " + Str(Self.LastErrorCode))
    MailSent = True
    Me.Disconnect
  End If
  
End Sub

I also have a method to send the Email; Here is the code for that:

Public Sub SendAttachmentEmail(msg As String, subj As String, msgtype As String, sendtoadd As String, f As FolderItem)
  // sends an email to the specified address
  Var i As Integer
  Var currsec As Date
  Var timeout As Integer
  Var mail as EmailMessage
  Var attfile As EmailAttachment
  Var msgbody,recipients(-1) as string
  Var emailTask As EmailSocket
  
  // make a new message
  mail = new EmailMessage
  
  recipients = Split(sendtoadd,",")
  For i = 0 To UBound(recipients)
    If Not (recipients(i) = "") Then mail.AddRecipient(recipients(i))
  Next
  mail.FromAddress = FromEmailAddress
  mail.Subject = subj
  mail.BodyPlainText = msg
  
  If Not (f = Nil) And f.Exists Then
    attfile = New EmailAttachment
    attfile.ContentEncoding="UTF8"
    attfile.LoadFromFile(f)
    mail.Attachments.Append(attfile)
  End If
  
  emailTask = New EmailSocket
  emailTask.port = 25
  emailTask.address = SMTPServerAddress
  emailTask.messages.Append mail
  emailTask.SendMail
  
  currsec = New Date
  timeout = currsec.Second
  While emailTask.MailSent = False
    emailTask.Poll
    
    currsec = New Date
    If currsec.Second > (timeout + 60) Then
      LogExceptions("The Exception Notification Email timed out and was not sent")
      Exit
    End If
  Wend
  emailTask = NIL
  f.Delete
  
End Sub

Later in the day I will try your code/solution/suggestion and report back the outcome.
Thank you so much for the detailed answer and help…
This Forum is unvaluable…

Grazie, Carlo…
Più tardi provo e poi faccio sapere…

Dear Tom,
I got lost here…
I inserted a new class to the app with its property MailSent boolean.
Could you please explain step by step how to create events and methods required?
I know… I am a real newbie…
Thank you so much,
Carlo

Your suggested code “as is” did not work.
I tried replacing ShowURL with the newer GoToURL but when I try to compile it says:
e,g GoToURL “mailto:” + t + “?subject=” + subj + “&body=” + mbody
mailto: this item does not exist.
??

I fear webapps may not support it.

Anyway, subj and mbody expect a string, so something like this should work (if webapps support GoToURL):
GoToURL “mailto:somename@gmail.com?subject=mySubject&body=myBody"

Once you have created the Class “EmailSocket” and set it’s super to “SMTPSocket”, select the “+” dropdown button and then select “Event Handler…”.

From the “Add Event Handler to EmailSocket” dialog add the events “Error”, “MailSent” and “ServerError” to the class.

Events

Add the code I provided above to the events.

From the main menu select “Insert” and the submenu “Module

Module

Select the “Module1” and give it a meaningful name like “EmailMethods” Then use the “+” dropdown again to add a “Method” to the module.

Give the “Untitled” method a meaningful name like “SendAttachmentEmail” and then put the code I provided above in the method. Be sure to change the line emailTask.address = SMTPServerAddress to actually use your SMTP server name or create a constant or variable with that SMTPserver name and use that in place of SMTPServerAddress. example:

Var SMTPServerAddress As String
SMTPServerAddress = "smtpservername@domainname.net"

–Tom

When I add to, for example, the code you provided to Server error I get the following error:
Code added:
Sub ServerError(ErrorID as integer, ErrorMessage as string, Email as EmailMessage) Handles ServerError
LogExceptions("Email Error ID: " + Str(ErrorID))
LogExceptions("Email Error Message: " + ErrorMessage + EndOfLine + EndOfLine + Email.ToAddress + EndOfLine + EndOfLine + Email.Subject + EndOfLine + EndOfLine + Email.BodyHTML)

MailSent = True
Me.Disconnect
End Sub

I get
Syntax Error
End Sub

You didn’t identify the syntax error so I’m going to have to guess that it is on the LogExceptions. In my original post I stated:

(Note: I use a method called LogExceptions and anther called LogTransactions to log errors and activity to text files. You could use a MessageBox or roll your own logging.)

Try using

MessageBox("Email Error: " + Str(Self.LastErrorCode))

MessageBox("Email Error Message: " + ErrorMessage + EndOfLine + EndOfLine + Email.ToAddress + EndOfLine + EndOfLine + Email.Subject + EndOfLine + EndOfLine + Email.BodyHT

ML)

Instead of LogExceptions or alternately create your own method to log errors to a text file.

All I get, as I said, is a syntax error on End Sub.
I replaced the code with:
Sub ServerError(ErrorID as integer, ErrorMessage as string, Email as EmailMessage) Handles ServerError
MessageBox("Email Error: " + Str(Self.LastErrorCode))

MessageBox("Email Error Message: " + ErrorMessage + EndOfLine + EndOfLine + Email.ToAddress + EndOfLine + EndOfLine + Email.Subject + EndOfLine + EndOfLine + Email.BodyHT
MailSent = True
Me.Disconnect
End Sub

Yet it says Syntax Error End Sub.

Same thing with Error and EmailSent (Event Handlers)

I suspect you are unnecessarily pasting in the Sub and End Sub Code that is part of the Event or Method. You are not interpreting the code correctly. The Events and the Method include Subs. That part of the code is not visible unless you copy the entire event and paste it into a text editor.

Below is what each of the events should look like.

There is already a screen capture above showing what the method in the module should look like.

In the Future It would be helpful if you included screen captures of the errors and code so we can see what you are doing.

Just to make sure you are not confused about the Sub line for the Module Method Here is a screen capture showing circled in red where the Module’s name and variables are typed or pasted.