Email Library

revIgniter's robust Email Library supports the following features:

Sending Email

Sending email is not only simple, but you can configure it on the fly or set your preferences in a config file.

Here is a basic example demonstrating how you might send email. Note: This example assumes you are sending the email from one of your controllers.

loaderLoadLibrary "Email"

mailFrom "your@example.com", "Your Name"
mailTo "someone@example.com"
mailCC "another@another-example.com"
mailBcc "them@their-example.com"

mailSubject "Email Test"
mailMessage "Testing the email library."

put mailSend() into tMailResult

put printMailDebugger() into gData["mailDebug"]

Setting Email Preferences

There are 17 different preferences available to tailor how your email messages are sent. You can either set them manually as described here, or automatically via preferences stored in your config file, described below:

Preferences are set by passing an array of preference values to the email initializeMail handler. Here is an example of how you might set some preferences:

put "sendmail" into tConfig["protocol"]
put "/usr/sbin/sendmail" into tConfig["mailpath"]
put "iso-8859-1" into tConfig["charset"]
put TRUE into tConfig["wordwrap"]

initializeMail tConfig

Note: Most of the preferences have default values that will be used if you do not set them.

Setting Email Preferences in a Config File

If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the email.irev, add the tConfig array in that file. At the end of the file write: runInitialMailConfig yourArrayVariableName. Then save the file at config/email.irev and it will be used automatically. You will NOT need to use the initializeMail handler if you save your preferences in a config file. Example:

local sEmailConf

put "sendmail" into sEmailConf["protocol"]
put "/usr/sbin/sendmail" into sEmailConf["mailpath"]
put "iso-8859-1" into sEmailConf["charset"]
put TRUE into sEmailConf["wordwrap"]

runInitialMailConfig sEmailConf

Email Preferences

The following is a list of all the preferences that can be set when sending email.

Preference Default Value Options Description
mailUseragentrevIgniterNoneThe "user agent".
protocolmailmail, sendmail, or smtpThe mail sending protocol.
mailPath/usr/sbin/sendmailNoneThe server path to Sendmail.
smtpHostNo DefaultNoneSMTP Server Address.
smtpUserNo DefaultNoneSMTP Username.
smtpPassNo DefaultNoneSMTP Password.
smtpPort25NoneSMTP Port.
smtpTimeout5NoneSMTP Timeout (in seconds).
wordwrapTRUETRUE or FALSE (boolean)Enable word-wrap.
wrapchars76 Character count to wrap at.
mailtypetexttext or htmlType of mail. If you send HTML email you must send it as a complete web page. Make sure you don't have any relative links or relative image paths otherwise they will not work.
charsetutf-8Character set (utf-8, iso-8859-1, etc.).
validateFALSETRUE or FALSE (boolean)Whether to validate the email address.
priority31, 2, 3, 4, 5Email Priority. 1 = highest. 5 = lowest. 3 = normal.
crlf LF CRLF or LF or numberToChar(13) Newline character. (Use CRLF to comply with RFC 822).
newlineLF CRLF or LF or numberToChar(13)Newline character. (Use CRLF to comply with RFC 822).
bccBatchModeFALSETRUE or FALSE (boolean)Enable BCC Batch Mode.
bccBatchSize200NoneNumber of emails in each BCC batch.

Email Method Reference

mailFrom

Sets the email address and name of the person sending the email:

mailFrom "you@example.com", "Your Name"

replyTo

Sets the reply-to address. If the information is not provided the information in the "mailFrom" handler is used. Example:

replyTo "you@example.com", "Your Name"

mailTo

Sets the email address(s) of the recipient(s). Can be a single email, a comma-delimited list or an array:

mailTo "someone@example.com" mailTo "one@example.com,two@example.com,three@example.com" put "one@example.com" into tList[1]
put "two@example.com" into tList[2]
put "three@example.com" into tList[3]

mailTo tList

mailCC

Sets the CC email address(s). Just like the "mailTo", can be a single email, a comma-delimited list or an array.

mailBcc

Sets the BCC email address(s). Just like the "mailTo", can be a single email, a comma-delimited list or an array.

mailSubject

Sets the email subject:

mailSubject "This is my subject"

mailMessage

Sets the email message body:

mailMessage "This is my message"

set_sAltMessage

Sets the alternative email message body:

set_sAltMessage "This is the alternative message"

This is an optional message string which can be used if you send HTML formatted email. It lets you specify an alternative message with no HTML formatting which is added to the header string for people who do not accept HTML email. If you do not set your own message revIgniter will extract the message from your HTML email and strip the tags.

clearMail

Initializes all the email variables to an empty state. This handler is intended for use if you run the email sending function in a loop, permitting the data to be reset between cycles.

repeat for each key tName in tList
put tList[tName] into tAddress
    clearMail

    mailTo tAddress
    mailFrom "your@example.com"
    mailSubject "Here is your info" && tName
    mailMessage "Hi" && tName && "Here is the info you requested."
    put mailSend() into tMailResult
end repeat

If you set the parameter to TRUE any attachments will be cleared as well:

clearMail TRUE

mailSend()

The Email sending function. Returns boolean TRUE or FALSE based on success or failure, enabling it to be used conditionally:

if mailSend() is not TRUE then
    -- Generate error
end if

mailAttach

Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple times. For example:

mailAttach "/path/to/photo1.jpg"
mailAttach "/path/to/photo2.jpg"
mailAttach "/path/to/photo3.jpg"

put mailSend() into tMailResult

printMailDebugger()

Returns a string containing any server messages, the email headers, and the email messsage. Useful for debugging.

Overriding Word Wrapping

If you have word wrapping enabled (recommended to comply with RFC 822) and you have a very long link in your email it can get wrapped too, causing it to become un-clickable by the person receiving it. revIgniter lets you manually override word wrapping within part of your message like this:

The text of your email that
gets wrapped normally.

{unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}

More text that will be
wrapped normally.

Place the item you do not want word-wrapped between: {unwrap} {/unwrap}