any example how to send mail using Apple Mail ?

hi to all,

I know I can send emails from within xojo with emailmessage and smtpsockets
but I would like to review the outgoing mail before sending it
so I would like to generate an email and view it in apple mail before sending it
I’m supposed to use applescript
is there any example with RS/Xojo code to do that available that I could use ?

thanks.

Maybe try ShowURL with the “mailto:” URI: https://en.wikipedia.org/wiki/Mailto

First, google to find an Applescript for creating an email in Applescript.
Test the Applescript in Apple’s Script Editor.
Once that works, save the applescript to your project folder and then drag that applescript file into your Xojo project. Now you can simply call that Applescript from Xojo by using its name like a method.

The tricky part is to add parameters.
If the applescript looks like this:

on sendmail (receiver, mailtext) ...

then make sure your saved applescript file is also named “sendmail”.

Then you can invoke it from Xojo just like this:

sendmail ("someone@mail.com", "bla bla bla")

Correct me, someone, if I’m wrong, please :slight_smile:

I use CurlsMbs and it’s Mac App Store safe.

nice catch, will this method also work on windows or even linux ?

I forgot to say that I have attached pictures to include in the mail
so it seems the “mailto:” method above will not work
but thomas, will your applescript method work with pictures ?

If you want to send a simple mail without html text or attachments then mailto is okay. For everything else AppleScript is way better.

Here is my embedded AppleScript for creating a mail in Mail:

[code] dim theDictionary as new Dictionary
theDictionary.Value(“theSubject”) = theSubject
theDictionary.Value(“theBody”) = theBody

dim theScript(-1) as String
theScript.Append “property theSubject: ‘’”
theScript.Append “property theBody: ‘’”
theScript.Append “tell application ‘Mail’”
theScript.Append “set newMessage to make new outgoing message with properties {subject: theSubject, content: theBody}”
theScript.Append “tell newMessage”
theScript.Append “set visible to true”
dim CurrentFrom, currentCC as Integer
for currentFrom = 0 to ubound(theFromList)
theScript.Append “make new to recipient at end of to recipients with properties {address: '” + theFromList(currentFrom) + “’}”
next
for currentCC = 0 to ubound(theCCList)
theScript.Append “make new cc recipient at end of cc recipients with properties {address: '” + theCCList(currentCC) + “’}”
next
theScript.append “end tell”
theScript.append “activate”
theScript.append “end tell”

dim doAppleScript as new RunAS(theScript)
doAppleScript.ExecuteScriptWithParameters(theDictionary)[/code]

You just need to fish out the AppleScript if you want to run it directly. How to add an attachment is described here:

https://discussions.apple.com/thread/2735237?start=0&tstart=0

Yes, but it assumes that there is a mail client setup and working.

Also keep in mind that not every Mac user uses Apple Mail - and they get very annoyed when Apple Mail starts and wants to create accounts …

its possible to find out which app handles the mailto URL and then script that app instead
But a single script may not work then as not all apps use the same mail dictionary
So you may need a script for Mail, Thunderbird etc etc etc etc

The following Code works for iCloud Mail Accounts:

[code] Dim e as new CURLEmailMBS

e.SetFrom mailSender, mailSender
e.Subject = mailSubject
e.AddHeader “Mime-Version: 1.0”
e.AddHeader “X-Mailer: my awesome Mailer”
e.SMTPUsername = mailLogin
e.SMTPPassword = mailPassword
e.SetServer mailServerAddress, False
e.AddTo mailRecepient, mailRecepient

e.PlainText = mailBodyPlainText

e.HTMLText = mailBodyHTML

dim c as new CURLSMBS

if c.SetupEmail(e) then

c.OptionTimeOut = 60
c.YieldTime = true
c.OptionVerbose = true
c.CollectOutputData = true
c.CollectDebugData = true
c.OptionPort = mailPort
c.OptionSSLVerifyPeer = 0
c.OptionSSLVerifyHost = 0
c.OptionIPResolve = c.kIPRESOLVE_V4
c.OptionUseSSL = c.kFTPSSL_ALL
c.OptionSSLSessionIDCache = False
c.OptionSSLVersion = c.kFTPAUTH_TLS

ErrorNumber = c.PerformMT
LastErrorText = c.LasterrorMessage

If ErrorNumber = 0 Then
  
  // Success
  
end if

end if[/code]

But you need CURLSMBS

BTW: If your User uses 2 Factor Auth, it needs to use an App specific Password for your the above Code to work.

I think you missed where Jean said [quote=258410:@Jean-Yves Pochez]but I would like to review the outgoing mail before sending it[/quote]
that code doesn’t let hime do that IN the mail client

[quote=258431:@Norman Palardy]I think you missed where Jean said
that code doesn’t let hime do that IN the mail client[/quote]

I am sure it’s possible to review the Source before the Mail goes out. If you use CURLEmailMBS f.e., there is CURLEmailMBS.EmailSource.

I just haven’t tested CURLEmailMBS now.

still not what he wants to do

[quote=258410:@Jean-Yves Pochez]so I would like to generate an email and view it in apple mail before sending it
[/quote]

In most of our apps, we use the ShowURL method. It’ll create a new e-mail in the user’s default e-mail client and sets the recipient, subject and the body of the text. It’s very lightweight and is 100% Sandbox safe. The downside is that my attempts to add attachments have all failed and it’s plain text only.

If you want to add attachments, or use rich text then I’d suggest using an Apple Script. Also there’s a limit on how much text you can include in a URL. Different mail clients seem to have different limits, but most of the time with Apple Mail, it’s not a problem.

Sub mailto(receipient as string, subject as string, body as string) showURL( "mailto:" + receipient + "?Subject=" + encodeURLComponent( subject ) + "&body=" + encodeURLComponent( body ) ) End Sub

[quote=258420:@Beatrix Willius]Here is my embedded AppleScript for creating a mail in Mail:

[code] dim theDictionary as new Dictionary
theDictionary.Value(“theSubject”) = theSubject
theDictionary.Value(“theBody”) = theBody

dim theScript(-1) as String
theScript.Append “property theSubject: ‘’”
theScript.Append “property theBody: ‘’”
theScript.Append “tell application ‘Mail’”
theScript.Append “set newMessage to make new outgoing message with properties {subject: theSubject, content: theBody}”
theScript.Append “tell newMessage”
theScript.Append “set visible to true”
dim CurrentFrom, currentCC as Integer
for currentFrom = 0 to ubound(theFromList)
theScript.Append “make new to recipient at end of to recipients with properties {address: '” + theFromList(currentFrom) + “’}”
next
for currentCC = 0 to ubound(theCCList)
theScript.Append “make new cc recipient at end of cc recipients with properties {address: '” + theCCList(currentCC) + “’}”
next
theScript.append “end tell”
theScript.append “activate”
theScript.append “end tell”

dim doAppleScript as new RunAS(theScript)
doAppleScript.ExecuteScriptWithParameters(theDictionary)[/code]

You just need to fish out the AppleScript if you want to run it directly. How to add an attachment is described here:

https://discussions.apple.com/thread/2735237?start=0&tstart=0[/quote]
I want to use this, but I am getting an error of can’t find a type with this name on the RunAs part, any suggestions?

you need the runas classes here : http://www.mothsoftware.com/content/xojo/
and mbs plugins to use it.

And the MBS plugin is needed.