[p]This chapter describes how to send email using Lasso. [/p]
[note][b]Note: [/b]The following Chapter 43: POP documents how to download email from a POP server using Lasso. [/note]
[p]Lasso includes a built
[p]Email messages are queued using the [code][Email_Send][/code] tag. All outgoing messages are stored in tables of the [code]Site[/code] database. The queue can be examined and started or stopped in the Utility > Email section of Site Administration. [/p]
[p]Lasso’s email system checks the queue periodically and sends any messages which are waiting. If the email system encounters an error when sending an email then it stores the error in the database and requeues the message. If too many errors are encountered then the message send will be cancelled. [/p]
[p]By default, Lasso sends queued messages directly to the SMTP server which corresponds to each recipient address. This means that a single message may end up being sent to multiple SMTP servers in order to deliver it to each recipient. Lasso can also be configured to route all messages through a local SMTP server in the Utility > Email > Setup section for Site Administration. An optional SMTP AUTH username and password allows Lasso to authenticate with the SMTP server in order to send messages. Lasso will use DIGEST or CRAM
[p]It is also possible to specify SMTP hosts directly within the [code][Email_Send][/code] tag. This can be useful if different SMTP servers need to be used for different purposes. For example, if one SMTP server needs to be used for internal company email and another for general Internet users. [/p]
[note][b]Note: [/b]If a local SMTP server is being used then Lasso must either have valid SMTP AUTH credentials or be otherwise allowed to send unrestricted messages through the SMTP server. Consult the SMTP server documentation for details about how to setup SMTP AUTH security or how to allow specific IP addresses to relay messages. [/note]
[p]By default Lasso will send up to 100 messages to each SMTP server every connection. Lasso will open up to 5 outgoing SMTP connections at a time. Lasso selects messages to send in priority order, but once it connects to an SMTP server it delivers as many messages as possible. This means that a batch send to an SMTP server will contain high priority messages as well as medium and low priority messages. [/p]
[note][b]Important: [/b]The maximum size of an email message including all attachments should be less than 8MB using the [code][Email_Send][/code] tag. If necessary, larger messages can be sent using the [code][Email_Immediate][/code] tag described in the [def]Email Composing[/def] section. [/note]
[p]The email system is administered using the Utility > Email section of Site Administration. The Email Queue can be inspected and any errors which have occurred can be reviewed. Email messages can be queued manually using the Send Email page. The preferences for the email system, such as how often the queue is checked for messages or how many times messages are requeued if an error is detected, can be modified using the Setup page. [/p]
[note][b]Note: [/b]Lasso’s email system is written in LassoScript using the network tags. All of the source code for the email system is included open source within the code for [code]Startup.LassoApp[/code]. [/note]
[note][b]Upgrading [/b][b]Note[/b]: The [code]
[p]The structure of a composed email message will depend on what type of message is being sent. Lasso supports the following structure variations depending on what parameters are specified in the [code][Email_Send][/code] or [code][Email_Compose][/code] tags. [/p]
[p]See each of the following sections for details about how other [code][Email_Send][/code] and [code][Email_Compose][/code] parameters affect the composition of each part. [/p]
[p]The [code][Email_Send][/code] tag is used to send email messages from Lasso. This tag supports the most common types of email including plain text, HTML, HTML with a plain text alternative, embedded HTML images, and attachments. [/p]
[table][tr][th]Tag[/th][th]Description[/th][th] [/th][/tr]
[tr][td][Email_Send][/td][td]Queues an email message. [/td][/tr]
[/table]
[p]The [code][Email_Send][/code] tag accepts many parameters. The basic parameters for sending plain text messages are shown in the table below. Additional parameters for sending HTML email, adding attachments, controlling character set, etc. are included in a subsequent tables. [/p]
[table][tr][th]Keyword[/th][th]Description[/th][th] [/th][/tr]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[/table]
[p]Use the [code][Email_Send][/code] tag with the desired parameters. The [code]
[pre][Email_Send:
<?LassoScript[/pre]
Variable: 'Email_Body' = 'This is the body of the email';
$Email_Body += 'nSent on: ' + (Server_Date) + ' at ' + (Server_Time);
$Email_Body += 'nCurrent visitor: ' + (Client_Username) + ' at ' + (Client_IP);[pre] Email_Send:
-To ='example@example.com',-From ='example@example.com',-Subject ='An Email',-Body =$Email_Body;
?>
[pre][Email_Send:
[p]Email can be sent to multiple recipients by including their addresses as a comma delimited list in the [code]
[pre][Email_Send:
[pre][Variable: 'President'='president@example.com']
[Variable: 'Someone'='someone@example.com']
[Email_Send:
[pre][Email_Send:
[p]HTML messages can be sent from Lasso by specifying the HTML body for the message using the [code]
[table][tr][th]Keyword[/th][th]Description[/th][th] [/th][/tr]
[tr][td]
[tr][td]
[/table]
[p]HTML email can be easily sent using the [code][Email_Send][/code] tag using any of the following methods. [/p]
[pre][Email_Send:
[pre]<h2>Money Saving Coupon</h2>
<p>
Print out the money saving coupon below or click on it to order directly from our Web site.<br />
<a href=”http://www.example.com/couponoffer.html”>
<img src=”http://www.example.com/couponoffer.gif” border=”0” width=”288” height=”288” />
</a>
</p> [/pre]
[pre][Email_Send:
[pre]Money Saving Coupon
Click on the following link to display a money saving coupon which can be printed and used in our store or to order directly from our Web site.
<http://www.example.com/couponoffer.html> [/pre]
[pre]<img src="/apache_pb.gif" /> [/pre]
[pre][Email_Send:
[pre]<img src="cid:apache_pb.gif" /> [/pre]
[pre][Email_Send:
[pre] [Email_Send:
[p]Lasso allows files to be sent as attachments with outgoing email messages. Any file that Lasso can read can be sent as an attachment. Images which are to be displayed inline within an HTML message should be specified using the [code]
[table][tr][th]Keyword[/th][th]Description[/th][th] [/th][/tr]
[tr][td]
[/table]
[p]Files can be included as attachments to email messages using the [code]
[note][b]Important: [/b]The maximum size of an email message including all attachments must be less than 8MB using the [code][Email_Send][/code] tag. If necessary, larger messages can be sent using the [code][Email_Immediate][/code] tag described in the [def]Email Composing[/def] section. [/note]
[p]The following example shows a pair of files being sent with an email message. The attachments are named [code]MyAttachment.txt [/code]and [code]MyAttachment2.text[/code]. They are located in the same folder as the Lasso page which is sending the email. These text files will not be processed by Lasso before they are sent. [/p]
[pre][Email_Send:
[p]Files can be generated programmatically and attached to an email message by specifying a pair including the name of the file and the contents of the file. For example, the following [code][Email_Send][/code] tag uses the [code][PDF_Doc][/code] tag to to create a PDF file. The generated PDF file is sent as an attachment without it ever being written to disk. [/p]
[pre][Var:'MyFile'=(PDF_Doc:
[$MyFile->(Add: (PDF_Text:'I am a PDF document',
[pre][Email_Send:
[p]The [code][Image][/code] tags, [code][Include_Raw][/code] tag, [code][Include][/code] tag, or the [code][Field][/code] tag can also be used to programmatically generate attachments [/p]
[p]Lasso can merge values into email messages just before it sends them. This allows a single email message to be composed and then customized for several recipients. The [code][Email_Send][/code] tag accepts two parameters which make email merge possible. The [/p]
[table][tr][th]Keyword[/th][th]Description[/th][th] [/th][/tr]
[tr][td]
[tr][td]
[/table]
[p]tIn order to use the [code]
[table][tr][th]Keyword[/th][th]Description[/th][th] [/th][/tr]
[tr][td][Email_Token][/td][td]Email tokens can be created using the [Email_Token] tag. This tag requires a single value which is the name of the email token. [/td][/tr]
[tr][td]#TOKEN#[/td][td]Email tokens can also be specified in pound signs. The name of the token should only contain letters, numbers, or underscores. [/td][/tr]
[tr]
[p]For example, an email message can be marked up with email tokens for the first name and last name of the recipient. The start of the message, stored in a file called [code]body.lasso[/code] might be as follows. [/p]
[pre]Dear [Email_Token: 'FirstName'] [Email_Token: 'LastName'],
… [/pre]
[p]The email message is going to be sent to two recipients [code]John Doe[/code] at [code]john@example.com[/code] and [code]Jane Doe[/code] at [code]jane@example.com[/code]. The merge map is constructed as follows. Each element of the map includes an email address as the key and a map of token values as its value. [/p]
[pre][Var: 'myMergeTokens' = (Map:
'john@example.com' = (Map: 'firstname' = 'John', 'lastname' = 'Doe'),
'jane@example.com' = (Map: 'firstname' = 'Jane', 'lastname' = 'Doe'),
)] [/pre]
[p]A default token map can also be constructed. The values from this map would be used if any tokens are missing from the email address specified maps shown above. [/p]
[pre][Var: 'myDefaultTokens' = (Map: 'firstname' = 'Lasso User', 'lastname' = '')] [/pre]
[p]The [code][Email_Send][/code] tag would be written as follows. The email message is being sent to two recipients. The tag references [code]body.lasso[/code] as the [code]
[pre][Email_Send:
[p]The message to [code]John Doe[/code] would contain this text. [/p]
[pre]Dear John Doe,
… [/pre]
[p]This section includes parameters for [code][Email_Send][/code] that are not required for sending basic email messages, but are useful in specific situations. These parameters allow attachments to be included with email messages or for the headers of the outgoing message to be modified. In general, these parameters should only be used if required. [/p]
[table][tr][th]Keyword[/th][th]Description[/th][th] [/th][/tr]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[/table]
[p]Specify a [code]
[p]Most messages should be sent at the default priority. Sending bulk messages like a newsletter at [code]Low[/code] priority will ensure that the normal email from the site is sent as soon as possible rather than waiting for the entire newsletter to be sent first. [code]High[/code] priority should be reserved for time dependent messages such as confirmation emails that a site visitor will be looking for immediately within their email client. [/p]
[p]The [code]
[pre][Email_Send:=
[p]The [code]
[pre][Email_Send:=
[p]By default all sent email will be transmitted directly to the SMTP servers of the recipients. However, if email must be sent through multiple specific SMTP servers then the host, port, and SMTP AUTH username and password can be specified directly within each [code][Email_Send][/code] tag. [/p]
[table][tr][th]Keyword[/th][th]Description[/th][th] [/th][/tr]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[tr][td]
[/table]
[p]Specify the [code]
[pre][Email_Send:
[p]Email messages which are sent using the [code][Email_Send][/code] tag are stored in an outgoing email queue temporarily and then sent by a background process. Any errors encountered when sending a message can be viewed in the [code]Utility > Email > Email Queue [/code]section of Lasso Site Administration. [/p]
[p]However, it is often desirable to get information about a message that was sent programatically without examining the queue table. The tags in the following table allow the status of a recently sent message to be examined. [/p]
[table][tr][th]Tag[/th][th]Description[/th][th] [/th][/tr]
[tr][td][Email_Result][/td][td]Can be called immediately after an [Email_Send] tag to get a unique ID string for the message that was queued. [/td][/tr]
[tr][td][Email_Status][/td][td]Accepts an ID from the [Email_Result] tag and returns the status of the queued message: sent, queued, or error. [/td][/tr]
[/table]
[note][b]Note: [/b]The email sender may take from a few seconds to longer to send an email message. Calling [code][Email_Status][/code] immediately after calling [code][Email_Send][/code] will always return [code]queued[/code]. The [code][Email_Status][/code] tag must be called after a short delay in order to return the true status of the message as [code]sent[/code] or [code]error[/code]. [/note]
[p]The following example shows an [code][Email_Send][/code] tag that sends a message. The [code][Email_Result][/code] tag is called immediately after [code][Email_Send][/code] to store the unique ID of the message that was sent. After a delay of 30 seconds the [code][Email_Status][/code] tag is called to see if the message was successfully sent. [/p]
[pre][Email_Send:
[Var: 'myEmail' = Email_Result]
[Sleep: 30000]
[Email_Status: $myEmail] [/pre]
[p]In a practical solution the unique ID returned by [code][Email_Result][/code] would be stored in a session variable or in a database table and then would be checked some time later using [code][Email_Status][/code] to see if the email message was sent or if the address it was sent to was invalid. [/p]
[p]The [code][Email_Send][/code] tag handles all of the most common types of email that can be sent through Lasso including plaintext messages, HTML messages, HTML messages with a plain text alternative messages, and messages with attachments. [/p]
[p]For more complex messages structures the [code][Email_Compose][/code] object can be used directly to create the MIME text of the message. The message can then be sent with the [code][Email_Queue][/code] tag. Both of these tags are used internally by [code][Email_Send][/code]. [/p]
[p][code][Email_Compose][/code] accepts the same parameters as [code][Email_Send][/code] except those which specify the SMTP server and priority of the outgoing message. After creating an email object with [code][Email_Compose][/code], member tags can be used to add additional text parts, html parts, attachments, or generic MIME parts. This allows very complex email structures to be created with a lot more control than [code][Email_Send][/code] provides. [/p]
[p][code][Email_Compose][/code] can also be used to create email parts. When this tag is called without a [code]
[p]The [code][Email_Queue][/code] tag is designed to be fed an [code][Email_Compose][/code] object. It requires three parameters, the [code]
[p]The [code][Email_Immediate][/code] tag takes the same parameters as the [code][Email_Queue][/code] tag, but sends the message immediately rather than adding it to the email queue. This tag can be used to send messages larger than 8MB if required. A status of sent or an error will still be added to the queue, but the body of the sent message will not be included. Use of the [code][Email_Immediate][/code] tag is not recommended since it bypasses the priority, error handling, and connection handling features of the email sending system. [/p]
[table][tr][th]Tag[/th][th]Description[/th][th] [/th][/tr]
[tr][td][Email_Batch] … [/Email_Batch][/td][td]Temporarily suspends some back
[tr][td][Email_Compose][/td][td]Creates an email object. Accepts the same parameters as [Email_Send]:
[tr][td][Email_Compose->AddAttachment][/td][td]Adds an attachment to an email object.The data of the attachment can be specified directly in the
[tr][td][Email_Compose->AddTextPart][/td][td]Adds a text part to an email object. The text of the part can be specified directly in the
[tr][td][Email_Compose->AddHTMLPart][/td][td]Adds an HTML part to an email object. The text of the HTML part can be specified directly in the
[tr][td][Email_Compose->AddPart][/td][td]Adds a generic part to an email object. Requires a parameter
[tr][td][Email_Compose->Data][/td][td]Returns the MIME text of the composed email. [/td][/tr]
[tr][td][Email_Compose->From][/td][td]Returns the from address of the composed email. [/td][/tr]
[tr][td][Email_Compose->Recipients][/td][td]Returns a list of recipients of the composed email. [/td][/tr]
[tr][td][Email_Queue][/td][td]Queues a message for sending. Requires either an [Email_Compose] object or a
[tr][td][Email_Immediate][/td][td]The same as [Email_Queue], but sends the message immediately without storing it in the database. A
[tr][td][Email_Merge][/td][td]Allows the email merge operation to be performed on any text. Requires two parameters: the text which is to be modified and a
[/table]
[p]The [code][Email_Batch] … [/Email_Batch][/code] tag can be used when a number of messages need to be queued all at once. The tag temporarily suspends some back
[p]The example below shows how an inline might be used to find a collection of email addresses. The [code][Email_Batch] … [/Email_Batch][/code] tags ensure that the messages are queued as fastly as possible. [/p]
[pre][Email_Batch]
[Inline:
[Records]
[Email_Send:
[/Records]
[/Inline]
[/Email_Bath] [/pre]
[note][b]Note: [/b]The email merge tags which are discussed earlier in this chapter can also be used to send an email message to a collection of recipients quickly. [/note]
[p]The [code][Email_Compose][/code] object can be used to compose an email message. In this example a simple email message is created in a variable [code]message[/code]. [/p]
<?LassoScript
var: 'message' = (Email_Compose:
?>
[p]The text of the composed email message can be viewed by outputing the variable [code]$message[/code] to the page. Note that [code][Encode_HTML][/code] should always be used since certain headers of the email message use angle brackets to surround values. Also, HTML [code]<pre> … </pre>[/code] tags make it a lot easier to see the formatting of the email message. [/p]
[p]<pre>[Encode_HTML: $message]</pre> [/p]
[p]Additional text or html parts or attachments can be added using the appropriate member tags with the [code]$message[/code] variable. For example, an attachment can be added using the [code][Email_Compose->AddAttachment][/code] tag as follows: [/p]
<?LassoScript
$message->(AddAttachment:
?>
[p]An email message that was created using the [code][Email_Compose][/code] object can be queued for sending using the [code][Email_Queue][/code] tag. The following example shows how to send the email message created above. The three required parameters [code]
<?LassoScript
Email_Queue:
?>
[p]The [code][Email_Queue][/code] tag will send to the default SMTP server set up in the Utility > Email > Setup section of Site Administration. If the email needs to be sent to a different SMTP server the [code]
[p]All communication with remote SMTP servers is handled by a data type called [code][Email_SMTP][/code]. These connections are normally handled automatically by the [code][Email_Send][/code], [code][Email_Queue][/code], [code][Email_Immediate][/code], and background email sending process. [/p]
[p]The [code][Email_SMTP][/code] type can be used directly for low
[table][tr][th]Tag[/th][th]Description[/th][th] [/th][/tr]
[tr][td][Email_SMTP][/td][td]Creates a new SMTP connection object. [/td][/tr]
[tr][td][Email_SMTP->Open][/td][td]Requires a
[tr][td][Email_SMTP->Send][/td][td]Sends a single message to the SMTP server. Requires a
[tr][td][Email_SMTP->Command][/td][td]Sends a raw command to the SMTP server. The
[tr][td][Email_SMTP->Close][/td][td]Closes the connection to the remote server. [/td][/tr]
[tr][td][Email_MXLookup][/td][td]This tag takes a domain as a parameter and returns a map that describes the MX server for the domain. The map includes the domain, host, username, password, timeout, and SSL preference for the MX server. [/td][/tr]
[/table]
[p]Use the [code][Email_MXLookup][/code] tag. This tag returns a map that describes the preferred MX server for the domain. An example lookup for AOL is shown below. If the administrator has entered routes into Lasso Site Administration then that information will be returned by this tag. Otherwise, the first time an MX record is looked up it will be cached and the same information will be returned on subsequent lookups. [/p]
[pre][Email_MXLookup: 'aol.com'] [/pre]
[pre]
map: (domain)=(aol.com), (password)=(), (host)=(mailin-02.mx.aol.com), (ssl)=(), (cache)=(3), (username)=(), (timeout)=(), (route)=(0) [/pre]
[p]The [code][Email_SMTP][/code] object can be used to send one or more messages directly to an SMTP server. In the following example a message is created using the [code][Email_Compose][/code] tag. That message is then sent to an example SMTP server [code]smtp.example.com[/code] using an SMTP AUTH username and password. Once the message is sent the connection is closed. [/p]
[p]This example does not perform any error checking and only sends one message. The actual source code for the built
<?LassoScript[/pre][pre] local: 'message' = (Email_Compose:
-To ='example@example.com',-From ='example@example.com',-Subject ='Example Message',-Body ='Example Message');
local: 'smtp' = (email_smtp);
#smtp->(open:-host ='smtp.example.com',-port =25,-username ='SMTPUSER',-password ='mysecretpassword',-timeout =60);
#smtp->(send:-from =#message->from,-recipients =#message->recipients,-message =#message->data + 'rn');
#smtp->close; [/pre][pre]?>