Пример #1
0
def sendExperimentResults(config):
    try:
        from sendgrid import SendGridAPIClient
        from sendgrid.helpers.mail import (Mail, Attachment, FileContent,
                                           FileName, FileType, Disposition,
                                           ContentId)

        message = Mail(
            from_email='*****@*****.**',
            to_emails='*****@*****.**',
            subject=f'Experiment results: {config.configurationDirectory}',
            html_content=
            f'These are your results for {config.configurationDirectory}. Please see the attached charts.'
        )

        for file in config.listAllFilesInFolder("charts"):
            data = config.loadKwolaFileData("charts", file)
            encoded = base64.b64encode(data).decode()
            attachment = Attachment()
            attachment.file_content = FileContent(encoded)
            attachment.file_type = FileType('image/png')
            attachment.file_name = FileName(file)
            attachment.disposition = Disposition('attachment')
            attachment.content_id = ContentId(file)
            message.add_attachment(attachment)

        videoFiles = config.listAllFilesInFolder("debug_videos")
        videoFiles = sorted(
            videoFiles,
            key=lambda fileName: pathlib.Path(
                os.path.join(config.configurationDirectory, "debug_videos",
                             fileName)).stat().st_mtime,
            reverse=True)
        for file in videoFiles[:2]:
            data = config.loadKwolaFileData("debug_videos", file)
            encoded = base64.b64encode(data).decode()
            attachment = Attachment()
            attachment.file_content = FileContent(encoded)
            attachment.file_type = FileType('video/mpeg')
            attachment.file_name = FileName(file)
            attachment.disposition = Disposition('attachment')
            attachment.content_id = ContentId(file)
            message.add_attachment(attachment)

        if 'sendgrid_api_key' in config:
            apiKey = config['sendgrid_api_key']
        elif 'SENDGRID_API_KEY' in os.environ:
            apiKey = os.environ.get('SENDGRID_API_KEY')
        else:
            getLogger().error(
                "There was no API key provided for Sendgrid. Please set sendgrid_api_key within your config.json file."
            )
            return

        sg = SendGridAPIClient(apiKey)
        response = sg.send(message)
    except Exception:
        getLogger().error(traceback.format_exc())
Пример #2
0
def send_email(email_to):
    message = Mail(
        from_email='*****@*****.**',
        to_emails=email_to,
        subject='Crypto Investment CSV File',
        html_content='<strong>Here is the file you requested.</strong>')

    file_path = os.path.join(os.getcwd(), "trades.csv")
    with open(file_path, 'rb') as f:
        data = f.read()
        f.close()

    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('application/csv')
    attachment.file_name = FileName('trades.csv')
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Example Content ID')
    message.attachment = attachment
    try:
        sg = SendGridAPIClient(
            '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'
        )
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
        return response.status_code
    except Exception as e:
        print(e.message)
Пример #3
0
def build_message(from_email: str, to_emails: tuple, subject: str,
                  html_content: str, file_name: str):

    message = Mail(
        from_email=from_email,
        to_emails=to_emails,
        subject=subject,
        html_content=html_content
    )

    if os.path.isfile(file_name):

        with open(file=file_name, mode='rb') as f:
            data = f.read()
            f.close()

        encoded = base64.b64encode(data).decode()

        attachment = Attachment()
        attachment.file_content = FileContent(encoded)
        attachment.file_type = FileType('application/html')
        attachment.file_name = FileName('Music_Recommendations.html')
        attachment.disposition = Disposition('attachment')

        message.attachment = attachment

    return message
Пример #4
0
    def sendgrid_attachments(self, bodyobj):
        to_emails = self.transform_to_emails()
        message = Mail(from_email=('<email_change>', '<name_change>'),
                       to_emails=to_emails,
                       subject=self.subject,
                       html_content=bodyobj['body'])

        # attachments
        attchments = []
        for fle in bodyobj['logfiles']:
            if fle == 'None':
                continue
            else:

                with open(fle, 'rb') as f:
                    dta = f.read()
                    fname = os.path.basename(f.name)
                encoded = base64.b64encode(dta).decode()
                attachment = Attachment()
                attachment.file_content = FileContent(encoded)
                # text/plain text/comma-separated-values
                attachment.file_type = FileType('text/plain')
                attachment.file_name = FileName(fname)
                attachment.disposition = Disposition('attachment')
                attachment.content_id = ContentId('Example Content ID')
                attchments.append(attachment)
        message.attachment = attchments
        try:
            sendgrid_client = SendGridAPIClient(self.apikey)
            response = sendgrid_client.send(message)
            print(response.status_code)
            print(response.body)
            print(response.headers)
        except Exception as e:
            print(e)
    def send_email(self, email, pdf_path):
        message = Mail(
            from_email="Aditya Raman <*****@*****.**>",
            to_emails=email,
            subject="[MSP Testing] Testing for Attendee's Certificate",
            html_content=" Sending the attachment<br>",
        )

        file_path = pdf_path
        file_name = list(file_path.split("/"))[-1]
        with open(file_path, "rb") as f:
            data = f.read()
            f.close()
        encoded = base64.b64encode(data).decode()
        attachment = Attachment()
        attachment.file_content = FileContent(encoded)
        attachment.file_type = FileType(
            "application/pdf")  # FileType("image/png")
        attachment.file_name = FileName(file_name)
        attachment.disposition = Disposition("attachment")
        attachment.content_id = ContentId("Microsoft Student Partner")
        message.attachment = attachment
        try:
            sg = SendGridAPIClient(settings.SENDGRID_API_KEY)
            response = sg.send(message)
            print(response.status_code)
            print(response.body)
            print(response.headers)
            return response
        except Exception as e:
            print(e)
            return e
Пример #6
0
def send_styled_email(to_email):
    load_dotenv(dotenv_path='sendgrid.env')

    message = Mail(
        from_email='*****@*****.**',
        to_emails=to_email,
        subject='Here is your Stylized image!',
        html_content=
        '<strong>and easy to do anywhere, even with Python</strong>')
    file_path = 'output.jpg'
    with open(file_path, 'rb') as f:
        data = f.read()
        f.close()
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('application/jpg')
    attachment.file_name = FileName('output.jpg')
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Example Content ID')
    message.attachment = attachment
    try:
        sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        response = sendgrid_client.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(e)
Пример #7
0
def send_email_task_sendgrid(payload, headers, smtp_config):
    try:
        message = Mail(from_email=From(payload['from'], payload['fromname']),
                       to_emails=payload['to'],
                       subject=payload['subject'],
                       html_content=payload["html"])
        if payload['attachments'] is not None:
            for attachment in payload['attachments']:
                with open(attachment, 'rb') as f:
                    file_data = f.read()
                    f.close()
                encoded = base64.b64encode(file_data).decode()
                attachment = Attachment()
                attachment.file_content = FileContent(encoded)
                attachment.file_type = FileType('application/pdf')
                attachment.file_name = FileName(payload['to'])
                attachment.disposition = Disposition('attachment')
                message.add_attachment(attachment)
        sendgrid_client = SendGridAPIClient(get_settings()['sendgrid_key'])
        logging.info('Sending an email regarding {} on behalf of {}'.format(
            payload["subject"], payload["from"]))
        sendgrid_client.send(message)
        logging.info('Email sent successfully')
    except urllib.error.HTTPError as e:
        if e.code == 429:
            logging.warning("Sendgrid quota has exceeded")
            send_email_task_smtp.delay(payload=payload,
                                       headers=None,
                                       smtp_config=smtp_config)
        elif e.code == 554:
            empty_attachments_send(sendgrid_client, message)
        else:
            logging.exception(
                "The following error has occurred with sendgrid-{}".format(
                    str(e)))
Пример #8
0
def send_email(subject="Workout",
               html="<p>Today's Workout</p>",
               pdf="workout.pdf"):
    client = SendGridAPIClient(
        SENDGRID_API_KEY)  #> <class 'sendgrid.sendgrid.SendGridAPIClient>
    message = Mail(from_email=MY_EMAIL,
                   to_emails=MY_EMAIL,
                   subject=subject,
                   html_content=html)
    #attaches the PDF we generated earlier
    file_path = 'workout.pdf'
    with open(file_path, 'rb') as f:
        data = f.read()
        f.close()
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('application/pdf')
    attachment.file_name = FileName('JuicyLiftWorkout.pdf')
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Example Content ID')
    message.attachment = attachment
    #send email
    try:
        response = client.send(message)
        return response
    except Exception as e:
        print("OOPS", e.message)
        return None
Пример #9
0
def email_with_html_attachment(from_email: str, to_email: str, subject: str,
                               body: str, filename: str, file_path: str):
    import base64
    from apis import authentication
    from sendgrid.helpers.mail import (Mail, Attachment, FileContent, FileName,
                                       FileType, Disposition)
    from sendgrid import SendGridAPIClient

    message = Mail(from_email=from_email,
                   to_emails=to_email,
                   subject=subject,
                   html_content=body)
    with open(file_path, 'rb') as f:
        data = f.read()
        f.close()
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('text/html')
    attachment.file_name = FileName(filename)
    attachment.disposition = Disposition('attachment')
    message.attachment = attachment
    try:
        sendgrid_client = SendGridAPIClient(
            authentication.get_token('https://www.apitutor.org/sendgrid/key'))
        sendgrid_client.send(message)
        return True
    except:
        return False
Пример #10
0
def EmergencyMail(mail_subject, html_content, file_path):
    """This function utilizes SendGrid Api to send emergcency signals as email
    to the necessary agencies"""
    message = Mail(from_email=current_app.config['APP_EMAIL'],
                   to_emails=current_app.config['AGENT_EMAILS'].split(' '),
                   subject=mail_subject,
                   html_content=html_content)

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

    # encode file
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('application/xls')
    attachment.file_name = FileName('data.xls')
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('PatientData')
    message.attachment = attachment
    try:
        sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        resp = sg.send(message)
        return True
    except HTTPError as e:
        #print(f"{resp.status_code}'\n'{resp.body}'\n'{resp.headers}")
        print(e.to_dict)
        return False
    else:
        print(e.to_dict)
        return False
Пример #11
0
def send(filename, job, city, email_receiver, sendgrid_api_key,
         sendgrid_email_sender):
    try:
        sg = sendgrid.SendGridAPIClient(api_key=sendgrid_api_key)

        message = Mail(
            from_email=sendgrid_email_sender,
            to_emails=email_receiver.replace(',', ''),
            subject=f"LinkedIn: {job} jobs in {city} (Weekly).",
            html_content=
            f"Automated report for {job} jobs in {city} generated on {datetime.datetime.now()}."
        )

        with open(filename, 'rb') as f:
            data = f.read()
            f.close()
        encoded = base64.b64encode(data).decode()
        attachment = Attachment()
        attachment.file_content = FileContent(encoded)
        attachment.file_type = FileType('application/csv')
        attachment.file_name = FileName(filename)
        attachment.disposition = Disposition('attachment')
        attachment.content_id = ContentId('')
        message.attachment = attachment

        response = sg.client.mail.send.post(request_body=message.get())

        print(f"\n\033[1m📩 Email sent successfully to {email_receiver}\033[0m")

    except Exception as e:
        print("Error:", e)
        print(
            "\n\033[1m❌ An error occurred while trying to send the email!\033[0m"
        )
Пример #12
0
def generate_attachment(filepath, filetype="application/pdf", filename=None):
    """given a filepath, generate an attachment object for SendGrid by reading
       it in and encoding in base64.
 
       Parameters
       ==========
       filepath: the file path to attach on the server.
       filetype: MIME content type (defaults to application/pdf)
       filename: a filename for the attachment (defaults to basename provided)
    """
    if not os.path.exists(filepath):
        return

    # Read in the attachment, base64 encode it
    with open(filepath, "rb") as filey:
        data = filey.read()

    # The filename can be provided, or the basename of actual file
    if not filename:
        filename = os.path.basename(filepath)

    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType(filetype)
    attachment.file_name = FileName(filename)
    attachment.disposition = Disposition("attachment")
    return attachment
Пример #13
0
def send_email(subject="Unemployment Data",
               html="<p>Unemployment Data</p>",
               png=image_name):
    client = SendGridAPIClient(
        SENDGRID_API_KEY)  #> <class 'sendgrid.sendgrid.SendGridAPIClient>
    message = Mail(from_email=MY_EMAIL,
                   to_emails=MY_EMAIL,
                   subject=subject,
                   html_content=html)

    #Attaches the PNG we Generated Earlier Using Selenium

    with open(image_name, 'rb') as f:
        image = f.read()
        f.close()

    image_encoded = base64.b64encode(image).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(image_encoded)
    attachment.file_type = FileType('image/png')
    attachment.file_name = FileName('unemployment_rate.png')
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Example Content ID')
    message.attachment = attachment

    #Send Email
    try:
        response = client.send(message)
        print(response.status_code)
        print(response.body)
        return response

    except Exception as e:
        print("OOPS", e.message)
        return None
Пример #14
0
def send_email(email_to):
    message = Mail(
        from_email = '*****@*****.**',
        to_emails = email_to,
        subject='Mango Investment CSV File',
        html_content='<strong>Here is the file you requested.</strong>')

    file_path = os.path.join(os.getcwd(), "trades.csv")
    with open(file_path, 'rb') as f:
        data = f.read()
        f.close()
        
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('application/csv')
    attachment.file_name = FileName('trades.csv')
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Example Content ID')
    message.attachment = attachment
    try:
        sg = SendGridAPIClient('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')
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
        return response.status_code
    except Exception as e:
        print(e.message)
Пример #15
0
def send_simple_mail(subj='Sending with Twilio SendGrid is Fun'):
    message = Mail(
        from_email='*****@*****.**',
        to_emails='*****@*****.**',
        subject=subj,
        html_content=
        '<strong>and easy to do anywhere, even with Python</strong>')

    import os
    THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
    file_path = os.path.join(THIS_FOLDER, 'bumblebees-flowers.jpg')
    # file_path = 'bumblebees-flowers.jpg'
    with open(file_path, 'rb') as f:
        data = f.read()
        f.close()
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('application/jpeg')
    attachment.file_name = FileName('bumblebees-flowers.jpg')
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Example Content ID')
    message.attachment = attachment

    try:
        sg = SendGridAPIClient(SENDGRID_API_KEY)
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(e.message)
Пример #16
0
def sendMailWithAttachment(context):
    # print(context)
    html_version = 'email/email.html'
    message = Mail(
        from_email='*****@*****.**',
        to_emails=context['emails'],
        subject="Reg: Payment remittance for {} ${} {} Vendor : {}".format(
            context["clearing_date"], context["amount"], context["currency"],
            context["supplier"]),
        html_content=render_to_string(html_version, {'context': context}))
    with open(context['attachment'], 'rb') as f:
        data = f.read()
        f.close()
    print(data)
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('text/csv')
    attachment.file_name = FileName(context['attachment'])
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('REMITTANCE')
    message.add_attachment(attachment)
    try:
        sg = SendGridAPIClient(
            "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"
        )
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(e)


# sendMailWithAttachment()
Пример #17
0
    def update(self, instance, validated_data):
        user = self.context['request'].user
        mail_subject = 'Покупка пакета'
        message = render_to_string('liqpay.html', {
            'pocket': validated_data['user_pocket'],
        })

        data = {
            'to_emails': [
                user.email,
            ],
            'subject': mail_subject,
            'html_content': message
        }

        pdfkit.from_string('TEST', 'smart_lead_pocket_paid.pdf')
        from_email = settings.DEFAULT_FROM_EMAIL
        message = Mail(
            from_email=from_email,
            **data,
        )
        attachment = Attachment()
        with open('smart_lead_pocket_paid.pdf', 'rb') as f:
            attachment.file_content = base64.b64encode(
                f.read()).decode('utf-8')
        attachment.file_name = 'smart_lead_pocket_paid.pdf'
        message.add_attachment(attachment)

        sg = SendGridAPIClient(settings.SENDGRID_API_KEY)
        sg.send(message)

        # send_email_task.delay(**data)
        return super().update(instance, validated_data)
Пример #18
0
def sendMail(file_path, timesec, to_mail):
    message = Mail(
        from_email='*****@*****.**',
        to_emails=to_mail,
        subject='IGuard: Обнаружено Нарушение',
        html_content="<strong>Обнаружено нарушение в {}</strong>".format(
            time.strftime('%H:%M:%S, %d.%m.%Y', time.localtime(timesec))))

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

    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('image/jpg')
    attachment.file_name = FileName('ImageIGuard.jpg')
    message.attachment = attachment

    try:
        sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(e)
Пример #19
0
    def send_zip(self, file_name: str, data: bytes) -> bool:
        # create message
        message = Mail(self.from_email, self.to_email, self.subject,
                       self.content)

        # create attachment
        file_type = 'GED file'
        encoded = base64.b64encode(data).decode()
        attachment = Attachment()
        attachment.file_content = FileContent(encoded)
        attachment.file_type = FileType(file_type)
        attachment.file_name = file_name
        attachment.disposition = Disposition('attachment')
        attachment.content_id = ContentId('Example Content ID')
        message.attachment = attachment

        # This code is for testing purposes.
        # Allows to test the class above without sending mail.
        # If a value in "SENDGRID_TEST" environment equals to "True" this function will return boolean True
        # otherwise this function will return boolean False.
        if os.environ.get("SENDGRID_TEST") is not None:
            return os.environ.get("SENDGRID_TEST") == "True"

        # send
        try:
            sg = SendGridAPIClient(self.api_key)
            response = sg.send(message)
            logger.info(
                "Sent gedcom successfully with response code: {}".format(
                    response.status_code))
            return True
        except Exception:
            logger.exception("Failed to sent gedcom")
            return False
Пример #20
0
 def MailGrid(self,*args,**kwargs):
     mail_txt = Content('text/plain',emailbody)
     message = Mail(
         from_email=sendermail,
         to_emails=recivermail,
         subject=subject,
         html_content=htmlcontent
         )
     message.add_content(mail_txt)
     if attach is not None:
         if os.path.isfile(attach):
             head, tail = os.path.split(attach)
             with open(attach, 'rb') as f:
                 data = f.read()
                 f.close()
             encoded = base64.b64encode(data).decode()
         else:
             encoded = attach
         attachment = Attachment()
         attachment.file_content = FileContent(encoded)
         attachment.file_type = FileType('application/pdf')
         attachment.file_name = FileName(tail)
         attachment.disposition = Disposition('attachment')
         attachment.content_id = ContentId('Example Content ID')
         message.attachment = attachment
     SENDGRID_API_KEY = "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"
     sendgrid_client = sendgrid.SendGridAPIClient(SENDGRID_API_KEY )
     response = sendgrid_client.send(message)
     print(response.status_code)
     print(response.body)
     print(response.headers)
Пример #21
0
    def send_email(self):
        """Send email with transcription attached"""
        import base64
        from sendgrid import SendGridAPIClient
        from sendgrid.helpers.mail import (Mail, Attachment, Email,
                                           FileContent, FileName, FileType)

        text_file = "transcription.txt"
        mail = Mail(
            from_email=Email('*****@*****.**', "Chi from Damaris"),
            to_emails=self.email,
            subject="Your transcription from Damaris 🔥",
            plain_text_content="Thank you for using Damaris. Please find \
            attached your transcribed file.")

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

        encoded_text_file = base64.b64encode(data).decode()
        attachment = Attachment()
        attachment.file_content = FileContent(encoded_text_file)
        attachment.file_type = FileType("text/plain")
        attachment.file_name = FileName("transcription.txt")
        mail.attachment = attachment

        try:
            sg = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
            sg.send(mail)
            log.info('Email sent successfully!')
        except Exception as e:
            log.error("Could not send email {}".format(e))
Пример #22
0
def sendMail(correo, ciudad):
    pattern = "(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
    #print(os.getcwd())
    MAIL_SENDER = os.getenv("MAIL_SENDER")
    if (re.search(pattern, correo)):
        message = Mail(from_email=MAIL_SENDER,
                       to_emails=correo,
                       subject='Starbucks report',
                       html_content='Your requested data is ready.')
        file_path = './output/stores_from_{}.pdf'.format(ciudad)
        with open(file_path, 'rb') as f:
            data = f.read()
            f.close()
        encoded = base64.b64encode(data).decode()
        attachment = Attachment()
        attachment.file_content = FileContent(encoded)
        #attachment.file_content = FileContent(data)
        attachment.file_type = FileType('application/pdf')
        attachment.file_name = FileName('stores_from_{}.pdf'.format(ciudad))
        attachment.disposition = Disposition('attachment')
        message.attachment = attachment
        try:
            token = os.getenv("SENDGRID_API_KEY")
            sg = SendGridAPIClient(token)
            response = sg.send(message)
            return response.status_code
            #print(response.body)
            #print(response.headers)
        except Exception as e:
            print(e.message)
    else:
        print("La dirección de email no es correcta")
        return 0
Пример #23
0
    def email(self, to_email, template, data):
        # We always append on the destination url to help.
        data['url'] = secret.get()['url']

        # Build the mail object & send the email.

        # Attach the ics to the email
        c = generate_ics()

        message = Mail(from_email=self.from_email, to_emails=to_email)
        message.dynamic_template_data = data
        message.template_id = self.templates[template]

        encoded = base64.b64encode(str(c).encode('utf-8')).decode()
        attachment = Attachment()
        attachment.file_content = FileContent(encoded)
        attachment.file_type = FileType('text/calendar')
        attachment.file_name = FileName('shift.ics')
        attachment.disposition = Disposition('attachment')
        attachment.content_id = ContentId('Example Content ID')
        message.attachment = attachment

        try:
            sendgrid_client = SendGridAPIClient(api_key=self.api_key)
            response = sendgrid_client.send(message)
            print(response.status_code)
            print(response.body)
            print(response.headers)
        except Exception as e:
            print(e)
Пример #24
0
def SendMail(ImgFileName):
    message = Mail(
        from_email='*****@*****.**',
        to_emails='*****@*****.**',
        subject='SURVEILLANCE ALERT: Unauthorized Access of MAIN GATE',
        html_content=
        '<strong>REPORT FOR INTRUSION DETECTION: Person has crossed the area, Here we attached a reference image </strong>'
    )

    data = cv2.imencode('.jpg', ImgFileName)[1].tostring()

    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('application/jpg')
    attachment.file_name = FileName('alert.jpg')
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Example Content ID')
    message.attachment = attachment

    try:
        sendgrid_client = SendGridAPIClient(
            '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'
        )
        response = sendgrid_client.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)

    except Exception as e:
        print(e)
Пример #25
0
def job():
    prices = []
    stars = []
    titles = []
    urlss = []

    pages_to_scrape = 10
    pages = [('http://books.toscrape.com/catalogue/page-{}.html').format(i)
             for i in range(1, pages_to_scrape + 1)]

    for item in pages:
        page = requests.get(item)
        soup = bs4(page.text, 'html.parser')
        for i in soup.findAll('h3'):
            titles.append(i.getText())
        for j in soup.findAll('p', class_='price_color'):
            prices.append(j.getText())
        for s in soup.findAll('p', class_='star-rating'):
            for k, v in s.attrs.items():
                stars.append(v[1])
        divs = soup.findAll('div', class_='image_container')
        for thumbs in divs:
            tgs = thumbs.find('img', class_='thumbnail')
            urls = 'http://books.toscrape.com/' + str(tgs['src'])
            newurls = urls.replace("../", "")
            urlss.append(newurls)
    data = {'Title': titles, 'Prices': prices, 'Stars': stars, "URLs": urlss}
    df = pd.DataFrame(data=data)
    df.index += 1
    directory = os.path.dirname(os.path.realpath(__file__))
    filename = "scrapedfile.csv"
    file_path = os.path.join(directory, 'csvfiles/', filename)
    df.to_csv(file_path)

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

    encoded = base64.b64encode(data).decode()
    message = Mail(
        from_email=FROM_EMAIL,
        to_emails=TO_EMAIL,
        subject='Your File is Ready',
        html_content='<strong>Attached is Your Scraped File</strong>')
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('text/csv')
    attachment.file_name = FileName('scraped.csv')
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Example Content ID')
    message.attachment = attachment
    try:
        sg = SendGridAPIClient(SENDGRID_API_KEY)
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(e)
Пример #26
0
def send_mail_sendgrid(description, agenda: Agenda):
    c = Calendar()
    e = Event()
    e.name = 'Atendimento Aset Terapias'

    str_begin = '-'.join(reversed(agenda.date.split('/')))
    str_begin = f'{str_begin} {agenda.time}'
    e.begin = (datetime.strptime(str_begin, '%Y-%m-%d %H:%M') +
               timedelta(hours=3))
    e.end = (datetime.strptime(str_begin, '%Y-%m-%d %H:%M') +
             timedelta(hours=4))

    e.attendees = [agenda.appointment.customer.email, agenda.therapist.email]
    e.description = description
    c.events.add(e)

    curdir = os.getcwd()

    with open(f'{curdir}{os.sep}go.ics', 'w') as f:
        f.writelines(c)

    with open(
            f'{curdir}{os.sep}apps{os.sep}agenda{os.sep}email_templates{os.sep}appointment-confirmation.html',
            encoding='utf-8') as templ:
        body = ''.join(templ.readlines())
        body = body.replace('{{nome}}', agenda.appointment.customer.name)
        body = body.replace('{{nome_terapia}}', agenda.appointment.specialty)
        body = body.replace('{{nome_terapeuta}}', agenda.therapist.name)
        body = body.replace('{{data}}', agenda.date)
        body = body.replace('{{hora}}', agenda.time)

        body = body.replace('{{api}}', os.getenv("API_ENDPOINT"))
        body = body.replace('{{calendar}}', agenda.calendar.name)
        body = body.replace('{{therapist_mail}}', agenda.therapist.email)
        body = body.replace('{{date}}', agenda.date.replace('/', '-'))
        body = body.replace('{{hour}}', agenda.time)
        body = body.replace('{{text}}', agenda.appointment.text)

        message = Mail(from_email=os.getenv("EMAIL_SENDER"),
                       to_emails=agenda.appointment.customer.email,
                       subject='Aset Terapias : Confirmação de consulta',
                       html_content=body)
        try:
            message.add_bcc(agenda.therapist.email)
            with open(f'{curdir}{os.sep}go.ics', 'rb') as f:
                data = f.read()
                f.close()
            encoded = base64.b64encode(data).decode()
            attachment = Attachment()
            attachment.file_content = FileContent(encoded)
            attachment.file_name = FileName('go.ics')
            attachment.disposition = Disposition('attachment')
            attachment.content_id = ContentId('Unique Content ID')
            message.attachment = attachment
            sg = SendGridAPIClient(api_key=os.getenv('EMAIL_TOKEN'))
            sg.send(message=message)
        except Exception as e:
            print('Erro no envio de e-mail')
            print(e)
Пример #27
0
def final_s():
    directory = os.path.dirname(os.path.realpath(__file__))
    filename2 = "results.csv"
    final_r = "virtual.csv"
    filename = "scrapedfile.csv"
    file_path = os.path.join(directory, 'clean/', filename)
    file_path3 = os.path.join(directory, 'clean/', final_r)
    file_path2 = os.path.join(directory, 'clean/', filename2)
    f = pd.read_csv(file_path)
    h = pd.read_csv(file_path2)
    df_merge_col = pd.merge(f, h, on='Match No')
    del df_merge_col['HomeTeam_y']
    del df_merge_col['AwayTeam_y']

    df_merge_col = df_merge_col.rename(columns={
        'HomeTeam_x': 'HomeTeam',
        'AwayTeam_x': 'AwayTeam'
    })

    df_merge_col.to_csv(file_path3, index=False)

    file_data = pd.read_csv(file_path3)
    dr = file_data.drop_duplicates(subset=['Match No'], keep='first')
    dr.to_csv(file_path3, index=False)
    insert()
    with open(file_path3, 'rb') as file:
        data_ = file.read()
        file.close()

    encoded = base64.b64encode(data_).decode()
    message = Mail(
        from_email=FROM_EMAIL,
        to_emails=TO_EMAIL,
        subject='Your File is Ready',
        html_content='<strong>Attached is Your Scraped File</strong>')
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('text/csv')
    x = datetime.datetime.now()
    w = x.strftime("%d_%b_%Y")
    filename = 'virtuals_' + str(w) + '.csv'
    attachment.file_name = FileName(filename)
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Example Content ID')
    message.attachment = attachment
    try:
        sg = SendGridAPIClient(SENDGRID_API_KEY)
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(e)
    os.remove(file_path)
    print(
        'combined the two csv files and created the final virtuals dataframe\nAnd then deleted the scrapedfile'
    )
Пример #28
0
def email_thread(queue: Queue, API: str, TESTER: str, EMAIL: str):
    status = ''
    running = True
    while running:
        data = queue.get()
        if data.message_type == 1:
            status = status + data.task_name + ': ' + data.message + '\n'
        elif data.message_type == 2:
            status = status + 'Warning: ' + data.task_name + ': ' + data.message + '\n'
        elif data.message_type == 3:
            status = status + 'Error: ' + data.task_name + ': ' + data.message + '\n'
        elif data.message_type == 4:
            status = status + 'Error: Test Failed: ' + data.task_name + '\n' + \
                str(data.exception[0]) + '\n' + str(data.exception[1]
                                                    ) + '\n' + traceback.format_tb(data.exception[2])[0]
            running = False
        elif data.message_type == 0:
            status = status + 'Test from unit: ' + TESTER + \
                ' has completed successfully, see results attached'
            running = False
            file_name = data.file_name
        print(data.task_name + ': ' + data.message)
        queue.task_done()
    print('sending email')
    zip_file = '{}.zip'.format(file_name)
    with ZipFile(zip_file, 'w') as zip:
        zip.write('{}.jpg'.format(file_name))
        zip.write('{}_hist.png'.format(file_name))
        zip.write('{}_hist.json'.format(file_name))

    message = Mail(from_email=TESTER + '@example.com',
                   to_emails=EMAIL,
                   subject='Test Results for Tester: ' + TESTER +
                   ' at time: x',
                   html_content='<p>' + status + '<p>')
    with open(zip_file, 'rb') as f:
        data = f.read()
        f.close()
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('application/zip')
    attachment.file_name = FileName('{}.zip'.format(file_name))
    attachment.disposition = Disposition('attachment')
    attachment.content_id = ContentId('Test Results')
    message.attachment = attachment
    try:
        sg = SendGridAPIClient(API)
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(str(type(e)) + ' ' + str(e))
    # code for sending email
    print('finished sending email')
    print(status)
Пример #29
0
    def send_vcf_file(ol, file_path):
        ol_email = ol['Kerberos']

        message = Mail(from_email='*****@*****.**',
                       to_emails=ol_email,
                       subject='Here is your OL contact file! 🎉',
                       html_content=F'''Hi {ol['First Name']},<br/><br/>
                Attached to this email is a file containing the contact information 
                for all of your First Year Students. Some of them may not have phone
                numbers or emails, but they definitely have at least one. If, for some
                reason, a contact doesn't have either an email or a phone number, please
                reach out to either Chelsea or Taylor (or myself, since I have the data)<br/><br/>
                <b>How to install contacts on iPhone</b><br/>
                If you have an iPhone, all you need to do to install the contacts is to click
                on the attachment in this email. Then, a window should pop up on your screen
                that says information about only one contact, ignore this and click the <em>share</em>
                button in the top right hand corner and then select the option 'Copy to Contacts'.
                Then, just make sure to click the option that says "Add all n contacts" and there you go!
                <br/><br/>
                Assuming all works well and my code isn't jank, you should be able to search 
                for your first years in the Contacts app by looking up "Blue 13" (if you are the OL
                for group Blue 13). Feel free to create your own contact group!
                <br/><br/>
                <b>How to install contacts on Android</b><br/>
                For the first time in all of human history, android is better at something 
                than iPhone. All you need to do is click on the attachment and android
                will import the contacts and even create a contact group for you named 
                '#Color #Number First Year'
                <br/><br/>
                Please let me know if something does not work!
                <br/><br/>
                Best,
                <br/><br/>
                Gonzo''')

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

        encoded = base64.b64encode(data).decode()

        attachment = Attachment()
        attachment.file_content = FileContent(encoded)
        attachment.file_type = FileType('text/x-vcard')
        attachment.file_name = FileName(file_path.replace(
            'contact-files/', ''))
        attachment.disposition = Disposition('attachment')
        message.attachment = attachment

        try:
            sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
            response = sg.send(message)
            print(response.status_code)
            print(response.body)
            print(response.headers)
        except Exception as e:
            print(str(e))
Пример #30
0
def build_attachment(file_path, disposition="attachment", content_id="Ebook"):
    filename, file_type, content = file_info(file_path)

    attachment = Attachment()
    attachment.file_content = FileContent(content)
    attachment.file_type = FileType(file_type)
    attachment.file_name = FileName(filename)
    attachment.disposition = Disposition(disposition)
    attachment.content_id = ContentId(content_id)

    return attachment