Email

Documentation of Meteor's email API.

The email package allows sending email from a Meteor app. To use it, add thepackage to your project by running in your terminal:

  1. meteor add email

The server reads from the MAILURL environment variable to determine how tosend mail. The MAIL_URL should reference anSMTP server anduse the form smtp://USERNAME:PASSWORD@HOST:PORT orsmtps://USERNAME:PASSWORD@HOST:PORT. The smtps:// form (the s is for“secure”) should be used if the mail server requires TLS/SSL (and does not useSTARTTLS) and is most common on port 465. Connections which start unencryptedprior to being upgraded to TLS/SSL (using STARTTLS) typically use port 587(and _sometimes 25) and should use smtp://. For more information see theNodemailer docs

If MAIL_URL is not set, Email.send outputs the message to standard outputinstead.

Server

Email.send(options)

import { Email } from 'meteor/email' (email/email.js, line 145)

Send an email. Throws an Error on failure to contact mail serveror if mail server returns an error. All fields should matchRFC5322 specification.

If the MAIL_URL environment variable is set, actually sends the email.Otherwise, prints the contents of the email to standard out.

Note that this package is based on mailcomposer 4, so make sure to refer tothe documentationfor that version when using the attachments or mailComposer options.

Options

  • fromString
  • "From:" address (required)

  • to, cc, bcc, replyToString or Array of Strings

  • "To:", "Cc:", "Bcc:", and "Reply-To:" addresses

  • inReplyToString

  • Message-ID this message is replying to

  • referencesString or Array of Strings

  • Array (or space-separated string) of Message-IDs to refer to

  • messageIdString

  • Message-ID for this message; otherwise, will be set to a random value

  • subjectString

  • "Subject:" line

  • text, htmlString

  • Mail body (in plain text and/or HTML)

  • watchHtmlString

  • Mail body in HTML specific for Apple Watch

  • icalEventString

  • iCalendar event attachment

  • headersObject

  • Dictionary of custom headers - e.g. { "header name": "header value" }. To set an object under a header name, use JSON.stringify - e.g. { "header name": JSON.stringify({ tracking: { level: 'full' } }) }.

  • attachmentsArray of Objects

  • Array of attachment objects, asdescribed in the mailcomposer documentation.

  • mailComposerMailComposer

  • A MailComposerobject representing the message to be sent. Overrides all other options.You can create a MailComposer object vianew EmailInternals.NpmModules.mailcomposer.module.

You must provide the from option and at least one of to, cc, and bcc;all other options are optional.

Email.send only works on the server. Here is an example of how aclient could use a server method call to send an email. (In an actualapplication, you’d need to be careful to limit the emails that aclient could send, to prevent your server from being used as a relayby spammers.)

  1. // Server: Define a method that the client can call.
  2. Meteor.methods({
  3. sendEmail(to, from, subject, text) {
  4. // Make sure that all arguments are strings.
  5. check([to, from, subject, text], [String]);
  6. // Let other method calls from the same client start running, without
  7. // waiting for the email sending to complete.
  8. this.unblock();
  9. Email.send({ to, from, subject, text });
  10. }
  11. });
  12. // Client: Asynchronously send an email.
  13. Meteor.call(
  14. 'sendEmail',
  15. 'Alice <alice@example.com>',
  16. 'bob@example.com',
  17. 'Hello from Meteor!',
  18. 'This is a test of Email.send.'
  19. );