Sending out emails from Seaside/Squeak

I think something that probably no-one has covered so far in any Seaside blogs is how to send SMTP emails through Seaside or Squeak. If you’re doing a public Seaside application you’ll probably end up having to send emails for various reasons like resetting of passwords or alerts to users etc, so this is something useful to have up your sleeves.

There are a number of ways to send an email in Squeak and Seaside and I know of 3 at the moment.

1. There is a utility class, which contains only class-level messages, called SeasidePlatformSupport, and it has a message called ‘deliverMailFrom: to: text: ‘ that sends a mail directly. However, you’ll need to set the SMTP server first, by using MailSender.

This is a code snippet you can cut and paste directly on to a workspace to evaluate(though obiviously replacing the appropriate email addresses):

MailSender setSmtpServer.
SeasidePlatformSupport deliverMailFrom: '<fromEmail>'
to: #('<toEmail>')
text: 'Subject: Hello from SeasidePlatformSupport!
Sending from SeasidePlatformSupport'.

Don’t worry that the setSmtpServer message doesn’t take in any parameters. What happens is that MailSender pops up a dialog box for you to enter the SMTP server. But you’ll need to enter an SMTP server that doesn’t need authentication because there isn’t a way to set the user name or password from this mechanism. Of course you can’t set the mail server from Seaside either (the dialog box is Morphic) but you can run the MailSender once from the Squeak environment and you’ll be good for the rest of the application. Notice the ‘to’ is actually an array of recipients.

2. The second way is to go directly to the MailComposition class that SeasidePlatformSupport is using (through MailSender). This is another piece of code snippet that you can directly evaluate from workspace:

MailSender setSmtpServer.
msg := (MailMessage new setField: 'to' to: '<toEmail>';
setField: 'from' to: '<fromEmail>';
setField: 'subject' to: 'Hello from MailMessage!';
body: 'Sending from MailMessage'; yourself).
(MailComposition new sendMailMessage: msg) submit.

Again you’ll need to set the MailSender SMTP server first. There isn’t any much difference in doing it this way, as far as I can see, but this is another way you can send emails.

3. Another way is directly go to the underlying guts of the other two mechanisms and use the SMTPClient class itself to send the email. SMTPClient subclasses TelnetProtocolClient and if you dig a bit deeper you’ll realise that it does nothing but to open up a socket session at the appropriate port and send in the SMTP commands as text.

username:= '<aUsername>'.
client := SMTPClient openOnHostNamed: '<yourSmtpServer>' port: <portNumber>.
client user: username; password: password; login; initiateSession.
client mailFrom: '<fromEmail>';
 	recipient: '<toEmail>';
'Subject: Hello from SMTPClient!
From: <fromEmail>
To: <toEmail>
Sending from SMTPClient!'.
client quit.

This is actually the underlying code that sends the mail in the first two mechanisms though they are couched by different classes. Of course ultimately you can use TelnetProtocolClient itself to do the work but that’s a bit going down to the basics too much :)

Note that this last method of sending emails you can authenticate the SMTP server and also set the port number.

About these ads

4 thoughts on “Sending out emails from Seaside/Squeak

  1. Sweet man. Would be good to also talk about integrating som seaside app process with the email send. A typical case would be a recover of password email. How a seaside callback can be sent in a link of that email. Did you make something like that?

  2. Hi, I found this site looking for some hint in how to send emails automatically when you add a post(specifically in Pier).

    I’m comment this to you because google said to me that this site contains malware.

    Maybe you should check that. Just some words.

    Did you use this on Pier(and with Visualworks)?

  3. One other thing I would like to convey is that in lieu of trying to fit all your online degree programs on days that you finish off work (since the majority of people are exhausted when they return home), try to receive most of your sessions on the saturdays and sundays and only a couple of courses on weekdays, even if it means a little time off your saturday and sunday. This is beneficial because on the week-ends, you will be extra rested as well as concentrated in school work. Many thanks for the different tips I have figured out from your website.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s