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

Attach PDF File stored on AWS S3 Bucket to SendGrid Email via Python (Django) #815

Closed
sgrobert opened this issue Jun 13, 2019 · 6 comments
Closed
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library

Comments

@sgrobert
Copy link

Issue Summary

Hi there, I just got on board with SendGrid for the first time and have been trying to attach a Generated PDF that is stored on an AWS S3 Bucket via URL to the SendGrid API v3. The result however, did not download the file and attach it to the email being sent.

Steps to Reproduce

I've looked at a few steps on Github namely,
a. sendgrid/sendgrid-php#480
b. sendgrid/sendgrid-php#729
c. https://github.com/sendgrid/sendgrid-python/blob/master/examples/mail/mail.py
d. https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/attachment.md


But can't seem to find a clear / working example that I may access to implement the codes to pull the AWS S3 link and attach as a PDF attachment to the email.

I've tried primary 2 steps that seem to partially work in code. But only No. 1 did attach a broken PDF and No. 2 just pops up an error that the file cannot be found, which I do trust that the code was meant for a local-file-on-server to be served.

No. 1. Served broken PDF file to Email that can't be opened

'template_id': json_data['template_id'],
'attachments': [
                {
                    'content': [base64 content pdf],
                    'content_id': json_data['attachment_name'],
                    'disposition': 'attachment',
                    'filename': json_data['attachment_filename'],
                    'name': json_data['attachment_name'],
                    'type': 'application/pdf'
                }
],

No. 2. Error thrown on server console: IOError: [Errno 2] No such file or directory:


# Where it was uploaded Path.
file_path = json_data['attachment_filename']

with open(file_path, 'rb') as f:
    data = f.read()

# Encode contents of file as Base 64
    encoded = base64.b64encode(data).decode()



'template_id': json_data['template_id'],
'attachments': [
                {
                    'content': encoded,
                    'content_id': json_data['attachment_name'],
                    'disposition': 'attachment',
                    'filename': json_data['attachment_filename'],
                    'name': json_data['attachment_name'],
                    'type': 'application/pdf'
                }
],

Any other information you want to share that is relevant to the issue being reported. Especially, why do you consider this to be a bug? What do you expect to happen instead?

Technical details:

  • sendgrid-python: sendgrid==6.0.5
  • python: 2.7

Hope someone may assist me on this as I faced a number of issues and will be posting different issue docs in hopes to address them as soon as possible. Thank you!

@thinkingserious
Copy link
Contributor

Hello @sgrobert,

My guess is that the content you are provided is not properly base64 encoded.

Here is an example that may help.

And this is the example I recommend you follow. Thanks!

With Best Regards,

Elmer

@thinkingserious thinkingserious transferred this issue from sendgrid/sendgrid-php Jun 14, 2019
@thinkingserious thinkingserious added difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library labels Jun 14, 2019
@sgrobert
Copy link
Author

sgrobert commented Jun 16, 2019

Hi @thinkingserious ,

Thank you for the swift reply. Pardon on my end as I was out of the country with no access to my laptop over the weekend.

Was looking though your code and tried testing with reference to the link provided by your (https://stackoverflow.com/questions/38408253/way-to-convert-image-straight-from-url-to-base64-without-saving-as-a-file-in-pyt/38408285#38408285), I was wondering will there be a file format conflict as I am pretty unsure of the usage of base64.

Is base64 more meant for Images or for all files? Since I am requiring to abstract a PDF file from a link via AWS S3.

I am currently still receiving an error via the python console after inserting the code as follows: IOError: [Errno 2] No such file or directory


import base64
import os
try:
    # Python 3
    import urllib.request as urllib
except ImportError:
    # Python 2
    import urllib2 as urllib

import json

from sendgrid import SendGridAPIClient


# Where it was uploaded Path.
        file_path = json_data['attachment_filename']

        with open(file_path, 'rb') as f:
            data = f.read()
            f.close()
        
        # Encode contents of file as Base 64
        encoded = base64.b64encode(data).decode()

message = {
           'attachments': [
               {
                   'content': encoded,
                   'content_id': json_data['attachment_name'],
                   'disposition': 'attachment',
                   'filename': json_data['attachment_filename'],
                   'name': json_data['attachment_name'],
                   'type': 'application/pdf'
               }
           ],
       }



try:
            sg = SendGridAPIClient(os.getenv("SENDGRID_API_KEY"))
            response = sg.send(message)
            
            print("response.status_code: %r" % response.status_code)
            print("response.body: %r" % response.body)
            print("response.headers: %r" % response.headers)

            
        except Exception as e:
            print("Exceptions: %r" % e.message)

I've double-checked on the codes but can't seem to find any differences from the suggested code provided in (https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/attachment.md). Not sure if you may help to better rectify where I might have gone wrong.

Thank you!

@sgrobert
Copy link
Author

Hi @thinkingserious ,

Just to add a point that may be helpful to debugging.

The current error being thrown (IOError: [Errno 2] No such file or directory), was being caused by this line: in sendmail
with open(file_path, 'rb') as f


I can open the PDF file in incognito mode on Chrome, so security approval should not be a key issue being bumped at the moment.

So hope this is will in the debugging.

@sgrobert
Copy link
Author

Hi @thinkingserious ,

Guess I finally found the solution to the problem.

The code you provided I do believe will work essentially. But I have a lingering feel that the problem may lie with potential security encoding with AWS S3 Buckets' files.

Thus I have to sought a new search to identify potential pockets of solution as AWS is quite infamous for not having very easily-helpful documentations.

Long story short:

  1. Sought using the Boto3 API: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.get_object

  2. Utilize the code example: https://docs.aws.amazon.com/code-samples/latest/catalog/python-s3-get_object.py.html

  3. Punch in the right credentials for the S3.client

  4. Connect the stream data file back to your encoded code & it works!


Appreciate the support on this issue thus far! ;)

@thinkingserious
Copy link
Contributor

Thanks for taking the time to report back @sgrobert, we appreciate it!

@sgrobert
Copy link
Author

You welcome @thinkingserious! Thanks for offering your assistance in the first place! Really appreciate it as well. Cheers! ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library
Projects
None yet
Development

No branches or pull requests

2 participants