Skip to content

Instantly share code, notes, and snippets.

@AledLewis
Created March 28, 2018 19:43
Show Gist options
  • Save AledLewis/740968ff68e3b5351a6892af79a97819 to your computer and use it in GitHub Desktop.
Save AledLewis/740968ff68e3b5351a6892af79a97819 to your computer and use it in GitHub Desktop.
EuSparkPost hack (until Java client support EU Sparkpost)
import com.sparkpost.Client;
import com.sparkpost.exception.SparkPostException;
import com.sparkpost.model.AddressAttributes;
import com.sparkpost.model.OptionsAttributes;
import com.sparkpost.model.RecipientAttributes;
import com.sparkpost.model.TemplateContentAttributes;
import com.sparkpost.model.TransmissionWithRecipientArray;
import com.sparkpost.model.responses.Response;
import com.sparkpost.resources.ResourceTransmissions;
import com.sparkpost.transport.IRestConnection;
import com.sparkpost.transport.RestConnection;
import java.util.ArrayList;
import java.util.List;
public class EuSparkPostClient extends Client {
public EuSparkPostClient(String sparkPostApiKey) {
super(sparkPostApiKey);
}
@Override
public Response sendMessage(String from, List<String> recipients, String subject, String text, String html) throws SparkPostException {
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
for (String recipient : recipients) {
RecipientAttributes recipientAttribs = new RecipientAttributes();
recipientAttribs.setAddress(new AddressAttributes(recipient));
recipientArray.add(recipientAttribs);
}
transmission.setRecipientArray(recipientArray);
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
contentAttributes.setFrom(new AddressAttributes(from));
contentAttributes.setSubject(subject);
contentAttributes.setHtml(html);
contentAttributes.setText(text);
transmission.setContentAttributes(contentAttributes);
if (from.toLowerCase().contains("@sparkpostbox.com")) {
OptionsAttributes options = new OptionsAttributes();
options.setSandbox(true);
transmission.setOptions(options);
}
IRestConnection connection = new RestConnection(this, "https://api.eu.sparkpost.com/api/v1");
Response response = ResourceTransmissions.create(connection, 0, transmission);
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment