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>'. password:='<aPassword>'. client := SMTPClient openOnHostNamed: '<yourSmtpServer>' port: <portNumber>. client user: username; password: password; login; initiateSession. client mailFrom: '<fromEmail>'; recipient: '<toEmail>'; data: '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.