Sendgrid-php: Custom Header for Attachments

Created on 15 Jun 2016  Â·  89Comments  Â·  Source: sendgrid/sendgrid-php

I'm having an issue with needing to add custom headers for attachments to an email. I'm generating and attaching calendar appointments (meeting requests) and sending them to the users of my web application. It works perfectly for Gmail users. However, for those who are using Outlook, it does not behave as expected. After some research (and finding a related issue on the nodejs Sendgrid library) it appears that Outlook expects something additional in the header with the attachment. This is the header generated by Outlook for a meeting invite:

Content-Type: text/calendar; charset="utf-8"; method=REQUEST

and this is what I can get from SendGrid:

Content-Type: text/calendar; charset=utf-8; name="1187.ics"

It appears that, in order for me to get it to behave properly, I have to add method=REQUEST to the Content-Type header on the attachment alone. (Having the attachment name does not seem like it's going to be an issue.)

I'm currently using the v2 library. The nodejs ticket referenced above says this isn't going to be an issue with v3, but after looking at the (just published) v3 for php, I don't see the ability to change the header beyond setting the content type. Is this something that can be added or am I just missing the fact that this is doable in some other way? Thanks!

unknown or a help wanted non-library issue

Most helpful comment

Hey @here, I just want you to know that I have been talking with the engineering team who owns this part of the code. We are working on getting this change made. It won't be deployed this quarter because it is our customer's busiest time of the year (Holiday shopping). I am actively working to get this done and I apologize that it is not done yet - as this was one of the main reasons for allowing custom content-types on the v3/mail/send API endpoint!

Please let me know if you have other feedback about anything SendGrid, our API, or our libraries. As the Developer Experience Product Manager, it is my job to be talking to customers about what we do and finding ways to make their experience better. I want to understand what you're doing with our API, how you're doing it, what you liked, what you don't like, and what would make your job easier. So, if you want to chat - please feel free to grab a time on my calendar - https://calendly.com/matt-bernier/30min

All 89 comments

Thanks for the question @jfade, we will take a look and see if we have a solution for you.

Thank you! I'm hopeful we find a solution, as I much prefer SendGrid to the other services I've looked at and used. I'll keep an eye out for an update from you. Appreciate it!

Hello @jfade,

Have you given this a try using the updated library?

You can try over-riding the header like this: https://github.com/sendgrid/sendgrid-php/blob/master/test/unit/SendGridTest.php#L1075

Do you have a code snippet on how that would work in the new library?

I'm getting this error when I try it:

The attachment type cannot contain ';', or CRLF characters.

My code is:

$attachment->setType('text/calendar; charset="UTF-8"; method=REQUEST');

@chipallen2,

I have not been able to get this to work either. I've put in a ticket with the API team.

Also, I added your vote to this issue to help move it up the queue.

Thanks!

@chipallen2, @thinkingserious, sorry for not participating sooner. However, I did end up getting a workaround figured out that is not ideal, but let me proceed immediately with getting things working as I needed to. It wasn't quite what I originally wanted, but it DOES work. Maybe this will help you, @chipallen2.

It did require another library, however, so I would still like for the method in the Sendgrid PHP library to be updated. Having to use and maintain additional libraries is not something I'd like to deal with long term.

I utilized the PHPMailer library: https://github.com/PHPMailer/PHPMailer

From there, I was able to create and use SendGrid SMTP credentials to send the email like so:

$message = file_get_contents('/path/to/calendarfile.ics');
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;                               // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.sendgrid.net';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'XXX';                 // SMTP username
$mail->Password = 'XXX';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
$mail->setFrom('[email protected]', 'Fake Company Meetings');
$mail->addAddress($apptWith);     // Add a recipient, set earlier
$mail->isHTML(false);                                  // Set email format as NOT HTML
$mail->Subject = $subject; //Subject variable, declared earlier
$mail->Body    = $message; //Message variable, set earlier
$mail->ContentType = 'text/calendar; method=REQUEST'; //It appears that we can just set whatever here, just leave the trailing ; off as that's added by the library... I do wonder if this is technically unexpected behavior that the developers of PHPMailer wouldn't want, but it works, so I won't complain.

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

It isn't quite perfect, and it's not quite the original method I wanted that more closely mirrors what Outlook actually does in most cases. However, in some instances, Outlook will send what would've been the .ics attachment contents as the body of the email (and setting the content type of the email) instead of attaching the file and setting its content type.

Hopefully that's clear enough, and hopefully it may help... But again, it would be nice if the $attachment->setType() method would allow for the content type to be set as needed for the more detailed calendar invite message.

Thanks for sharing a workaround!

Can we please send you some swag? Just email us at [email protected] with your mailing address and T-shirt size.

Are there any updates on this? I ran into same issue with sendgrid-python:

The attachment type cannot contain ';', or CRLF characters.

It would be great if we could use the ; character in set_type() or have a method allowing to append method=REQUEST to Content-Type.

Ah @MihailRussu, I just responded to you on another thread. currently this issue is currently on our backlog and we have added your vote to help increase the priority for a solution.

We are now running into this issue as well. The Content-Type header should either accept a charset attribute being set, or (if it's feasible), the API should be able to detect the necessary encoding type and specify it internally. (An encoding guru would probably need to chime in as to whether or not this is feasible.)

Thanks for the additional vote and feedback on this issue @winzig,

I've sent your feedback to the team.

Hello @thinkingserious ,

I guess everyone is still waiting for a solution, so am I !
I'd also like to set method to REQUEST and encounter the problem.

Do you have an update ? It's been 2 months !

Thanks,
Have a nice day,
Jérémy

Hi @jlanglais,

At this point, it's a matter of prioritization.

I thank you for adding your vote to the issue and I've passed it along to the team.

Hey @thinkingserious ,

I am still locked due to this problem !
Do you have any update ? Any solution ?

Thanks,
Jérémy

@ataraxido,

Unfortunately, I don't have any update or solution.

I'll make another request for a status update.

@thinkingserious should I consider using another email provider since you can't solve a blocking issue within 6 months ?

@ataraxido,

Unfortunately, I still don't have an update on this one other than it is currently in the engineering backlog. If we get more people requesting this feature, that could help get this issue a higher priority.

+1 for supporting Outlook (using the PHP SDK). I've had multiple clients forced into alternative providers/clients due to this.

I've had no luck finding a workaround.

Again @thinkingserious do you plan to solve this soon ?
We really need it !

@ataraxido,

At this point the only thing that can help move this item up the priority queue is more requests.

I think it would be great to have this working and I'll do what I can to continue to advocate the prioritization of this issue.

@thinkingserious
I jumped in here since it is relevant to this thread, and I'd like to request help with this as well. My attachments are now working, however, I have encounter issues with attaching .ics files.

Definitely would be great to have this working!

:+1:

Thanks @paoga87!

I've added your vote to the issue.

I am facing the problem even for gmail also. It is sending my ical code as it is and not parsing it to convert to calendar invitation. I used V2 web API initially, than upgraded to v3 web API, than a support guy told me that i-calendar can be sent through SMPT only. I used it but not fixed my problem. I am using codeigniter framework and sending emails using its email class.

Same issue here, any update?..

@sxmichael,

This issue is currently working it's way up the backlog. I've added your vote to expedite. Thanks!

I came here from the node.js project thread: https://github.com/sendgrid/sendgrid-nodejs/issues/217.

Add my vote, too. I've got quite a bit of feature pressure here. We're on a Pro 300k level account, and if no solution is in sight I'll have to look at a different provider within the next couple of weeks.

Thank you @pascalopitz! Your vote has been added.

This is pretty high in the list, but all the feedback is welcome and appreciated. We will update as soon as we have any information to share. Please if you come across this thread and need this feature, continue to let us know. The count of people asking for a feature is very important to us.

Hey there,

Not sure if I need to comment to get to vote, but I also need this feature to send proper iCalendar event.

Thanks,

@mguillet,

Your vote is counted :)

Hi there,
I am also facing same problem. when sending inline i-calendar event it simply prints the i-cal code instead of parsing it to event invitation. Sending email using simple PHP mail() function and every thing is working fine but when applied sendgrid API it doesn't works at all.
Please add my vote too.
Thanks.

Thanks @aammjad

Add my vote please.

Thanks.

Same here! Thanks!

@Dalrev, @robinlambert,

Your votes have been added, thanks!

Same issue here, thanks for your job @SendGridDX !

Could you add mine too? We really need this.

Please prioritize this, thank you!

Hello guys, same issue here. Do you have an ETA or a # rank to share about this on your backlog ? Thank you !

Need this too... Thanks!

+1 We need this ASAP :)

What's news on this ?

I am also waiting for solution of this bug. I think they just forgot this bug.

Looks like it for sure. Actively looking for alternatives now.

Was integrating Sendgrid, but, since this is still an issue, look like I have to look for alternatives.

Thanks to everyone adding their vote to this issue.

Your vote has been added to this item in our backlog.

Up-voting this issue to be fixed please. We really want to use SendGrid as our platform but are being forced to look at other providers instead due to this critical issue.

Thanks @Productmind, we appreciate you taking the time to add your voice.

+1 for this

Thanks @YuriyPavlishin, your vote has been counted.

+1. This is really holding us back at the moment. Loving sendgrid, but this is a major kink. Would be a shame to have to switch providers over something like this. Please implement!

+1 Need this too.

I need it too, thanks!

Thanks for the votes @NothingSethual, @DimitrisNL and @litofunes!

@jlanglais, thank you for the call. If you would like, please go to https://dx.sendgrid.com/swag and we will send you a t-shirt for your time!

Hey @here, I just want you to know that I have been talking with the engineering team who owns this part of the code. We are working on getting this change made. It won't be deployed this quarter because it is our customer's busiest time of the year (Holiday shopping). I am actively working to get this done and I apologize that it is not done yet - as this was one of the main reasons for allowing custom content-types on the v3/mail/send API endpoint!

Please let me know if you have other feedback about anything SendGrid, our API, or our libraries. As the Developer Experience Product Manager, it is my job to be talking to customers about what we do and finding ways to make their experience better. I want to understand what you're doing with our API, how you're doing it, what you liked, what you don't like, and what would make your job easier. So, if you want to chat - please feel free to grab a time on my calendar - https://calendly.com/matt-bernier/30min

+1 vote for this.

Thanks for the vote @ledjon!

+1 vote for this

Could we have an update on this issue ?

Yes it would be very appreciated !

@mbernier Any update on your end? We're making our decision currently on transitioning our transactional email service and would prefer to remain with SendGrid but this has been major roadblock for our customers needs.

Hi @Productmind,

I've put in a query and I'll update you when I learn more. Thanks!

@thinkingserious any update here? We're in the process of transitioning away from Sendgrid if we can't sort out how to attach ICS emails. Unfortunately, it's becoming crucial to our business needs, despite loving sendgrid.

Same thing here!

Thanks for adding your voice to the issue @NothingSethual and @robinlambert! I've passed along your feedback to our product team.

With Best Regards,

Elmer

I can see this thread going on for more than a year with no clear timeline for fixing the issue!
So what's the point of voting?

@amrsawy I agree. Our organization switched to Amazon's SES because of this problem. It has it's own restrictions and trade-offs but this worked correctly and was critical to our business needs.

Thanks for the votes @amrsawy and @ledjon!

I'm working to get this issue visibility and implementation internally. Each time I get a new vote or comment, that allows me to increase that visibility and hopefully push it up our priority queue.

Need this feature too but it seems I've hit the same roadblock.

Has this not yet been resolved?

Hello @sugoireed,

No, this feature is not get been implemented; however, your vote has been added. Thanks!

Please resolve this.

Thanks for the vote @baynera.

I need this, too.

Please resolve this for NodeJS

Thanks for the votes @tedterrill and @abhilashabhatia!

@thinkingserious, how many votes do we need to get some action on this? Know you guys are busy but dang, it's been two years!

Hi @NothingSethual,

Unfortunately, I don't have a concrete answer to that. But, I can say that each vote helps and I will use your comment as one more means to help get this prioritized.

With Best Regards,

Elmer

We also need this. Really unfortunate that it's not possible at the moment. Some sort of timeline would be great. Seems like low hanging fruit to allow the necessary additional characters.

Hello @brianmuse,

Thanks for adding your voice to this thread. As soon as I get any kind of timeline internally, I will post it here.

With Best Regards,

Elmer

Hello Everyone!

I'm SUPER EXCITED to announce that this issue has been resolved and the fix is live in production! Please give it a try and let me know if you run into any further issue.

Thank you for your patience on this one!

With Best Regards,

Elmer

Hello @thinkingserious

I just tried and I still get an error ( using the API ) :

{"errors":[{"message":"The attachment type cannot contain ';', or CRLF characters.","field":"attachments.0.type","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.attachments.type"},{"message":"The attachment type cannot contain ';', or CRLF characters.","field":"attachments.1.type","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.attachments.type"}]}

Here is what I am doing using the API API :

$attachment->setType('text/calendar; charset="UTF-8"; method=REQUEST');

Are you sure the issue is solved ? What should we do ?

Thanks

Hi Elmer,

Do you know if there’s a GitHub request open for conditional formatting support in SendGrid emails?

All the best,
Michael

From: Elmer Thomas notifications@github.com
Reply-To: sendgrid/sendgrid-php reply@reply.github.com
Date: Monday, June 11, 2018 at 5:05 PM
To: sendgrid/sendgrid-php sendgrid-php@noreply.github.com
Cc: Michael Buckley michael@eventsthatflow.com, Mention mention@noreply.github.com
Subject: Re: [sendgrid/sendgrid-php] Custom Header for Attachments (#250)

Hello Everyone!

I'm SUPER EXCITED to announce that this issue has been resolved and the fix is live in production! Please give it a try and let me know if you run into any further issue.

Thank you for your patience on this one!

With Best Regards,

Elmer

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/sendgrid/sendgrid-php/issues/250#issuecomment-396423972, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AZgmokiP_FSr03omnOJ0nI--zhx9eANOks5t7wWngaJpZM4I2iFB.

@ataraxido,

Could you please try? $attachment->setType('text/calendar; method=REQUEST');

Thanks!

I'm still getting this error on v3 api, I thought this was fixed already?

FAILED java.io.IOException: Request returned status Code 400Body:{"errors":[{"message":"The content type cannot contain ';', or CRLF characters.","field":"content.1.type","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content.type"}]}

Hi @stefanbacon,

We added this functionality for attachment headers.

What is the end result you are looking for?

With Best Regards,

Elmer

@thinkingserious I'm having the same problem as the ones before; the SendGrid Mail v3 API rejects attachments with type containing ';':

Failed to send e-mail! JSON: {"errors":[{"message":"The attachment type cannot contain ';', or CRLF characters.","field":"attachments.0.type","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.attachments.type"}]}

I was trying to send an iCalendar (.ics) file with type set to "text/calendar; charset=\"UTF-8\"; method=REQUEST" as mentioned earlier in this thread.

Edit: apparently this specific issue is being discussed in this thread: https://github.com/sendgrid/sendgrid-nodejs/issues/217. To summarise: setting the attachment type to "text/calendar; method=REQUEST" (without the charset) works, however, the iCalendar file is not appearing in Outlook as a calendar invite.

To summarise: setting the attachment type to "text/calendar; method=REQUEST" (without the charset) works, however, the iCalendar file is not appearing in Outlook as a calendar invite.

It works for me with Outlook 16.17 on Mac.

I was also able to send calendar events and get them parsed by gmail with:

$mail->addAttachment(
                    new \SendGrid\Mail\Attachment(
                        base64_encode('ICS_CONTENT_HERE'),
                        "text/calendar; method=REQUEST",
                        "invite.ics",
                        "attachment",
                        "attachment"
                    )
                );
Was this page helpful?
0 / 5 - 0 ratings

Related issues

elshafey picture elshafey  Â·  4Comments

iamanupammaity picture iamanupammaity  Â·  4Comments

morazain picture morazain  Â·  3Comments

bjornmann picture bjornmann  Â·  3Comments

jverlee picture jverlee  Â·  4Comments