Postal: Allow inline attachments in send message API

Created on 29 Jan 2019  路  8Comments  路  Source: postalhq/postal

Hi!
Seems it's no way to send messages by send API with inline attachments
I would like to send message with logo, that i can refer by cid:logo in html part. According to https://sendgrid.com/blog/embedding-images-emails-facts/ it's most reliable way to embed images to email.

Of course i can send by API as raw message, but it's really hard to prepare message in such form.

AFAIK Mailer 3.0 support inline attachments, can you also add that support ?

feature request

Most helpful comment

Oh, now i see, sorry :)
It was easy for me, because i'd already send messages through SMTP from my server. I reused creation of message for SMTP and just feed result it to /send/raw method. Only trick was in create empty Mime message, because Java usually create new MimeMessage with SMTP session.
So, actual solution in Kotlin/Spring is

@Service
class PostalApiSender(private val restTemplate: RestTemplate,
                      private val postalProperties: PostalProperties) : EmailSender() {
    val logger = getLogger<PostalApiSender>()


    override fun send(emailMessage: EmailMessage): Boolean {
        val session: Session? = null
        val message = MimeMessage(session)

        try {
            val helper = MimeMessageHelper(message, true, "utf-8")

            helper.setFrom(emailMessage.from)
            helper.setTo(emailMessage.to)
            if (emailMessage.replyTo != null) {
                helper.setReplyTo(emailMessage.replyTo!!)
            }
            helper.setSubject(emailMessage.subject)
            if (emailMessage.html != null) {
                helper.setText(emailMessage.html!!, true)
            }
            if (emailMessage.logoPath != null) {
                helper.addInline("inlineLogo", ClassPathResource(emailMessage.logoPath))
            }

            val os = ByteArrayOutputStream()
            message.writeTo(os)
            val rawMessageRequest = RawMessageRequest(
                    emailMessage.from,
                    listOf(emailMessage.to),
                    Base64.getEncoder().encodeToString(os.toByteArray())
            )
            val response = restTemplate.postForObject<SendMessageResponse>("/api/v1/send/raw", rawMessageRequest)!!
            if (response.status == "error") {
                logger.error("Error sending email via Postal API", SendPostalEmailMessageException(response.data.message
                        ?: "Message not provided"))
                return false
            }
            return true
        } catch (e: Exception) {
            logger.error("Error sending message", e)
            return false
        }
    }
}

It's shortened version of my code, i have avoid some logic just to show an idea. Hope it helps somebody

All 8 comments

This would be a nice feature to have but would probably require a fair bit of rewriting to incorporate it because of the various client libraries.

Ok, got it, thanks! Will be use /send/raw instead.

No worries, if you could share your code when it is working, I'm sure it will be handy for whoever wants to add this feature.

I very want to help, but actually, i'm Java/Kotlin guy, Ruby is like petroglyphs for me.

I hear that! I meant if you're able to share how you use the /send/raw endpoint, it will be useful information even if its in a different programming language!

Oh, now i see, sorry :)
It was easy for me, because i'd already send messages through SMTP from my server. I reused creation of message for SMTP and just feed result it to /send/raw method. Only trick was in create empty Mime message, because Java usually create new MimeMessage with SMTP session.
So, actual solution in Kotlin/Spring is

@Service
class PostalApiSender(private val restTemplate: RestTemplate,
                      private val postalProperties: PostalProperties) : EmailSender() {
    val logger = getLogger<PostalApiSender>()


    override fun send(emailMessage: EmailMessage): Boolean {
        val session: Session? = null
        val message = MimeMessage(session)

        try {
            val helper = MimeMessageHelper(message, true, "utf-8")

            helper.setFrom(emailMessage.from)
            helper.setTo(emailMessage.to)
            if (emailMessage.replyTo != null) {
                helper.setReplyTo(emailMessage.replyTo!!)
            }
            helper.setSubject(emailMessage.subject)
            if (emailMessage.html != null) {
                helper.setText(emailMessage.html!!, true)
            }
            if (emailMessage.logoPath != null) {
                helper.addInline("inlineLogo", ClassPathResource(emailMessage.logoPath))
            }

            val os = ByteArrayOutputStream()
            message.writeTo(os)
            val rawMessageRequest = RawMessageRequest(
                    emailMessage.from,
                    listOf(emailMessage.to),
                    Base64.getEncoder().encodeToString(os.toByteArray())
            )
            val response = restTemplate.postForObject<SendMessageResponse>("/api/v1/send/raw", rawMessageRequest)!!
            if (response.status == "error") {
                logger.error("Error sending email via Postal API", SendPostalEmailMessageException(response.data.message
                        ?: "Message not provided"))
                return false
            }
            return true
        } catch (e: Exception) {
            logger.error("Error sending message", e)
            return false
        }
    }
}

It's shortened version of my code, i have avoid some logic just to show an idea. Hope it helps somebody

I forked an existing java client for Postal Api to add support for embedded images if anyone is interested. It's using the raw endpoint to send the message with the images added as inline attachments.
https://github.com/m4nu56/postal-java

I forked an existing java client for Postal Api to add support for embedded images if anyone is interested. It's using the raw endpoint to send the message with the images added as inline attachments.
Wow, that's pretty cool! i'll try it very soon!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

destroyer998 picture destroyer998  路  5Comments

shinebayar-g picture shinebayar-g  路  3Comments

ajsb85 picture ajsb85  路  4Comments

Igcorreia picture Igcorreia  路  4Comments

BlueHatbRit picture BlueHatbRit  路  3Comments