Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for inline email attachments #5

Merged
merged 1 commit into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/net/sargue/mailgun/MultipartBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public MultipartBuilder attachment(String content, String filename) {
return bodyPart(new StreamDataBodyPart("attachment", is, filename));
}

/**
* Adds a named inline attachment.
*
* @param is an stream to read the attachment
* @param cidName the name to give to the attachment as referenced by the HTML email body
* i.e. use cidName sample-image.png for the below example
* <p>
* <img src="cid:sample-image.png"/>
* </p>
* @return this builder
*/
public MultipartBuilder inline(InputStream is, String cidName) {
return bodyPart(new StreamDataBodyPart("inline", is, cidName));
}

/**
* Finishes the building phase and returns a {@link Mail}.
* <p>
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/net/sargue/mailgun/test/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.Rule;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -197,6 +198,28 @@ public void sendWithAttachment() {
//TODO proper content checking
}

@Test
public void sendWithInlineAttachment() {
stubFor(post(urlEqualTo("/api/" + DOMAIN + "/messages"))
.withHeader("Authorization",
equalTo("Basic " + expectedAuthHeader))
.withHeader("Content-Type",
containing("multipart/form-data"))
.willReturn(aResponse().withStatus(200)));

Response response = MailBuilder.using(configuration)
.to("doc@delorean.com")
.subject("This message has an text attachment")
.html("<html>Inline image here: <img src=\"cid:cartman.jpg\"></html>")
.multipart()
.inline(new ByteArrayInputStream("MockBytes".getBytes()), "cartman.jpg")
.build()
.send();
Assert.assertTrue(response.isOk());

//TODO proper content checking
}

@Test
public void sendAsync() {
stubFor(expectedBasicPost().willReturn(aResponse().withStatus(200)));
Expand Down