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

Multiple attachement in FormData #14

Closed
micky008 opened this issue Oct 19, 2022 · 7 comments
Closed

Multiple attachement in FormData #14

micky008 opened this issue Oct 19, 2022 · 7 comments

Comments

@micky008
Copy link

Hello,

I've a problem with FormData...
How to put many attachment with FormData...
All attachments is created in memory and i don't want to create and delete a File for sending my email.

Thank you

it's possible to make this ?

@szagr
Copy link
Collaborator

szagr commented Oct 19, 2022

Hi @micky008,
It's currently not possible to use a list of FormData like we can use a list of files.
I see this PR two years ago old. But no progress with it.
OpenFeign/feign-form#92
Hope it should be possible in the future.
Or we can implement some workaround ourselves but not in the near future.

@micky008
Copy link
Author

Thank you for the answer.
so, i try to make a workaround... :'(

@szagr
Copy link
Collaborator

szagr commented Oct 19, 2022

Maybe you can even share it ;)

@micky008
Copy link
Author

i need to find first, but it's ok if i find i post here the solution

@szagr
Copy link
Collaborator

szagr commented Oct 19, 2022

Thank you

@micky008
Copy link
Author

micky008 commented Oct 20, 2022

The solution is very dirty...

I use unirest

public class MyEmailAttachment  {
byte[] content;
String name;
}

public class MailGunService implements IMailerService {

    private static final Logger logger = Logger.getLogger(MailGunService.class);

    @Override
    public void send(List<User> recipient, List<User> ccU, String subject, String message, List<MyEmailAttachment> attachments, String mailingList) throws Exception {

        List<String> to = Collector.collect(recipient, "email");
        List<String> cc = Collector.collect(ccU, "email");

        HttpResponse<JsonNode> request = null;

        String baseUrl = getKey(Configuration.MAILGUN_BASEURL);
        String domain = getKey(Configuration.MAILGUN_DOMAIN);
        String apiKey = getKey(Configuration.MAILGUN_API_KEY);
        String from = getKey(Configuration.MAILGUN_FROM_SENDER);

        String url = baseUrl + "/v3/" + domain + "/messages";

        MultipartBody body = Unirest.post(url)
                .basicAuth("api", apiKey)
                .field("from", from)
                .field("to", to)
                .field("cc", cc)
                .field("subject", subject)
                .field("html", message);

        for (MyEmailAttachment mya : attachments) {
            body.field("attachment", new ByteArrayInputStream(mya.content), mya.name);
        }
                
        request = body.asJson();

        if (request.getBody().getObject().getString("id") != null) {
             for (User user : recipient) {
                logger.info("Email sent to " + (user != null ? user.getName() : "null") + (ccU != null && !ccU.isEmpty() ? " (cc to " + ccU + ")" : "") + (StringUtils.isNotEmpty(mailingList) ? " from Mailing List " + mailingList : ""));
            }
        }
        
        request.ifFailure(error -> {
            logger.error(error.getStatusText());
            if (error.getParsingError().isPresent()) {
                Exception e = error.getParsingError().get();
                logger.error(e, e);
            }
        });

    }

    private String getKey(String key) {
        return DAO.getConfigurationDao().getConfiguration(key);
    }

}

@szagr
Copy link
Collaborator

szagr commented Nov 8, 2022

@micky008 Multiple FormData attachment support was added in v1.0.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants