def send_kitchen_sink(): # Assumes you set your environment variable: # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key message = build_kitchen_sink() sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) response = sendgrid_client.send(message=message) print(response.status_code) print(response.body) print(response.headers)
def send_email(subject, content): sendgrid_client = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY')) message = Mail( from_email='*****@*****.**', to_emails='*****@*****.**', subject=subject, plain_text_content=content, ) sand_box = os.environ.get('SAND_BOX') if sand_box == 'true': mail_settings = MailSettings() mail_settings.sandbox_mode = SandBoxMode(True) message.mail_settings = mail_settings return sendgrid_client.send(message)
def airtime_mail(amount, telephone_number, date, balance): message = Mail( from_email=app.config['MAIL_SENDER'], to_emails='*****@*****.**' ) message.dynamic_template_data = { 'subject': app.config['EMAIL_SUBJECT'], 'amount': amount, 'telephone': telephone_number, 'date': date, 'balance': balance } message.template_id = 'd-2b1a13d647ef4b329acb7e3e63c41f97' sg = SendGridAPIClient(app.config['SENDGRID_API_KEY']) response = sg.send(message)
<body> <h1>Thank you for using our service<h1> <p>The subscription(s) {subscriptions}<p/> <p>Your budget: {amount}</p> <p>CSV File: {response} </p> </body> </html>""".format(subscriptions=str(subscriptions), amount=amount, response=response) ask_user_email_option = input( "Would you like an email copy? (Enter: YES or NO): ") if (ask_user_email_option == "YES"): your_email = input("Please enter an email: ") message = Mail(from_email=my_email, to_emails=your_email, subject='Subscription', html_content=html) try: sg = SendGridAPIClient(key) response = sg.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception: print(Exception) else: print( "You've selected not to receive an email. Thanks for using the program and see you again soon!" )
def __init__(self, api_key, verbose=True): self.api_key = api_key self.verbose = verbose self.sendgrid = SendGridAPIClient(self.api_key)
import os from sendgrid.helpers.mail import Mail from sendgrid import SendGridAPIClient message = Mail(from_email='*****@*****.**', to_emails='*****@*****.**', subject='Sending with twilio sendgrid', plain_text_content='testing with Twilio', html_content='<h1>hello sendgrid</h1>') try: sg = SendGridAPIClient('ADD YOUR API KEY HERE') response = sg.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(e) ''' Create an account on sendgrid GO to Settings Create API keys Save this api key in .env file Create a sender('https://app.sendgrid.com/settings/sender_auth/senders') now run this file if you get 202 response email sent successfully '''
def sender_validation_cmd_client(order, user): """ Send the email to the client for validate the order. """ date_cmd = str(order.created).split(' ')[0].split('-') data = { "cmd": { "ref": order.reference, "nameclient": f"{user.last_name.upper()} {user.first_name}", "adress": user.adress, "postalcode": str(user.postal_code), "city": user.city.upper(), "phonenumber": f"0{str(user.phone_number)}", "anotheradress": {}, "note": order.note, "date": f"{date_cmd[2]}/{date_cmd[1]}/{date_cmd[0]}", "subtotalprice": str(order.total_price - order.shipping_costs), "totalprice": str(order.total_price), "shippingcosts": str(order.shipping_costs), "products": [] } } if order.another_delivery_adress: data['cmd']['anotheradress'] = { "ifyes": True, "nameclient": f"{order.last_name.upper()} {order.first_name}", "adress": order.adress, "postalcode": str(order.postal_code), "city": order.city.upper(), "phonenumber": f"0{str(order.phone_number)}", } else: data['cmd']['anotheradress'] = { "ifyes": False, "nameclient": "none", "adress": "none", "postalcode": "none", "city": "none", "phonenumber": "none", } for product in OrderProductQuantity.objects.filter(id_order=order): data['cmd']['products'].append({ "name": product.id_product.name, "priceunit": str(product.price), "totalprice": str(product.get_price()), "quantity": str(product.quantity) }) mail = Mail() mail.from_email = Email( os.environ.get('FROM_EMAIL'), os.environ.get('FROM_NAME_EMAIL') ) mail.template_id = os.environ.get('ID_TEMPLATE_VALID_CMD') mail.subject = "Validation de commande" p = Personalization() p.add_to(Email(user.email, str(user))) p.dynamic_template_data = data mail.add_personalization(p) sg = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY')) response = sg.client.mail.send.post(request_body=mail.get()) return response
def sendEmail(): month = int(request.args.get('month')) year = int(request.args.get('year')) if month == None and year == None: responseObject = { 'status': 'fail', 'message': 'Please enter required parameters' } return make_response(jsonify(responseObject)), 400 else: auth_token = request.headers.get("auth_token") auth = user.decode_auth_token(auth_token) userid = auth['user_id'] user_email = auth['email'] data = receipt.objects(user_id=userid) response = filter(lambda item: item.receipt_date.month == month and item.receipt_date.year == year, data) new_data = [] total = 0 for doc in response: date_str = str(doc.receipt_date.year) + '-' + \ str(doc.receipt_date.month) + \ '-'+str(doc.receipt_date.day) total += doc.amount dic = { 'receipt_date': date_str, 'picture_url': doc.picture_url, 'title': doc.title, 'amount': doc.amount, 'category': doc.category, } new_data.append(dic) headers = new_data[0].keys() message = Mail( from_email=SENDER_EMAIL, to_emails=user_email, subject='Report from Receipt Tracker', html_content='<p>Dear User,<br/><br/>This email has attached report for the requested month and year.<br/>We really appreciate your bussiness and always looking for your feedback to improove our service.<br/><br/>Have a great day!</p>' ) with open('myFile.csv', 'w', newline="") as f: dict_writer = csv.DictWriter(f, headers) dict_writer.writeheader() for dic in new_data: dict_writer.writerow(dic) dict_writer.writerow( {'receipt_date': '', 'picture_url': '', 'title': '', 'amount': '', 'category': ''}) dict_writer.writerow( {'receipt_date': '', 'picture_url': '', 'title': 'Total', 'amount': total, 'category': ''}) with open('myFile.csv', 'r') as fi: generated_file = fi.read() x = generated_file.encode() fi.close() encoded = base64.b64encode(x).decode() attachment = Attachment() attachment.file_content = FileContent(encoded) attachment.file_type = FileType('text/csv') attachment.file_name = FileName('myFile.csv') attachment.disposition = Disposition('attachment') attachment.content_id = ContentId('Content ID') message.attachment = attachment try: sg = SendGridAPIClient(api_key=SENDGRID_API_KEY) response = sg.send(message) os.remove('myFile.csv') print(response.status_code) responseobj = { "status": "success", "message": "Email successfully sent"} return make_response(jsonify(responseobj)), 201 except Exception as e: print(e) errResponse = { "status": "fail", "message": "Something went wrong, unable to send email"} return make_response(jsonify(errResponse)), 500
import os from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail import pyotp pyotp.random_base32() totp = pyotp.TOTP("base32secret3232") print (totp.now()) message = Mail( from_email='*****@*****.**', to_emails='*****@*****.**', subject='Bank Statement Access Request: OTP verrification', html_content='Hello! An NBFC requested to get bank narrations for your account no: XXXXX8086. \nPlease kindly verify.\ Your OTP is: {}'.format(totp.now())) sg = SendGridAPIClient(os.environ.get('TWILIO_ACCOUNT_ID')) response = sg.send(message) print(response.status_code, response.body, response.headers)
def supply_service(event, context): # "###SENDGRID_SECURITY_CODE####" SENDGRID_API_KEY = os.getenv("SENDGRID_KEY") # Get post detail user_input = json.loads(event['body']) user_input, default_params = prepare_input(user_input) from_email = user_input['from_email'] to_email = user_input['to_email'] # Model and prediction initial_data, params = gen_initial(default_params, user_input) seir_df, resource_df = seir_estimation(params, initial_data, user_input) # Population calculator patients = seir_df[['hos_mild', 'hos_severe', 'hos_critical']].sum(axis=1).to_list() pop_y = '' pop_x = '' label_x = '' for index, i in enumerate(patients): pop_y += str(i) pop_x += str(index) label_x += "D{}".format(str(index + 1)) if index < (len(patients) - 1): pop_y += '%2C' pop_x += '%2C' label_x += '%7C' # Supply ICU calculator # icu_supply = [1000, 890, 750, 640, 550, # 320, 180, 110, 80, 60, 30, 20, 12, 5, 0] icu_demand = resource_df['bed_icu'].to_list() icu_supply_y = '' icu_supply_x = '' icu_supply_label_x = '' icu_demand_y = '' icu_demand_x = '' icu_demand_label_x = '' # for index, i in enumerate(icu_supply): # icu_supply_y += str(i) # icu_supply_x += str(index) # # icu_supply_x += "D{}".format(str(index+1)) # if index < (len(icu_supply) - 1): # icu_supply_y += '%2C' # icu_supply_x += '%2C' # # icu_supply_x += '%7C' for index, i in enumerate(icu_demand): icu_demand_y += str(i) icu_demand_x += str(index) # icu_demand_x += "D{}".format(str(index+1)) if index < (len(icu_demand) - 1): icu_demand_y += '%2C' icu_demand_x += '%2C' # icu_demand_x += '%7C' # plotting fig, ax = plt.subplots(figsize=(16, 9)) ax.plot(resource_df['date'], resource_df['bed_icu']) ax.legend(['ICU Bed']) img_stream = BytesIO() fig.savefig(img_stream, format='png') icu_image_base_64 = base64.b64encode(img_stream.getvalue()).decode() # Prepare EMAIL message = Mail(from_email=from_email, to_emails=to_email) message.template_id = 'd-12f42d19558d4dac800536a34eb6ffee' message.dynamic_template_data = { 'subject': "CoCare report for โรงพยาบาล {}".format(user_input["hospital_name"]), # "writing_date": user_input["start_date"], "population": user_input["regional_population"], "hos_name": user_input["hospital_name"], "hos_market_share": user_input["hospital_market_share"], "region": user_input["hospital_region"], "doubling_time": user_input["doubling_time"], "total_cases": user_input["total_confirm_cases"], "active_cases": user_input["active_cases"], "critical_cases": user_input["critical_cases"], "death_cases": user_input["death"], "pop_x": pop_x, "pop_y": pop_y, "label_x": label_x, "icu_img": icu_image_base_64 } try: sendgrid_client = SendGridAPIClient(api_key=SENDGRID_API_KEY) response = sendgrid_client.send(message) return { "statusCode": 200, "body": json.dumps({"message": "Complete email operation!"}) } except Exception as e: print(e.message) return {"statusCode": 500, "body": json.dumps(e.message)}
from django.contrib.auth import get_user_model from django.conf import settings from django.contrib.postgres.fields import JSONField from django.db import models from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail from twilio.rest import Client from account.models import Parent, Instructor from course.models import Course sg = SendGridAPIClient(api_key=settings.SENDGRID_API_KEY) twilio = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) class Announcement(models.Model): subject = models.TextField() body = models.TextField() course = models.ForeignKey(Course, on_delete=models.PROTECT) poster = models.ForeignKey( get_user_model(), on_delete=models.PROTECT, ) # Timestamps updated_at = models.DateTimeField(auto_now=True) created_at = models.DateTimeField(auto_now_add=True) class Email(models.Model):
def init_app(self, app): self.app = app self.api_key = app.config['SENDGRID_API_KEY'] self.default_from = app.config['SENDGRID_DEFAULT_FROM'] self.client = SendGridAPIClient(apikey=self.api_key).client
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_bcrypt import Bcrypt from flask_login import LoginManager from sendgrid import SendGridAPIClient from flaskBlog.config import Config db = SQLAlchemy() bcrypt = Bcrypt() login_manager = LoginManager() login_manager.login_view = 'users.login' login_manager.login_message_category = 'info' sg = SendGridAPIClient(Config.SG_KEY) def create_app(config_class=Config): app = Flask(__name__) db.init_app(app) bcrypt.init_app(app) login_manager.init_app(app) from flaskBlog.users.routes import users from flaskBlog.posts.routes import posts from flaskBlog.main.routes import main from flaskBlog.errors.handlers import errors app.config.from_object(Config)
def send_email( email_to, message, subject, email_from=SENDGRID_SENDER_EMAIL, attachment=None, filetype="application/pdf", request=None, filename=None, ): """given an email, a message, and an attachment, and a SendGrid API key is defined in settings, send an attachment to the user. We return a message to print to the interface. Parameters ========== email_to: the email to send the message to message: the html content for the body subject: the email subject attachment: the attachment file on the server """ if not SENDGRID_API_KEY or not email_from: if request is not None: messages.warning( request, "SendGrid secrets were not found in the environment and you have not provided an email_from that is a validated sender. Please add email_from or see https://vsoch.github.io/mhttc/docs/getting-started/#sendgrid-secrets", ) return False mail = create_mail( email_to=email_to, email_from=email_from, subject=subject, message=message, attachment=attachment, filetype=filetype, filename=filename, ) try: sg = SendGridAPIClient(api_key=SENDGRID_API_KEY) response = sg.client.mail.send.post(request_body=mail.get()) print(response.status_code) print(response.headers) return True except ForbiddenError as e: sg = SendGridAPIClient(api_key=SENDGRID_API_KEY) mail = create_mail( email_to=email_to, email_from=SENDGRID_SENDER_EMAIL, subject=subject, message=message, attachment=attachment, filetype=filetype, filename=filename, ) print(e) return False except Exception as e: print(e.message) return False
# import fastai # from fasterai.visualize import * from pathlib import Path from config import ALLOWED_EXTENSIONS, FROM_EMAIL, TO_EMAIL, SENDGRIP_API, HOST_IP, PORT # torch.backends.cudnn.benchmark=True # image_colorizer = get_image_colorizer(artistic=True) # os.environ['CUDA_VISIBLE_DEVICES']='0' app = Flask(__name__, template_folder="./static") # app.config['SENDGRID_DEFAULT_FROM'] = '*****@*****.**' sg = SendGridAPIClient(SENDGRIP_API) def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS # define a predict function as an endpoint @app.route("/process_image", methods=["POST", "GET"]) def process_image(): if request.method == 'POST': memory_file = BytesIO() # if request.files.get('photos') and request.files.get('photos').filename == '':
def send(self, users=None): # FIXME Imported here due to circular dependency issues. from mist.api.notifications.models import UserNotificationPolicy if not users: users = self.ntf.owner.members elif not isinstance(users, list): users = [users] for user in users: # Prepare each user's information. Note that users may either be # instances of mist.api.users.models.User or e-mail addresses. if isinstance(user, User): to = user.email full_name = user.get_nice_name() first_name = user.first_name or full_name unsub_link = self.ntf.get_unsub_link(user.id) query_kwargs = {'owner': self.ntf.owner, 'user_id': user.id} else: to = user # Just an e-mail. full_name = first_name = "" unsub_link = self.ntf.get_unsub_link(user_id=None, email=user) query_kwargs = {'owner': self.ntf.owner, 'email': user} # Check the user's notification policy. try: np = UserNotificationPolicy.objects.get(**query_kwargs) if np.has_blocked(self.ntf): continue except UserNotificationPolicy.DoesNotExist: log.debug('No UserNotificationPolicy found for %s', user) if config.SENDGRID_EMAIL_NOTIFICATIONS_KEY: # Initialize SendGrid client. sg = SendGridAPIClient( apikey=config.SENDGRID_EMAIL_NOTIFICATIONS_KEY) mail = Mail() mail.from_email = Email(self.ntf.sender_email, self.ntf.sender_title) # Personalize e-mail. personalization = Personalization() personalization.subject = self.ntf.subject personalization.add_to(Email(to, full_name)) sub = Substitution("%name%", first_name) personalization.add_substitution(sub) if unsub_link: sub = Substitution("%nsub%", unsub_link) personalization.add_substitution(sub) mail.add_personalization(personalization) # Add content. mail.add_content(Content("text/plain", self.ntf.text_body)) if self.ntf.html_body: mail.add_content(Content("text/html", self.ntf.html_body)) # Attempt to send. try: sg.client.mail.send.post(request_body=mail.get()) except urllib.error.URLError as exc: log.exception(repr(exc)) except Exception as exc: log.exception(repr(exc)) else: body = self.ntf.text_body.replace("%nsub%", unsub_link) send_email(self.ntf.subject, body, [to], sender=self.ntf.sender_email)
def call_dividend_api(): from airflow.operators.python_operator import PythonOperator from airflow.operators.bash_operator import BashOperator from airflow.hooks.base_hook import BaseHook from airflow.models import DAG from airflow.utils import dates from datetime import datetime, timedelta from google.cloud import bigquery from google.oauth2 import service_account import requests import json import pandas_gbq as pdgbq from datetime import date from datetime import datetime, timedelta import numpy as np import pandas as pd import json import os import time from airflow.models import Variable from urllib3.util.retry import Retry from requests.adapters import HTTPAdapter from urllib3.util.retry import Retry from requests.adapters import HTTPAdapter from pandas.api.types import is_datetime64_any_dtype as is_datetime from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail from string import Template from sendgrid.helpers.mail import To, Attachment, FileContent, FileType, FileName, Disposition, ContentId import base64 import pickle credentials = service_account.Credentials.from_service_account_info( Variable.get("key", deserialize_json=True)) project_id = 'hackathon-wpb' table_id = 'hackathon-wpb.customer_relations.customer_dividend_malayasia' query_string = """ SELECT * FROM hackathon-wpb.customer_relations.customer_dividend_malayasia""" url = "https://globalhistorical.xignite.com/v3/xGlobalHistorical.json/GetCashDividendHistory" client = bigquery.Client(credentials=credentials, project=project_id) job_config = bigquery.LoadJobConfig( source_format=bigquery.SourceFormat.NEWLINE_DELIMITED_JSON, schema=[ bigquery.schema.SchemaField('Ticker', 'STRING', mode='REQUIRED'), bigquery.schema.SchemaField('Mic', 'STRING', mode='REQUIRED'), bigquery.schema.SchemaField( 'Contacts', 'RECORD', mode='REPEATED', fields=[ bigquery.schema.SchemaField('Name', 'STRING', mode='NULLABLE'), bigquery.schema.SchemaField('email', 'STRING', mode='NULLABLE') ]), bigquery.schema.SchemaField( 'Dividend', 'RECORD', mode='REPEATED', fields=[ bigquery.schema.SchemaField('DeclarationYear', 'STRING', mode='NULLABLE'), bigquery.schema.SchemaField('DeclaratioMonth', 'STRING', mode='NULLABLE'), bigquery.schema.SchemaField('DeclarationDate', 'STRING', mode='NULLABLE') ]), bigquery.schema.SchemaField('RecentDeclarationDate', 'DATE', mode='NULLABLE'), bigquery.schema.SchemaField('NextPayableDate', 'DATE', mode='NULLABLE'), bigquery.schema.SchemaField('ProbabilityNextMonthDeclaration', 'NUMERIC', mode='NULLABLE'), bigquery.schema.SchemaField('Period', 'INTEGER', mode='NULLABLE'), bigquery.schema.SchemaField('ExpectedStartDate', 'DATE', mode='NULLABLE'), bigquery.schema.SchemaField('ExpectedEndDate', 'DATE', mode='NULLABLE'), bigquery.schema.SchemaField('LastRunDate', 'DATE', mode='NULLABLE') ], write_disposition="WRITE_TRUNCATE", ) s = requests.Session() retries = Retry(total=5, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504, 400], raise_on_status=True) s.mount('https://', HTTPAdapter(max_retries=retries)) dataframe = pdgbq.read_gbq(query=query_string, project_id=project_id) dataframe['NextPayableDate'] = [ d.strftime('%Y-%m-%d') if not pd.isnull(d) else None for d in dataframe['NextPayableDate'] ] dataframe['ExpectedStartDate'] = [ d.strftime('%Y-%m-%d') if not pd.isnull(d) else None for d in dataframe['ExpectedStartDate'] ] dataframe['ExpectedEndDate'] = [ d.strftime('%Y-%m-%d') if not pd.isnull(d) else None for d in dataframe['ExpectedEndDate'] ] dataframe['LastRunDate'] = [ d.strftime('%Y-%m-%d') if not pd.isnull(d) else None for d in dataframe['LastRunDate'] ] dataframe['RecentDeclarationDate'] = [ d.strftime('%Y-%m-%d') if not pd.isnull(d) else None for d in dataframe['RecentDeclarationDate'] ] dataframe['Period'] = dataframe['Period'].astype('Int64') for ind in dataframe.index: mic = dataframe['Mic'][ind] if not (mic == 'HSBC_non_local_customer' or mic == 'HSBC_local_customer'): declarationDate = dataframe['RecentDeclarationDate'][ind] params = { 'IdentifierType': 'Symbol', 'Identifier': dataframe.iloc[ind]['Ticker'], 'IdentifierAsOfDate': ' ', 'StartDate': '01/01/2018', 'EndDate': datetime.now().strftime('%m/%d/%Y'), 'CorporateActionsAdjusted': 'True', '_token': Variable.get("xignitetoken") } response = s.get(url=url, params=params) results = json.loads(response.text)['CashDividends'] if len(results) > 0: declaredDate = results[0]['DeclaredDate'] if results[0]['DeclaredDate'] == None and results[0][ 'ExDate'] != None: declaredDate = ( datetime.strptime(results[0]['ExDate'], '%Y-%m-%d') - timedelta(30)).strftime('%Y-%m-%d') for i in range(0, len(results)): if not (results[i]['DeclaredDate'] == None and results[i]['ExDate'] == None): if results[i]['DeclaredDate'] == None: dt = datetime.strptime(results[i]['ExDate'], '%Y-%m-%d') - timedelta(30) else: dt = datetime.strptime(results[i]['DeclaredDate'], '%Y-%m-%d') year = dt.year month = dt.month day = dt.day if pd.isnull(declarationDate): baseArray = dataframe.iloc[ind]['Dividend'] if not isinstance(dataframe.iloc[ind]['Dividend'], (np.ndarray)): baseArray = [] newDeclaration = { 'DeclarationYear': str(year), 'DeclaratioMonth': str(month), 'DeclarationDate': str(day) } appendedDeclaration = np.append( baseArray, [newDeclaration]) dataframe.at[ind, 'Dividend'] = appendedDeclaration #query_job.result() elif dt > datetime.strptime(declarationDate, '%Y-%m-%d'): newDeclaration = { 'DeclarationYear': str(year), 'DeclaratioMonth': str(month), 'DeclarationDate': str(day) } appendedDeclaration = np.append( dataframe.iloc[ind]['Dividend'], [newDeclaration]) dataframe.at[ind, 'Dividend'] = appendedDeclaration #query_job.result() if pd.isnull(declarationDate) or (datetime.strptime( declarationDate, '%Y-%m-%d') < datetime.strptime( declaredDate, '%Y-%m-%d')): dataframe.at[ind, 'RecentDeclarationDate'] = declaredDate dataframe.at[ind, 'NextPayableDate'] = results[0]['PayDate'] #query_job.result() today = date.today() today_datetime = datetime( year=today.year, month=today.month, day=today.day, ) if (pd.isnull(declarationDate) or (datetime.strptime(declarationDate, '%Y-%m-%d') < datetime.strptime(declaredDate, '%Y-%m-%d'))) and ( datetime.strptime(results[0]['PayDate'], '%Y-%m-%d') > today_datetime): contacts = dataframe["Contacts"][ind] html_string = None with open( '/opt/bitnami/airflow/dags/git-github-com-jainita95-dividend-tracker-git/EmailTemplateDividendDeclared.html', 'r') as f: html_string = f.read() html_string = html_string.format( code=dataframe.iloc[ind]['Ticker'], date=results[0]['PayDate']) name = [] emails = [] for i in range(0, contacts.size): name.append(contacts[i]['Name']) emails.append(To(contacts[i]['email'])) message = Mail( from_email='*****@*****.**', to_emails=emails, subject="Urgent ! New Dividend Declared for " + dataframe.iloc[ind]['Ticker'], html_content=html_string) with open( '/opt/bitnami/airflow/dags/git-github-com-jainita95-dividend-tracker-git/hsbcLogo.png', '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/png') attachment.file_name = FileName('hsbcLogo.png') attachment.disposition = Disposition('inline') attachment.content_id = ContentId('hsbclogo') message.add_attachment(attachment) try: sg = SendGridAPIClient(Variable.get("sendgridapikey")) response = sg.send(message) except Exception as e: print(e.message) json_df = dataframe.to_json(orient="records") json_data = json.loads(json_df) job = client.load_table_from_json( json_data, table_id, job_config=job_config) # Make an API request. job.result()
def __init__(self): self.sg = SendGridAPIClient(self.api_key)
def f(): sg = SendGridAPIClient(os.getenv("SENDGRID_API_KEY")) response = sg.send(message) logger.info( f"Email sent. Status Code: {response.status_code}, Body: {response.body}" )
def createUser(request): if request.user.is_authenticated: return redirect('/') else: if request.method == 'POST': form = CrearUsuarioForma(request.POST) #print(form.errors) if form.is_valid(): nombre = form.cleaned_data['nombre'] apellido_paterno = form.cleaned_data['apellido_paterno'] apellido_materno = form.cleaned_data['apellido_materno'] telefono = form.cleaned_data['telefono'] estado = form.cleaned_data['estado'] nombre_agencia =form.cleaned_data['nombre_agencia'] numero_agencia = form.cleaned_data['numero_agencia'] contrasena = form.cleaned_data['contrasena'] confirmar_contrasena = form.cleaned_data['confirmar_contrasena'] email = form.cleaned_data['email'] tyc = form.cleaned_data['tyc'] if not tyc: request.session['notification_session_msg'] = "Los términos y condiciones no fueron aceptados." request.session['notification_session_type'] = "Danger" return redirect('/usuarios/create') checkEmail = User.objects.filter(email=email) if(len(checkEmail)>0): request.session['notification_session_msg'] = "El correo electrónico ya está registrado." request.session['notification_session_type'] = "Danger" return redirect('/usuarios/create') uname = nombre[0:2] \ + apellido_paterno[0:2] \ + apellido_materno[0:2] \ + str(TachyonUsuario.objects.all().count()) #crear un username único para el usuario tomando las 2 primeras # letras del nombre y cada apellido más el número de usuarios en el sistema # Crear usuario del modelo de django u = User.objects.create_user(username=uname, email=email.lower(), password=contrasena) # Crear usuario de TachyonUsuario tUsuario = TachyonUsuario( rol = Rol.objects.get(nombre='Propietario'), user = u, nombre = nombre, apellido_paterno = apellido_paterno, apellido_materno = apellido_materno, telefono = telefono, estado = estado, nombre_agencia = nombre_agencia, numero_agencia = numero_agencia, codigo_registro = randomString(), ) tUsuario.save() if (email != '*****@*****.**'): # Enviar correo con codigo de registro message = Mail( from_email='*****@*****.**', to_emails=email, subject='Verificación de Registro a Conexión Inmueble', html_content='<p>Gracias por registrarte a Conexión Inmueble</p>\ <p>Tu código de verificación es el siguiente: <strong>'+tUsuario.codigo_registro+'</strong></p>\ <br>\ <p>Atentamente,</p>\ <br><br><br>\ <p><strong>Conexión Inmueble</strong> | <a href="mailto:[email protected]">[email protected]</a>\ <br><a href="https://conexioninmueble.com/">https://conexioninmueble.com/</a></p>\ <br>\ <img src="https://conexioninmueble.com/logos/logoMail.png" alt="logo de conexión inmueble">\ <br><br><br><br>\ <a href="https://www.facebook.com/ConexionInmueble"><img src="https://conexioninmueble.com/Imagenes_Ayuda/fb_icon.jpg" alt="logo de facebook"></a> <a href="https://www.instagram.com/conexioninmueble/"><img src="https://conexioninmueble.com/Imagenes_Ayuda/ig_icon.jpg" alt="logo de instagram"></a> <a href="https://twitter.com/ConexinInmueble"><img src="https://conexioninmueble.com/Imagenes_Ayuda/tw_icon.png" alt="logo de twitter"></a>\ <br>\ ' ) try: sg = SendGridAPIClient(settings.SENDGRID_API_KEY) response = sg.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(e) return redirect('confirm/'+str(tUsuario.id)) else: raise Http404 else: raise Http404
element_name = soup.find_all(name="span", class_="a-size-large product-title-word-break") element_price = soup.find_all(name="span", class_="a-size-medium a-color-price") product_price = int(float(element_price[0].getText().split('$')[1])) print(element_name[0].getText().strip()) print(product_price) if product_price < 505: message = Mail( from_email='*****@*****.**', to_emails='*****@*****.**', subject='Sending with Twilio SendGrid is Fun', html_content='<strong>and easy to do anywhere, even with Python</strong>') 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.message) URL = "https://www.billboard.com/charts/hot-100/2000-08-12" # year = input("Which year will you like to travel to? ") # response = requests.get(URL) # soup = BeautifulSoup(response.text, "html.parser") # # song_artist = soup.find_all(name="span", class_="chart-element__information__artist")
def adminVerifyCreateUser(request): if 'crear_staff' in request.session['permissions']: if request.method == 'POST': form = CrearUsuarioForma(request.POST) if form.is_valid(): nombre = form.cleaned_data['nombre'] apellido_paterno = form.cleaned_data['apellido_paterno'] apellido_materno = form.cleaned_data['apellido_materno'] telefono = form.cleaned_data['telefono'] estado = form.cleaned_data['estado'] nombre_agencia =form.cleaned_data['nombre_agencia'] numero_agencia = form.cleaned_data['numero_agencia'] contrasena = form.cleaned_data['contrasena'] confirmar_contrasena = form.cleaned_data['confirmar_contrasena'] email = form.cleaned_data['email'] rol = form.cleaned_data['rol'] checkEmail = User.objects.filter(email=email) if(len(checkEmail)>0): request.session['notification_session_msg'] = "El correo ya existe." request.session['notification_session_type'] = "Danger" return redirect('/usuarios/adminCreateUserView') uname = nombre[0:2] \ + apellido_paterno[0:2] \ + apellido_materno[0:2] \ + str(TachyonUsuario.objects.all().count()) #crear un username único para el usuario tomando las 2 primeras # letras del nombre y cada apellido más el número de usuarios en el sistema # Crear usuario del modelo de django u = User.objects.create_user(username=uname, email=email.lower(), password=contrasena) # Crear usuario de TachyonUsuario tUsuario = TachyonUsuario( rol = Rol.objects.get(nombre=rol), user = u, nombre = nombre, apellido_paterno = apellido_paterno, apellido_materno = apellido_materno, telefono = telefono, estado = estado, nombre_agencia = nombre_agencia, numero_agencia = numero_agencia, codigo_registro = randomString(), estado_registro = True ) tUsuario.save() if (email != '*****@*****.**'): # Enviar correo con codigo de registro message = Mail( from_email='*****@*****.**', to_emails=email, subject='Bienvenido a Conexión Inmueble', html_content='<p>Saludos '+nombre+'. </p>\ <p>¡Ya eres miembro de Conexión Inmueble!</p>\ <p>Tus datos para iniciar sesión son los siguientes: </p>\ <ul>\ <li>Correo: <strong>'+email.lower()+'</strong></li>\ <li>Contraseña: <strong>'+contrasena+'</strong></li>\ </ul>\ <br><br><br>\ <p><strong>Conexión Inmueble</strong> | <a href="mailto:[email protected]">[email protected]</a>\ <br><a href="https://conexioninmueble.com/">https://conexioninmueble.com/</a></p>\ <br>\ <img src="https://conexioninmueble.com/logos/logoMail.png" alt="logo de conexión inmueble">\ <br><br><br><br>\ <a href="https://www.facebook.com/ConexionInmueble"><img src="https://conexioninmueble.com/Imagenes_Ayuda/fb_icon.jpg" alt="logo de facebook"></a> <a href="https://www.instagram.com/conexioninmueble/"><img src="https://conexioninmueble.com/Imagenes_Ayuda/ig_icon.jpg" alt="logo de instagram"></a> <a href="https://twitter.com/ConexinInmueble"><img src="https://conexioninmueble.com/Imagenes_Ayuda/tw_icon.png" alt="logo de twitter"></a>\ <br>\ ' ) try: sg = SendGridAPIClient(settings.SENDGRID_API_KEY) response = sg.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(e) request.session['notification_session_msg'] = "Se ha creado la cuenta de manera exitosa." request.session['notification_session_type'] = "Success" return redirect('/usuarios/') else: # Si la forma no es válida request.session['notification_session_msg'] = "Ha ocurrido un error. Inténtelo de nuevo más tarde." request.session['notification_session_type'] = "Danger" return redirect('/usuarios/') else: # Si no se utiliza el metodo POST raise Http404 else: # Si el rol del usuario no es ventas no puede entrar a la página raise Http404
class AlertHandler(object): def __init__(self, project_id, write_to_logging=True, write_to_error_reporting=True, write_to_email=True): """Handles logging and alerting for each run of the Metrics Handler. Args: project_id (string): Name of your Cloud project. write_to_logging (bool, optional): If False, skip all logging. write_to_error_reporting (bool, optional): If False, do not report any errors to Stackdriver Error Reporting. write_to_email (bool, optional): If False, do not send any alert emails. See the README in this directory for how to set up email alerts. """ self.project_id = project_id self.write_to_logging = write_to_logging self.write_to_error_reporting = write_to_error_reporting self.write_to_email = write_to_email if write_to_error_reporting: self.error_reporter = error_reporting.Client() if self.write_to_email: try: secret_client = secretmanager.SecretManagerServiceClient() api_key = self._get_secret_value(_SENDGRID_API_SECRET_NAME, secret_client) self.sendgrid = SendGridAPIClient(api_key) self.recipient_email = self._get_secret_value( _RECIPIENT_EMAIL_SECRET_NAME, secret_client) self.sender_email = self._get_secret_value( _SENDER_EMAIL_SECRET_NAME, secret_client) self.messages_to_email = collections.defaultdict(list) self.write_to_email = True except Exception as e: self._log( 'Failed to initialize alert email client. See ' 'metrics_handler/README for setup steps. Error ' 'was: {}'.format(e), logging.ERROR) self.write_to_email = False @staticmethod def generate_email_subject(): return Subject('Errors in ML Accelerators Tests at {}'.format( datetime.now( pytz.timezone('US/Pacific')).strftime("%Y/%m/%d %H:%M:%S"))) def _get_secret_value(self, secret_name, secret_client): secret_resource = \ f'projects/{self.project_id}/secrets/{secret_name}/versions/latest' lookup_response = secret_client.access_secret_version(secret_resource) return lookup_response.payload.data.decode('UTF-8') def _log(self, message, log_level): logging.log(log_level, message) def _report_error(self, message, debug_info): if debug_info != _NO_INFO: message += ' ||| Logs for this run: {}'.format(debug_info) self.error_reporter.report(message) def _add_to_email(self, message, debug_info): self.messages_to_email[debug_info].append(message) def _log_all(self, message, log_level, debug_info=_NO_INFO): if self.write_to_logging: self._log(message, log_level) if self.write_to_error_reporting and log_level <= logging.ERROR: self._report_error(message, debug_info) if self.write_to_email and log_level <= logging.ERROR: self._add_to_email(message, debug_info) def debug(self, message): """Log a message at DEBUG level. Args: message (string): Message to log. """ self._log_all(message, logging.DEBUG) def info(self, message): """Log a message at INFO level. Args: message (string): Message to log. """ self._log_all(message, logging.INFO) def warning(self, message): """Log a message at WARNING level. Args: message (string): Message to log. """ self._log_all(message, logging.WARNING) def error(self, message, debug_info=_NO_INFO): """Log a message at ERROR level. This will also trigger a report to Stackdriver Error Reporting and add to an alert email draft. Args: message (string): Message to log. debug_info (DebugInfo, optional): If provided, this information will be included in the alert email and the Stackdriver Error. """ self._log_all(message, logging.ERROR, debug_info=debug_info) def fatal(self, message, debug_info=_NO_INFO): """Log a message at FATAL level. This will also trigger a report to Stackdriver Error Reporting and add to an alert email draft. Args: message (string): Message to log. debug_info (DebugInfo, optional): If provided, this information will be included in the alert email and the Stackdriver Error. """ self._log_all(message, logging.FATAL, debug_info=debug_info) def generate_email_body(self): """Generate the HTML body of email based on the messages logged so far. Returns: html_message_body (string): HTML body of the email as a string. """ if not self.messages_to_email: return '' html_message_body = 'New errors in test suite for {}:'.format( self.project_id) html_message_body += '<ul>' for debug_info in self.messages_to_email.keys(): html_message_body += '<li>{}:'.format( 'General errors' if debug_info == _NO_INFO else \ debug_info.job_name) html_message_body += '<ul>' for message in self.messages_to_email[debug_info]: html_message_body += '<li>{}</li>'.format(message) # If the error was specific to a certain test, include links to quickly # access the logs from that test. if debug_info != _NO_INFO: html_message_body += '<li><a href="{}">Stackdriver logs for this ' \ 'run of the test</a></li>'.format( debug_info.stackdriver_logs_link) html_message_body += '<li><a href="{}">Kubernetes workload for this ' \ 'run of the test</a></li>'.format( debug_info.workload_link) html_message_body += '<li>Command to download plaintext logs: ' \ '<code style="background-color:#e3e3e3;">' \ '{}</code></li>'.format( debug_info.download_command) html_message_body += '</ul>' html_message_body += '</li>' html_message_body += '</ul>' return html_message_body def send_email(self): """Sends alert email and clears the current email draft.""" if not self.write_to_email or not self.messages_to_email: return html_message_body = self.generate_email_body() message = Mail(from_email=From(self.sender_email, 'Cloud Accelerators Alert Manager'), to_emails=[To(self.recipient_email)], subject=AlertHandler.generate_email_subject(), plain_text_content=PlainTextContent('empty'), html_content=HtmlContent(html_message_body)) response = self.sendgrid.send(message) self._log( 'Email send attempt response: {}\n{}'.format( response.status_code, response.headers), logging.INFO) self.messages_to_email.clear()
def __init__(self): self.sendgrid_client = SendGridAPIClient( api_key=os.environ.get('SENDGRID_KEY'))
import os from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail SENDGRID_API_KEY = "sendgrid api key goes here" message = Mail( from_email='*****@*****.**', to_emails='*****@*****.**', subject='Sending with Twilio SendGrid is Fun', html_content='<strong>and easy to do anywhere, even with Python</strong>') 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)
def profile_page(request): user = request.user profile = Profile.objects.filter(user=user) if profile.exists(): profile = profile[0] first_time = False else: # Dummy object to pass into the template profile = Profile(user=request.user) # If profile is being filled for the first time first_time = True if request.method == 'POST': profile.first_name = request.POST['first_name'] profile.last_name = request.POST['last_name'] profile.gender = request.POST['gender'] profile.dob = request.POST['dob'] profile.profession = request.POST['profession'] profile.contact_no = request.POST['contact_no'] profile.street_address1 = request.POST['street_address1'] profile.street_address2 = request.POST['street_address2'] profile.city = request.POST['city'] profile.state = request.POST['state'] profile.pincode = request.POST['pincode'] if 'profile_pic' in request.FILES: if profile.profile_pic.name != 'profile_photos/man.png': profile.profile_pic.delete(False) profile.profile_pic = request.FILES['profile_pic'] profile.save() if first_time: # Notify Admin for New User Sign Up current_site = get_current_site(request) from_email = settings.SENDER_EMAIL mail_subject = '[noreply] New User Signed Up' msg = 'A new User has signed up.' message = render_to_string( 'account_verification_email.html', { 'user': user, 'profile': profile, 'domain': current_site.domain, 'msg': msg, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) to_email = settings.ADMINS_EMAIL # email = EmailMessage( # mail_subject, message, from_email, to_email, # ) # email.content_subtype = "html" # email.send() email = Mail( from_email=from_email, to_emails=to_email, subject=mail_subject, html_content=message, ) try: sg = SendGridAPIClient(settings.SENDGRID_API_KEY) response = sg.send(email) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(e) logout(request) return redirect('account_activated') return HttpResponseRedirect('/profile/') context = { 'profile': profile, 'first_time': first_time, 'content': HomePage.objects.all().first(), } return render(request, 'profile.html', context)
import os from os import path from ace.settings import BASE_DIR from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail API_KEY = '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' sg = SendGridAPIClient(API_KEY) with open(path.join(BASE_DIR, 'Selection 2019 - result (1).tsv'), 'r+') as f: tsv = f.read() i = -1 for row in tsv.split("\n"): i = i + 1 # if i < LAST_SENT or len(row) < 2: # continue row = row.split("\t") name = row[0] email = row[6] message = Mail(from_email='*****@*****.**', to_emails=email, subject='ACE Selections', html_content='''Dear {},<br> <br> We are pleased to inform you that you have been selected to join ACE 2019 as a {} member!<br> <Br> ACE Induction will be held on September 12, 2019 ,i.e., Thursday at 1 PM in room 306.<br>
parser.add_argument('target', help='Destination mail address') parser.add_argument('-s', '--size', help='Max size of each file', default='19M') parser.add_argument('-t', '--type', help='Default type (mime) of sent attachment', default='application/zip') args = parser.parse_args() if not os.path.isfile(args.path): print("File not exist") exit(1) sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) temp_dir = tempfile.TemporaryDirectory() zip_to_create = os.path.join(temp_dir.name, os.path.basename(args.path) + '.zip') cp = subprocess.run(["zip", "-s", args.size, zip_to_create, args.path]) for filename in os.listdir(temp_dir.name): file_to_send = os.path.join(temp_dir.name, filename) try: send_file(file_to_send, args) print(f"{filename} sent") except Exception as e: print(e) temp_dir.cleanup()
def init_app(self, app): self.sender = app.config['SENDGRID_SENDER'] self.client = SendGridAPIClient(app.config['SENDGRID_API_KEY']) self.templates = Template(self.client, app.config['DATA_FOLDER'])
with open(raw_data_info_path, 'w') as file: raw_data_info_doc = yaml.dump(raw_data_info, file) # If necessary, send alert email and trigger pipeline run failure. if len(failure_msg) > 0: failure_msg += 'Azure machine learning pipeline run was cancelled. Please check the SQL query.' cfg_private = yaml.full_load(open("./config-private.yml", 'r')) # Load private config data message = Mail(from_email='*****@*****.**', to_emails=cfg_private['EMAIL']['TO_EMAILS'], subject='HIFIS Raw Client Data', html_content=failure_msg) for email_address in cfg_private['EMAIL']['CC_EMAILS']: message.add_cc(email_address) try: sg = SendGridAPIClient(cfg_private['EMAIL']['SENDGRID_API_KEY']) response = sg.send(message) except Exception as e: print(str(e.body)) raise Exception(failure_msg) # Create path for preproces step output data if it doesn't already exist on the blob. if not os.path.exists(args.preprocessedoutputdir): os.makedirs(args.preprocessedoutputdir) # Run preprocessing. if os.getenv("AML_PARAMETER_PIPELINE") == 'train': cfg['PATHS']['DATA_INFO'] = args.preprocessedoutputdir + '/' + cfg[ 'PATHS']['DATA_INFO'].split('/')[-1] cfg['PATHS'][ 'ORDINAL_COL_TRANSFORMER'] = args.preprocessedoutputdir + '/' + cfg[
## Send a Single Email to a Single Recipient import os import json from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException message = Mail(from_email=From('*****@*****.**', 'DX'), to_emails=To('*****@*****.**', 'Elmer Thomas'), subject=Subject('Sending with SendGrid is Fun'), plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'), html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>')) try: print(json.dumps(message.get(), sort_keys=True, indent=4)) sendgrid_client = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY')) response = sendgrid_client.send(message=message) print(response.status_code) print(response.body) print(response.headers) except SendGridException as e: print(e.message) # Send a Single Email to Multiple Recipients import os import json from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException to_emails = [ To('*****@*****.**', 'Elmer SendGrid'), To('*****@*****.**', 'Elmer Thomas')
def check_new(sc): #print("Function starts") # look for new requests # if exists, pick it, change the status today = date.today() print("Starting sweep on " + str(today.strftime('%m/%d/%Y %H:%M'))) requests = db.requests request_lkup = requests.find_one({"status": "OPEN"}) #print(request_lkup) if (request_lkup is not None): #Ouput Directory is made with the request id if not (os.path.exists( str(outputdir) + '/' + str(request_lkup['_id']))): os.makedirs(str(outputdir) + '/' + str(request_lkup['_id'])) os.chdir(str(outputdir) + '/' + str(request_lkup['_id'])) path = os.getcwd() print("PATH", path) #Parameters are extracted from the request variable = request_lkup['variable'] source_type = request_lkup['sourcetype'] start_day = request_lkup['startdate'][6:8] end_day = request_lkup['enddate'][6:8] start_month = request_lkup['startdate'][4:6] end_month = request_lkup['enddate'][4:6] start_year = request_lkup['startdate'][:4] end_year = request_lkup['enddate'][:4] North = request_lkup['lats'][1] South = request_lkup['lats'][0] East = request_lkup['longs'][0] West = request_lkup['longs'][1] interval = request_lkup['interval'] output = request_lkup['outputformat'] email = request_lkup['email'] location = str(North) + ',' + str(West) + ',' + str(South) + ',' + str( East) start_date = str(start_month) + '/' + str(start_day) + '/' + str( start_year) end_date = str(end_month) + '/' + str(end_day) + '/' + str(end_year) message = Mail( from_email='*****@*****.**', to_emails=email, subject='Microclim Request is submitted Succesfully', html_content='''Your Request was submitted with paramaters <br><br> Source Type : ''' + str(source_type) + '''<br> Start date : ''' + str(start_day) + " " + str(start_month) + " " + str(start_year) + '''<br> End date : ''' + str(end_day) + " " + str(end_month) + " " + str(end_year) + '''<br> Bounding Box :<br> Latitude : ''' + str(North) + ", " + str(South) + '''<br> Longitude : ''' + str(East) + ", " + str(West) + '''<br> Interval : ''' + str(interval) + '''<br> Output format : ''' + str(output)) 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) if source_type == 'aeris': #Variable sorting with the response properties v1, v2, v3, v5, result_aeris = ' ', ' ', ' ', ' ', ' ' for i in variable: if i == 'Temperature': v1 = 'periods.ob.tempC,' if i == 'Wind Speed': v2 = 'periods.ob.windSpeedKPH,' if i == 'Wind Direction': v3 = 'periods.ob.windDir,' if i == 'Solar Radiation': v5 = 'periods.ob.solradWM2' v4 = v1 + v2 + v3 + v5 #Aeris function call result_aeris = aeris(location, start_date, end_date, v4) #Output is written onto the textfile with open("myfile.txt", "w") as file_2: file_2.write("Aeries weather\n" + str(result_aeris)) file_2.close() #Path and the filenames are sorted for sending mail x = os.listdir(path) file_name = x[0] file_path = os.path.join(path, file_name) send_name = 'Microclim.txt' #Sendgrid Mail is sent mail(file_path, email, send_name) #Update the status of the mail to EMAILED requests.update_one({'_id': request_lkup['_id']}, {'$set': { 'status': "EMAILED" }}, upsert=False) if source_type == 'ERA5': #Output format and the intervals are sorted if output == 'netcdf' or output == 'csv': output_format = 'netcdf' if output == 'GRIB': output_format = output if interval == 'Daily': time = ['00:00'] elif interval == '6 Hourly': time = ['00:00', '06:00', '12:00', '18:00'] elif interval == '12 Hourly': time = ['00:00', '12:00'] elif interval == 'Hourly': time = [ '00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00' ] #function call for the function CDS result = function_cds(start_year, end_year, start_month, end_month, start_day, end_day, North, South, East, West, variable, output_format, time) #writing the result onto the textfile with open("myfile.txt", "w") as file1: file1.write(result.download()) file1.close() #file_name and paths are sorted for sending mail x = os.listdir(path) file_name = x[0] file_path = os.path.join(path, file_name) print(file_path) #csv file conversion if output == 'csv': netcdf_file_in = path + '\\' + file_name csv_file_out = path + '\\' + file_name[:-3] + '.csv' #function for file_conversion netCDF to csv ds = xr.open_dataset(netcdf_file_in) df = ds.to_dataframe() df.to_csv(csv_file_out) file_path = csv_file_out send_name = 'Microclim.csv' if output == 'netcdf': send_name = 'Microclim.nc' if output == 'GRIB': send_name = 'Microclim.grib' #Sendgrid mail mail(file_path, email, send_name) #Update the status of the mail to EMAILED requests.update_one({'_id': request_lkup['_id']}, {'$set': { 'status': "EMAILED" }}, upsert=False)
# retrieve all the div tags with a paritcular css element on the page divs = re.findall(r'<div class="dm-contentListItem__title">(.*?)</div>', str(data)) # loop for retrieving div tags for eachDiv in divs: hrefS = re.findall(r'title=(.*?)>', str(eachDiv)) #links = re.findall(r'href=(.*?)>', str(eachDiv)) for hRef in hrefS: # loop for retrieving only titles in the relevent divs #remove "" at first and last positions hRef = hRef[1::] hRef = hRef[:-1:] str_image_num = str(image_num) image_content = "https://picsum.photos/id/" + str_image_num + "/100/100" email_content = email_content + "<div style='border-style: groove;'>" \ + '<img src='+image_content+' alt="SCNQA Image"><b>'\ +hRef+"</b></div>" image_num = random.randint(0, 1000) sign_content = "<br/><br/>" + "Thanks," + "<br/>" + "Jakes" message = Mail(from_email='*****@*****.**', to_emails='*****@*****.**', subject=mailSubject, html_content=email_content + sign_content) try: sg = SendGridAPIClient(os.environ.get('SG Key')) response = sg.send(message) #print(response.status_code) #print(response.body) #print(response.headers) except Exception as e: print(e.message)
if member['deleted'] == True: updated = member['updated'] if updated > config['last-check'] and member['profile']['real_name']: when = datetime.fromtimestamp( member['updated']).strftime('%m/%d/%Y') email_contents += member['profile']['real_name'] + ' - ' + member[ 'profile']['title'] + ' (' + when + ')\n' if len(email_contents) == 0: print('Nothing to send!') else: print('Sending mail with contents:') print(email_contents) message = Mail(from_email=from_email, to_emails=to_email, subject='Deactivated users', plain_text_content=email_contents) 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(str(e)) config = {'last-check': time.time()} with open('config.json', 'w') as f: json.dump(config, f) print('Updated config to ' + str(config))