Purpose¶
Send emails.
Methods¶
Binding name: p6.email
Info
Above all, you must check that you have a default email profile set in the Email Profiles service.
readMailbox¶
Read a mailbox
Syntax
Parser p6.email.readMailbox( Map<String, Object> configuration )
Parameter: configuration
Configuration Name | Description |
---|---|
onEmail | Mandatory. Closure to parse the email (first argument). Return false to stop mailbox parsing. (cf parse method to see the returned type) |
type | Optional. The mailbox type pop3 , imap or imaps (default to imaps ) |
search | Optional. Search criteria used to filter emails. (default to flag:!seen ) |
server | Optional. Server connection configuration (as a Map |
session | Optional. Session connection configuration (as a Map |
debug | Optional. Display debug information such as connection parameters (boolean, default to false ) |
search
Recognized search criteria are:
subject
for the email subjectbody
for the email bodyfrom
for the email senderflag
for the email flags- Allowed flags are
seen
,flagged
,answered
,deleted
,draft
,recent
anduser
- Multiple flags can be used joining on a
,
- You can set the negation of the flag by adding a
!
before the name
- Allowed flags are
Tip
If the search can’t be expressed with the current mechanism, you can pass instead of a map a SearchTerm
. (cf examples)
server
server
: server connection configuration (as a Map
mailbox.server.host
: host namemailbox.server.username
: usernamemailbox.server.password
: passwordmailbox.server.port
: port (default to-1
as disabled value)mailbox.server.folder
: folder (default toINBOX
)
Default values
Parameters have default values based on the mailbox type:
- pop3
- host
= pop.gmail.com
- imap
- host
= imap.gmail.com
- imaps
- host
= imap.gmail.com
session
session
: session connection configuration (as a Map
Default values
Parameters have default values based on the mailbox type:
- pop3
- mail.pop3.host
= pop.gmail.com
- mail.pop3.port
= 995
- mail.pop3.ssl.enable
= true
- imap
- mail.imap.host
= imap.gmail.com
- mail.imap.port
= 143
- mail.imap.ssl.enable
= false
- imaps
- mail.imaps.host
= imap.gmail.com
- mail.imaps.port
= 993
- mail.imap.ssl.enable
= true
Tip
Server variables from pipeline should be prefixed by mail.server.
Info
The configuration entries used to connect to the server are used with the priority order:
- conf value
- pipeline value
- default value
Note
All the server
or session
properties passed to the method that are not part of the recognized one
will be added to the final parameters
Examples
Read Gmail mailbox
def config = [
server: [username: 'user@server.com', password: "secret"],
onEmail: (email) -> {
p6.log.debug "Subject: " + email.getSubject()
p6.log.debug "From: " + email.getFrom()
p6.log.debug "To: " + email.getSingleTo()
p6.log.debug "Reply-To: " + email.getReplyTo()
p6.log.debug "Sent date: " + email.getSentDate()
p6.log.debug "Received date: " + email.getReceivedDate()
p6.log.debug "Plain content? " + (email.hasPlainContent() ? "Yes" : "No")
p6.log.debug "Html content? " + (email.hasHtmlContent() ? "Yes" : "No")
p6.log.debug "Attachments? " + (email.hasAttachments() ? ("Yes (" + (email.getAttachmentList().size()) + ")") : "No")
if (email.hasAttachments()) {
email.getAttachmentList().each { attachment ->
p6.log.debug " - " + attachment.getName() + " (" + attachment.getContentType() + ")"
def targetFile = File.createTempFile("email", "attachement");
log.debug "Saving to... " + targetFile.toString()
log.debug email.saveAttachment(attachment, targetFile.toPath()) ? "OK" : "Failure";
}
}
}
]
p6.email.readMailbox(config)
Read random mailbox
def config = [
server: [
username: "username@server.com",
password: "password",
host: "imaps.server.com",
folder: "Global",
port: "145",
],
onEmail: (email) -> {
// Stop on first email without attachment
return email.hasAttachments();
}
]
p6.email.readMailbox(config)
Search parameters
def config = [
search: [from: 'sender@server.com', flag: '!seen'],
server: [
username: "username@server.com",
password: "password",
],
onEmail: (email) -> {
// Unseen emails from sender@server.com
}
]
p6.email.readMailbox(config)
Multiple flags
def config = [
search: [flag: '!seen,draft'],
server: [
username: "username@server.com",
password: "password",
],
onEmail: (email) -> {
// Unseen drafts
}
]
p6.email.readMailbox(config)
Complex search
import javax.mail.search.*;
def config = [
search: new OrTerm(new FlagTerm(new Flags(Flags.Flag.ANSWERED), true), new FlagTerm(new Flags(Flags.Flag.RECENT), true)),
server: [
username: "username@server.com",
password: "password",
],
onEmail: (email) -> {
// answered emails OR recent one
}
]
p6.email.readMailbox(config)
Additional mailbox parameters
p6.pipeline.put('mail.server.token', 'secret')
def config = [
server: [
username: "username@server.com",
password: "password",
fee: "foo",
],
session: [
bar: "bee",
],
debug: true,
onEmail: (email) -> {
}
]
p6.email.readMailbox(config)
Debug
By setting debug
to true the following output will be displayed
Connection parameters form 'imaps'
- Server parameters: ["host": "imap.gmail.com", "username": "username@server.com", "password": "password", "folder": "INBOX", "fee": "foo", "token": "secret"]
- Session parameters: ["mail.store.protocol": "imaps", "mail.imaps.host": "imap.gmail.com", "mail.imaps.port": "993", "mail.imap.ssl.enable": "true", "bar": "fee"]
parse¶
Parses the given MimeMessage returning a Parser object
Syntax
Parser p6.email.parse( MimeMessage message )
Parser definition
Date getSentDate()
: return the sent dateDate getReceivedDate()
: return the received dateString getFirst(List<Address>)
: return the first address as stringList<Address> getTo()
: return the list of recipient of type TOString getSingleTo()
: return the first of recipient of type TOList<Address> getCc()
: return the list of recipient of type CCString getSingleCc()
: return the first of recipient of type CCList<Address> getBcc()
: return the list of recipient of type BCCString getSingleBcc()
: return the first of recipient of type BCCString getFrom()
: return the senderString getReplyTo()
: return the reply-toString getSubject()
: return the subjectboolean isMultipart()
: is the email multipart?boolean hasHtmlContent()
: has the email HTML content?String getHtmlContent()
: return the HTML contentboolean hasPlainContent()
: has the email plain content?String getPlainContent()
: return the plain contentCollection<String> getContentIds()
: return content idsboolean hasAttachments()
: has the email attachments?List<DataSource> getAttachmentList()
: return attachmentsDataSource findAttachmentByName(String name)
: return an attachment by nameDataSource findAttachmentByCid(String cid)
: return an attachment by cidboolean saveAttachment(DataSource dataSource, Path target)
: save the attachment on the target (returns false on failure)boolean saveAttachment(String name, Path target)
: save the attachment by it name on the target (returns false on failure)
Example
def config = [
server: [],
onEmail: (email) -> {
println "Email received from: " + p6.email.parse(email).getFrom()
return true;
}
]
p6.email.readMailbox(config)
sendEmail¶
Send an email using the JavaMail API. It will return whether the email has been correctly sent or not.
Syntax
boolean p6.email.sendEmail( final String from, final String to, final String plainBody )
Example
Send a message from dev.b2box.com@amalto.com to toto@amalto.com.
p6.email.sendEmail('dev.b2box.com@amalto.com', 'toto@amalto.com', 'This is email body content.')
sendEmail (with params)¶
Send an email using the JavaMail API and returns whether the email has been correctly sent or not.
Syntax
boolean p6.email.sendEmail(
final String from,
final String to,
final String plainBody,
final Map<String, String> params
)
Parameter: params
Example
Send a message from dev.b2box.com@amalto.com to toto@amalto.com specifying the subject.
def params = [ 'subject': 'Email Service Test' ]
p6.email.sendEmail('dev.b2box.com@amalto.com', 'toto@amalto.com', 'This is email body content.', params)
sendEmail (with attachments)¶
Send an email using the JavaMail API. It will return whether the email has been correctly sent or not.
Syntax
boolean p6.email.sendEmail(
final String from,
final String to,
final String plainBody,
final Map<String, String> params,
final List<Map> mapAttachments
)
Parameter: params
Parameter: mapAttachments
Example
Send a message from dev.b2box.com@amalto.com to toto@amalto.com specifying the subject and attaching two files.
def attachments = [
[
headers: [
filename: 'hello.txt',
type: 'text/plain'
],
bytes: 'This is attachment content'.getBytes()
],
[
headers: [ fileurl: 'file:///opt/p6core.data/logo.tiff' ]
]
]
def params = [ 'subject': 'Email Service Test' ]
print p6.email.sendEmail('dev.b2box.com@amalto.com', 'roxane.mace@amalto.com', 'This is email body content.\n', params, attachments)
sendHtmlEmail¶
Send an email using the JavaMail API. It will return whether the email has been correctly sent or not.
Syntax
boolean p6.email.sendHtmlEmail( final String from, final String to, final String htmlBody )
Example
Send a message from dev.b2box.com@amalto.com to toto@amalto.com.
p6.email.sendEmail('dev.b2box.com@amalto.com', 'toto@amalto.com', 'This is email <b>body</b> content.')
sendHtmlEmail (with params)¶
Send an email using the JavaMail API and returns whether the email has been correctly sent or not.
Syntax
boolean p6.email.sendHtmlEmail(
final String from,
final String to,
final String htmlBody,
final Map<String, String> params
)
Parameter: params
Example
Send a message from dev.b2box.com@amalto.com to toto@amalto.com specifying the subject.
def params = [ 'subject': 'Email Service Test' ]
p6.email.sendHtmlEmail('dev.b2box.com@amalto.com', 'toto@amalto.com', 'This is email <b>body</b> content.', params)
sendHtmlEmail (with attachments)¶
Send an email using the JavaMail API. It will return whether the email has been correctly sent or not.
Syntax
boolean p6.email.sendHtmlEmail(
final String from,
final String to,
final String htmlBody,
final Map<String, String> params,
final List<Map> mapAttachments
)
Parameter: params
Parameter: mapAttachments
Example
Send a message from dev.b2box.com@amalto.com to toto@amalto.com specifying the subject and attaching two files.
def attachments = [
[
headers: [
filename: 'hello.txt',
type: 'text/plain'
],
bytes: 'This is attachment content'.getBytes()
],
[
headers: [ fileurl: 'file:///opt/p6core.data/logo.tiff' ]
]
]
def params = [ 'subject': 'Email Service Test' ]
print p6.email.sendHtmlEmail('dev.b2box.com@amalto.com', 'roxane.mace@amalto.com', 'This is email <b>body</b> content.\n', params, attachments)
sendEmail (multipart)¶
Send a multipart email containing a plain text and html body using the JavaMail API. It will return whether the email has been correctly sent or not.
Syntax
boolean p6.email.sendEmail(
final String from,
final String to,
final String plainBody,
final String htmlBody,
final Map<String, String> params,
final List<Map> mapAttachments
)
Parameter: params
Parameter: mapAttachments
Example
Send a multipart email
def params = [ ]
def attachments = [ ]
p6.email.sendEmail('dev.b2box.com@amalto.com', 'toto@amalto.com', 'This is plain text email body content.', 'This is <b>html email</b> body content.', params, attachments)
sendEmail (raw)¶
Send an email with the JavaMail API using a RAW MIME encoded header map and body. It will return whether the email has been correctly sent or not.
Syntax
boolean p6.email.sendEmail(
String to,
byte[] mimeMessageBytes,
Map<String, String> mimeHeaders
) throws P6Exception
Example
def mimeHeaders = [:]
def message = []
p6.email.sendEmail('toto@amalto.com', message, mimeHeaders)
sendEmail¶
Send an email with the JavaMail API using a RAW MIME encoded header map and body. It will return whether the email has been correctly sent or not.
Syntax
boolean p6.email.sendEmail(
String to,
byte[] mimeMessageBytes,
Map<String, String> mimeHeaders,
Map<String, String> params
) throws P6Exception
Parameter: params
Example
def params = [ 'subject': 'Email Service Test' ]
def mimeHeaders = [:]
def message = []
p6.email.sendEmail('toto@amalto.com', message, mimeHeaders, params)
Parameters¶
Attachments files¶
Zero or more attachments can be specified. Unless using the fileurl
header the attachment content must be specified as bytes.
Key | Description | Value |
---|---|---|
name |
Used to identify the email body (required) | body |
type |
MIME Types (required) | |
filename |
Name for attachment | |
filenurl |
The URL of a file to attach |
Additional parameters of the request¶
You can set the information below in the params
map.
Key | Description |
---|---|
profile.name |
Email profile name to use for the request |
profile.appkey |
Email profile appKey to use for the request |
replyto |
Reply to address |
cc |
Courtesy copy address |
bcc |
Blind courtesy copy address |
subject |
The email subject |