Home > other >  SendGrid Random Duplicate Emails (Python Flask Heroku)
SendGrid Random Duplicate Emails (Python Flask Heroku)

Time:09-08

I'm currently working on an app that updates artists with times they are booked for events. Randomly the app will send duplicate emails a few times a day, over 90% of the time there are not duplicates.

Currently, there are 10 emails that can be produced, but there is only one email send function. The duplicates occur across all emails which makes me think there is an issue with the email send function or the configuration of the web server to make multiple requests to sendgrid. PLEASE HELP ME FIND THE CAUSE OF THE DUPILCATES!

Stack:

  • Python (v3.10.6)
  • Flask (v2.2.2)
  • Heroku (buildstack-22)
  • Sendgrid python library (v6.9.7)

Email Send Function:

def send_email(to, subject, template, cc='None', attachment_location='None', attachment_name='None', private_email=False, **kwargs):

    ## Remove Guest DJ Emails Here
    guest_dj_list = get_list_of_guest_djs()
    if to in guest_dj_list:
        return None


    message = Mail(
        from_email=From('[email protected]', current_app.config['MAIL_ALIAS']),
        to_emails=[to],
        subject=subject,
        html_content=render_template(template, **kwargs))

    ## cc management
    if private_email == False:
        cc_list = ['[email protected]']
        if cc != 'None':
            cc_list.append(cc)

        message.cc = cc_list

    if attachment_location != 'None':
        with open(attachment_location, 'rb') as f:
            data = f.read()
            f.close()
        encoded_file = base64.b64encode(data).decode()

        attachedFile = Attachment(
            FileContent(encoded_file),
            FileName(attachment_name),
            FileType('application/xlsx'),
            Disposition('attachment')
        )
        message.attachment = attachedFile

    try:
        sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        response = sg.send(message)
        # print(response.status_code)
        # print(response.body)
        # print(response.headers)
        print(f'Complete: Email Sent to {to}')

    except Exception as e:
        print(e.message)

Heroku Procfile

web: gunicorn test_app.app:app --preload --log-level=debug

CodePudding user response:

According to your comments, the emails are triggered by user actions. From the code you have shared there is nothing that would cause an email to send twice, so my guess is that users are causing the occasional double sending by submitting forms more than once.

To discover the root of this, I would first attempt to disable your forms after the first submit. You can do this with a bit of JavaScript, something like:

document.querySelectorAll('form').forEach(form => {
    form.addEventListener('submit', (e) => {
        // Prevent if already submitting
        if (form.classList.contains('is-submitting')) {
            e.preventDefault();
        }
        
        // Add class to hook our visual indicator on
        form.classList.add('is-submitting');
    });
});

This comes from this article which has a good discussion of the issue too.

Once you have done that, you should likely also log the various actions that cause an email to send and try to chase back through your system to find what could be causing double submissions. I would pay attention to other things like callbacks or background jobs that may be the culprit here too.

  • Related