use of org.killbill.billing.util.email.EmailApiException in project killbill by killbill.
the class EmailInvoiceNotifier method notify.
@Override
public void notify(final Account account, final Invoice invoice, final TenantContext context) throws InvoiceApiException {
if (Strings.emptyToNull(account.getEmail()) == null) {
throw new InvoiceApiException(new IllegalArgumentException("Email for account " + account.getId() + " not specified"), ErrorCode.EMAIL_SENDING_FAILED);
}
final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(account.getId(), context);
final List<String> to = new ArrayList<String>();
to.add(account.getEmail());
final List<AccountEmail> accountEmailList = accountApi.getEmails(account.getId(), internalTenantContext);
final List<String> cc = new ArrayList<String>();
for (final AccountEmail email : accountEmailList) {
cc.add(email.getEmail());
}
// Check if this account has the MANUAL_PAY system tag
boolean manualPay = false;
final List<Tag> accountTags = tagUserApi.getTags(account.getId(), ObjectType.ACCOUNT, internalTenantContext);
for (final Tag tag : accountTags) {
if (ControlTagType.MANUAL_PAY.getId().equals(tag.getTagDefinitionId())) {
manualPay = true;
break;
}
}
final HtmlInvoice htmlInvoice;
try {
htmlInvoice = generator.generateInvoice(account, invoice, manualPay, internalTenantContext);
} catch (final IOException e) {
throw new InvoiceApiException(e, ErrorCode.EMAIL_SENDING_FAILED);
}
// take localized subject, or the configured one if the localized one is not available
String subject = htmlInvoice.getSubject();
if (subject == null) {
subject = config.getInvoiceEmailSubject();
}
final EmailSender sender = new DefaultEmailSender(config);
try {
sender.sendHTMLEmail(to, cc, subject, htmlInvoice.getBody());
} catch (final EmailApiException e) {
throw new InvoiceApiException(e, ErrorCode.EMAIL_SENDING_FAILED);
} catch (final IOException e) {
throw new InvoiceApiException(e, ErrorCode.EMAIL_SENDING_FAILED);
}
}
use of org.killbill.billing.util.email.EmailApiException in project killbill by killbill.
the class OverdueStateApplicator method sendEmailIfRequired.
private void sendEmailIfRequired(final UUID accountId, final BillingState billingState, final OverdueState nextOverdueState, final InternalTenantContext context) throws AccountApiException {
// If sending is not configured, skip
if (nextOverdueState.getEmailNotification() == null) {
return;
}
final Account account = accountApi.getAccountById(accountId, context);
if (Strings.emptyToNull(account.getEmail()) == null) {
log.warn("Unable to send overdue notification email for account {} and overdueable {}: no email specified", account.getId(), account.getId());
return;
}
final List<String> to = ImmutableList.<String>of(account.getEmail());
// TODO - should we look at the account CC: list?
final List<String> cc = ImmutableList.<String>of();
final String subject = nextOverdueState.getEmailNotification().getSubject();
try {
// Generate and send the email
final String emailBody = overdueEmailGenerator.generateEmail(account, billingState, account, nextOverdueState);
if (nextOverdueState.getEmailNotification().isHTML()) {
emailSender.sendHTMLEmail(to, cc, subject, emailBody);
} else {
emailSender.sendPlainTextEmail(to, cc, subject, emailBody);
}
} catch (final IOException e) {
log.warn("Unable to generate or send overdue notification email for accountId='{}'", account.getId(), e);
} catch (final EmailApiException e) {
log.warn("Unable to send overdue notification email for accountId='{}'", account.getId(), e);
} catch (final MustacheException e) {
log.warn("Unable to generate overdue notification email for accountId='{}'", account.getId(), e);
}
}
Aggregations