-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Paolo Di Tommaso <[email protected]>
- Loading branch information
1 parent
624a853
commit c4dae40
Showing
17 changed files
with
1,576 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
plugins { | ||
id 'io.seqera.java-library-conventions' | ||
id 'io.seqera.groovy-library-conventions' | ||
// Micronaut minimal lib | ||
// https://micronaut-projects.github.io/micronaut-gradle-plugin/latest/ | ||
id "io.micronaut.minimal.library" version '3.7.10' | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven { url = 'https://s3-eu-west-1.amazonaws.com/maven.seqera.io/releases' } | ||
maven { url = 'https://s3-eu-west-1.amazonaws.com/maven.seqera.io/snapshots' } | ||
maven { url = 'https://jitpack.io' } | ||
} | ||
|
||
dependencies { | ||
compileOnly "io.micronaut:micronaut-inject-groovy" | ||
implementation "io.seqera:wave-utils:0.5.0" | ||
implementation "ch.qos.logback:logback-classic:1.2.11" | ||
implementation "org.codehaus.groovy:groovy:3.0.15" | ||
implementation "org.codehaus.groovy:groovy-nio:3.0.15" | ||
implementation "org.codehaus.groovy:groovy-templates:3.0.15" | ||
api "com.sun.mail:javax.mail:1.6.2" | ||
api "org.jsoup:jsoup:1.15.3" | ||
implementation("io.micronaut:micronaut-runtime:3.3.4") | ||
|
||
testImplementation "org.subethamail:subethasmtp:3.1.7" | ||
} | ||
|
||
group = 'io.seqera' | ||
version = rootProject.version | ||
|
||
micronaut { | ||
version '3.9.4' | ||
runtime("netty") | ||
testRuntime("spock2") | ||
processing { | ||
incremental(true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
/* | ||
* Copyright (c) 2019-2020, Seqera Labs. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* This Source Code Form is "Incompatible With Secondary Licenses", as | ||
* defined by the Mozilla Public License, v. 2.0. | ||
*/ | ||
|
||
package io.seqera.mail | ||
|
||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.time.OffsetDateTime | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.transform.ToString | ||
/** | ||
* Helper class modeling mail parameters | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
@ToString(includes = 'from,to,cc,subject') | ||
@CompileStatic | ||
class Mail implements Serializable { | ||
|
||
UUID id | ||
|
||
String from | ||
|
||
String to | ||
|
||
String cc | ||
|
||
String bcc | ||
|
||
String subject | ||
|
||
String charset | ||
|
||
String body | ||
|
||
String text | ||
|
||
String type | ||
|
||
boolean sent | ||
|
||
OffsetDateTime dateCreated | ||
|
||
OffsetDateTime lastUpdated | ||
|
||
String lastError | ||
|
||
List<MailAttachment> attachments = new ArrayList<>() | ||
|
||
/** | ||
* Creates a {@link Mail} object given a {@link Mail} object | ||
* | ||
* @param params | ||
* @return A mail object representing the message to send | ||
*/ | ||
static Mail of(Map params) { | ||
def result = new Mail() | ||
|
||
if( params.from ) | ||
result.from(params.from.toString()) | ||
|
||
if( params.to ) | ||
result.to(params.to.toString()) | ||
|
||
if( params.cc ) | ||
result.cc(params.cc.toString()) | ||
|
||
if( params.bcc ) | ||
result.bcc(params.bcc.toString()) | ||
|
||
if( params.subject ) | ||
result.subject(params.subject.toString()) | ||
|
||
if( params.charset ) | ||
result.charset(params.charset.toString()) | ||
|
||
if( params.type ) | ||
result.type(params.type.toString()) | ||
|
||
if( params.body ) | ||
result.body(params.body) | ||
|
||
if( params.text ) | ||
result.text(params.text) | ||
|
||
if( params.attach ) | ||
result.attach(params.attach) | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Mail sender (addresses must follow RFC822 syntax) | ||
* Multiple addresses can be separated by a comma | ||
*/ | ||
void from( String address ) { | ||
this.from = address | ||
} | ||
|
||
/** | ||
* Mail TO recipients (addresses must follow RFC822 syntax). | ||
* Multiple addresses can be separated by a comma. | ||
*/ | ||
void to( String address ) { | ||
this.to = address | ||
} | ||
|
||
/** | ||
* Mail CC recipients (addresses must follow RFC822 syntax). | ||
* Multiple addresses can be separated by a comma. | ||
*/ | ||
void cc( String address ) { | ||
this.cc = address | ||
} | ||
|
||
/** | ||
* Mail BCC recipients (addresses must follow RFC822 syntax). | ||
* Multiple addresses can be separated by a comma. | ||
*/ | ||
void bcc( String address ) { | ||
this.bcc = address | ||
} | ||
|
||
/** | ||
* @param subject The email subject | ||
*/ | ||
void subject( String subject ) { | ||
this.subject = subject | ||
} | ||
|
||
private String stringify( value ) { | ||
if( value instanceof File ) | ||
return value.text | ||
if( value instanceof Path ) | ||
return new String(Files.readAllBytes(value)) | ||
if( value instanceof CharSequence ) | ||
return value.toString() | ||
if( value != null ) | ||
throw new IllegalArgumentException("Not a valid mail body argument [${value.getClass().getName()}]: $value") | ||
return null | ||
} | ||
|
||
/** | ||
* @param str The email content | ||
*/ | ||
void body( value ) { | ||
this.body = stringify(value) | ||
} | ||
|
||
/** | ||
* Plain text mail content | ||
* @param text The mail text content | ||
*/ | ||
void text( value ) { | ||
this.text = stringify(value) | ||
} | ||
|
||
/** | ||
* Mail content mime-type | ||
*/ | ||
void type( String mime ) { | ||
this.type = mime | ||
} | ||
|
||
/** | ||
* @param charset The mail content charset | ||
*/ | ||
void charset( String charset ) { | ||
this.charset = charset | ||
} | ||
|
||
/** | ||
* Add an email attachment | ||
* | ||
* @param item A attachment file path either as {@link File}, {@code Path} or {@link String} path | ||
*/ | ||
void attach( item ) { | ||
|
||
if( item instanceof MailAttachment ) { | ||
this.attachments.add((MailAttachment)item) | ||
} | ||
else if( item instanceof Collection ) { | ||
for( def it : ((Collection)item) ) | ||
this.attachments.add(new MailAttachment(it)) | ||
} | ||
else if( item instanceof Object[] ) { | ||
for( def it : ((Object[])item) ) | ||
this.attachments.add(new MailAttachment(it)) | ||
} | ||
else if( item ) { | ||
this.attachments.add(new MailAttachment(item)) | ||
} | ||
} | ||
|
||
/** | ||
* Add an email attachment headers | ||
* | ||
* @param headers | ||
* MailAttachment optional content directives. The following parameters are accepted: | ||
* - contentId: Set the "Content-ID" header field of this body part | ||
* - fileName: Set the filename associated with this body part, if possible | ||
* - description: Set the "Content-Description" header field for this body part | ||
* - disposition: Set the "Content-Disposition" header field of this body part | ||
* | ||
* @param item | ||
*/ | ||
void attach( Map headers, item ) { | ||
|
||
if( this.attachments == null ) | ||
this.attachments = [] | ||
|
||
this.attachments << new MailAttachment(headers, item) | ||
} | ||
|
||
} |
Oops, something went wrong.