get sent or recieved emails from sendgrid
We're using SendGrid for email service for a project backend.
Now, we want to set up e2e tests for the project. To get a well-verified user, we have to know the verification code sent to the user's email at the time of the registration.
I've tried with the messages
endpoint as:
curl --request GET \
--url https://api.sendgrid.com/v3/messages \
--header 'Authorization: Bearer SG.xxxxxxxxxxxxxxxxxxxxxxxxx' \
--header 'Content-Type: application/json'
But the response says:
{"errors":[{"message":"authorization required"}]}
Is there any way that we can get the sent emails using the SendGrid API?
Thank you in advance.
do you know?
how many words do you know
See also questions close to this topic
-
How to fetch User Based Automation Stats via the Sendgrid API
I'm working on fetching the Sendgrid Automation Stats via the API, but I'm only able to get the stats of "Who opened/clicked the email?" rather than "How many opened/clicked the email?".
The API Documentation doesn't seem to indicate anything that would relay this information. Wanted to know if this is a possibility with the current API?
-
How to send email for a particular scheduled date and time ? [Schedule Email Feature]
I tried cron jobs for scheduling but it gets triggered for each and every day at that particular time.
Also workaround Gmail API for scheduling, but there isn't a feature in Gmail API My requirement is to send a scheduled email for the given time by the user.
Here I'm using Postgresql as DB and for backend Nest.js
Kindly help me to figure out this issue.
-
Retrieve data from my database and send it as an email using SendGrid in Python
I am a beginner and I am building my first web application. I will use SendGrid API to send emails to users. I already created an account on SendGrid and imported everything that I need and also already tested sending an email. It is working! Now I am having a hard time trying to figure out how I am going to retrieve data from my database and use SendGrid to send the email. I am using Python and my database is on Model.py (I am using postgresql).
Does anyone have any thoughts to help me?
Code in model.py:
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class User(db.Model): """A user.""" __tablename__ = "users" user_id = db.Column(db.Integer, autoincrement=True, primary_key=True) fname = db.Column(db.String) lname = db.Column(db.String) email = db.Column(db.String, unique=True) password = db.Column(db.String) photo = db.Column(db.String) letters = db.relationship("Letter", back_populates="user") favorites = db.relationship("Letter", secondary="favorites", back_populates="user") def __repr__(self): """Show info about user.""" return f'<User user_id={self.user_id} email={self.email}>' class Letter(db.Model): """A letter.""" __tablename__ = "letters" letter_id = db.Column(db.Integer, autoincrement=True, primary_key=True) letter_title = db.Column(db.String) letter_body = db.Column(db.String) creation_date = db.Column(db.Date) delivery_date = db.Column(db.Date) likes = db.Column(db.Integer) read = db.Column(db.Boolean) publish = db.Column(db.Boolean) user_id = db.Column(db.Integer, db.ForeignKey("users.user_id")) # create a relationship user = db.relationship("User", back_populates="letters") def __repr__(self): """Show info about letter.""" return f'<Letter letter_id={self.letter_id} letter_title={self.letter_title}>' """This is my server.py (this is just one of my routes)""" from flask import (Flask, render_template, request, flash, session, redirect) import random from model import connect_to_db, db import crud from jinja2 import StrictUndefined import os import cloudinary.uploader CLOUDINARY_KEY = os.environ['CLOUDINARY_KEY'] CLOUDINARY_SECRET = os.environ['CLOUDINARY_SECRET'] CLOUD_NAME = "dnw3idclo" app = Flask(__name__) app.secret_key = "dev" app.jinja_env.undefined = StrictUndefined @app.route("/registration", methods=["POST"]) def create_user(): """Create a user account.""" fname = request.form.get("fname") lname = request.form.get("lname") email = request.form.get("email") password = request.form.get("password") photo = request.files['my-file'] response= send_user_profile_pic(photo) if 'secure_url' in response: photo_url = response['secure_url'] else: photo_url = "" user = crud.get_user_by_email(email) if user: flash("You cannot create an account with that email. Please try again.") else: user = crud.create_user(fname=fname, lname=lname, email=email, password=password, photo=photo_url) db.session.add(user) db.session.commit() flash("Account created succesfully! Please log in.") """Also use crud functions to query data from my data base:""" from model import db, User, Letter, Favorite, connect_to_db def create_user(fname, lname, email, password, photo): """Create user's account.""" user_account = User(fname=fname, lname=lname, email=email, password=password, photo=photo) return user_account def get_user_by_email(email): """Return a user by email.""" return User.query.filter(User.email == email).first() def get_user_by_id(user_id): return User.query.get(user_id) def create_letter_for_user(letter_title, letter_body, creation_date, delivery_date, likes, read, publish, user_id): """Create letter for users.""" letter = Letter( letter_title=letter_title, letter_body=letter_body, creation_date=creation_date, delivery_date=delivery_date, likes=likes, read=read, publish=publish, user_id=user_id) return letter """And finally is my file for SendGrid:""" import sendgrid import os from sendgrid.helpers.mail import Mail, Email, To, Content from model import connect_to_db, db import crud from flask import (Flask, render_template, redirect, session, request,) from jinja2 import StrictUndefined app = Flask(__name__) app.secret_key = "dev" app.jinja_env.undefined = StrictUndefined # import model, crud, server, flask sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY')) from_email = Email("sender.test@gmail.com") # changed to my verified sender to_email = To("receiver.test@gmail.com") # the recipient that I want to send the email subject = "Letter to Your Future Self" content = Content("text/plain", "Test, test, test and more test") mail = Mail(from_email, to_email, subject, content) # Get a JSON-ready representation of the Mail object mail_json = mail.get() # Send an HTTP POST request to /mail/send response = sg.client.mail.send.post(request_body=mail_json) print(response.status_code) print(response.headers)
Thank you!
-
How to add JSON section tags to a SendGrid contact for parsing with handlebars templating language in dynamic email
A Brief Overview of the Problem
I am trying to store JSON data on my SendGrid contacts for usage in dynamic email templates designed in the SendGrid GUI.
To start, within the SendGrid email builder I can write the following code within a codeblock:
Here is the handlebar code in that code block...
<ol> {{#each user.accounts}} {{#if this.isPending}} <li> {{this.name}} is <strong>pending activation.</strong> </li> {{else}} <li> {{this.name}} is <strong>active.</strong> </li> {{/if}} {{/each}} </ol>
When I go to preview that code & add some test data I get the following:
Here is the JSON code in that code block formatted a bit nicer...
{ "user": { "accounts": [ { "name": "Checking", "isPending": false }, { "name": "401k", "isPending": true }, { "name": "Savings", "isPending": true } ] } }
The Question
Everything mentioned above is PERFECT so far - this is exactly what I want... To populate email data based on dynamic content present on each contact the email is going to. Here is where I hit the roadblock, where is that JSON Test Data coming from on the contact when the real email is sent out? And how do I populate a contact with JSON data using the API?
As far as I can tell, there is no option to add this custom JSON data to a new contact when creating one via the API (or via the GUI, for that matter) (see API docs here)
When I set up this email to send out to my SendGrid contacts via a SendGrid automation flow, does anyone know how to populate the JSON used by my code block for showing pending/activated accounts with data specific to each user?
Thank you, I greatly appreciate any help on this!