6.5 Sending emails

Sending emails from SmartFoxServer Extension is very easy and it allows developers to send confirmation messages after a user registration, news about the game, administrator messages etc...
Emails can be formatted as simple text or HTML.

Before you can send email messages from an extension you should configure the SMTP server you're going to use.

» Configuring the SMTP

Open your config.xml file and look for the <Mailer></Mailer> block:

<Mailer>
	<MailHost>smtp.mail.com</MailHost>
	<MailUser>myAccount@mail.com</MailUser>
	<MailPass>myPassword</MailPass>
	<WorkerThreads>1</WorkerThreads>
</Mailer>
  

There are only three parameters to set:

MailHost   Your email SMTP IP address or url
MailUser   Your SMTP account
MailPassword   Your account password
WorkerThreads   The number of threads that should handle the mail taks. By default it is set to 1.
We recommend not to change this value unless you really need it and you understand what java threads are and how they work.

» Sending emails from a server side extension

Sending messages with the server side API is straightforward. Here's a quick code example:

/*
* Example of how to send an email.
*/
var from = "info@gotoandplay.it"
var to = "dickDynamite@mail.com"
var subject = "1, 2, 3 ... check!"
var message = "Hi there! This email was sent from SmartFoxServer!"

var ok = _server.sendMail(from, to, subject, message)

if (ok)
	trace("Mail sent!")
else
	trace("Ooops! Failed sending mail to: " + to)

Everything is handled by the _server.sendMail() method which takes four arguments:

from   the email address of the sender
to   the email address of the recipient
subject   the subject of the email
message   a text or html message

The method returns true if the email was sent successfully and if it returns false you should check what's wrong with your SMTP configuration.




doc index