Mailgun Sender


new

Create a new Mailgun message object.

Syntax

mailgun.new( key, domain[, region] )

Returns

A new message object.

Parameters

Name Description Type Required
key Your Mailgun API key. String Y
domain Your verified Mailgun domain. String Y
region API region endpoint. Possible options are US and EU. The region is set to US by default. String N

Example

US endpoint:

local msg = mailgun.new( "key-abcdef12345", "verified.mailgun.domain" )

EU endpoint:

local msg = mailgun.new( "key-abcdef12345", "verified.mailgun.domain", "EU" )

Note

You can create multple instances of the message object:

local msg1 = mailgun.new(key, domain)
local msg2 = mailgun.new(key, domain)

Message Object


:to

The address(es) to send the message to. Multiple values must be seperated by a comma. This is a required value.

Syntax

msg:to( address[, addressN] )

Parameters

Name Description Type
address The recipient address(es). String

Examples

Single address:

--Address only
msg:to("user@email.com")

--With name
msg:to("Chris <user@email.com>")

Multiple addresses:

msg:to("user1@email.com", "Jim <user2@email.com>")

:from

The message sender address. This is a required value.

Syntax

msg:from( address )

Parameters

Name Description Type
address The sender address. String

Example

msg:from("sender@email.com")

:cc

The address(es) to "cc" the message to. Multiple values must be seperated by a comma.

Syntax

msg:cc( address[, addressN] )

Parameters

Name Description Type
address The "cc" address(es). String

Examples

Single address:

--Address only
msg:cc("user@email.com")

--With name
msg:cc("Chris <user@email.com>")

Multiple addresses:

msg:cc("user1@email.com", "Jim <user2@email.com>")

:bcc

The address(es) to "bcc" the message to. Multiple values must be seperated by a comma.

Syntax

msg:bcc( address[, addressN] )

Parameters

Name Description Type
address The "bcc" address(es). String

Examples

Single address:

--Address only
msg:bcc("user@email.com")

--With name
msg:bcc("Chris <user@email.com>")

Multiple addresses:

msg:bcc("user1@email.com", "Jim <user2@email.com>")

:replyTo

Sets a specific reply-to address. If not set, the sender address is the default.

Syntax

msg:replyTo( address )

Parameters

Name Description Type
address A specific reply-to address. String

Example

msg:replyTo("Support <support@email.com>")

:subject

The subject line for the email. The default is "(no subject)" if not provided.

Syntax

msg:subject( subject_str )

Parameters

Name Description Type
subject_str The subject title. String

Example

msg:subject("A Super Email")

:text

The plain text version of the message body.

Syntax

msg:text( message_str )

Parameters

Name Description Type
message_str A plain string message. String

Example

msg:text("Here is your plain information.")

:html

The html version of the message body.

Syntax

msg:html( message_html )

Parameters

Name Description Type
message_html An html string message. String

Example

msg:html("Here is your <b>bold</b> information.")

:tag

Sets a specific tag for the email. Can be used for tracking in Mailgun.

Syntax

msg:tag( tag_str )

Parameters

Name Description Type
tag_str A string tag for tracking. String

Example

msg:tag("appuser")

:requireTls

Force Mailgun to use TLS when sending the message. Defaults to false.

Syntax

msg:requireTls( bool )

Parameters

Name Description Type
bool Whether to enable TLS for this message. Boolean

Example

msg:requireTls(true)

:skipVerification

Skip certificate verification when using TLS. Defaults to false.

Syntax

msg:skipVerification( bool )

Parameters

Name Description Type
bool Whether to skip verification in TLS. Boolean

Example

msg:skipVerification(true)

:testMode

Used for debugging in Mailgun. Defaults to false.

Syntax

msg:testMode( bool )

Parameters

Name Description Type
bool Whether to enable test mode. Boolean

Example

msg:testMode(true)

:send

Send the final message via Mailgun.

Syntax

msg:send( [listener] )

Parameters

Name Description Type Required
listener A network listener function. Function N

Example

local function onSendResponse( evt )
  if evt.isError then
    print(evt.error)
  else
    print(evt.response)
  end
end

msg:send(onSendResponse)