Skip to content

clients.sendgrid #

SendGrid Client

The SendGrid module allows you to use SendGrid services.

About SendGrid

SendGrid is a cloud-based email delivery and communication platform that empowers businesses and developers to send transactional and marketing emails to their customers or users. It offers tools and APIs to manage email campaigns, monitor delivery, and gather analytics on recipient engagement.

Requirements

To utilize this module, you will need:

  • A SendGrid API key: Create a SendGrid account and acquire your API key here.

Usage

To send an email using the SendGrid module, follow these steps:

1. Set Up a new email

In your V code, set up the email as shown below:

email := sendgrid.new_email(
  ['target_email@example.com', 'target_email2@example.com'],
  'source_email@example.com',
  'Email Title', 'Email content; can include HTML')

2. Execute the program

You can execute the program using the following command:

v run sendgrid/example/main.v -t 'YOUR_API_TOKEN'

You can provide the API key using the -t command-line argument, or you can export the API key using the following command:

export SENDGRID_AUTH_TOKEN='YOUR_API_TOKEN'

Additionally, you can enable debug mode by passing the -d flag:

v run sendgrid/example/main.v -d -t 'YOUR_API_TOKEN'

Advanced

We provide some useful structs and methods in email and personalization that you can leverage to tailor the emails according to your specific requirements. You can check the SendGrid API reference here

fn new_client #

fn new_client(token string) !Client

fn new_email #

fn new_email(to []string, from string, subject string, content string) Email

struct Client #

struct Client {
pub:
	token string
}

fn (Client) send #

fn (c Client) send(email Email) !

struct Content #

struct Content {
	type_ string = 'text/html' @[json: 'type']
	value string
}

struct Email #

struct Email {
pub mut:
	personalizations  []Personalizations @[required]
	from              Recipient          @[required]
	subject           string             @[required]
	content           []Content          @[required]
	reply_to          ?Recipient
	reply_to_list     ?[]Recipient
	attachments       ?[]Attachment
	template_id       ?string
	headers           ?map[string]string
	categories        ?[]string
	custom_args       ?string
	send_at           ?i64
	batch_id          ?string
	asm_              ?UnsubscribeGroups @[json: 'asm']
	ip_pool_name      ?string
	mail_settings     ?MailSettings
	tracking_settings ?TrackingSettings
}

fn (Email) add_personalization #

fn (mut e Email) add_personalization(personalizations []Personalizations)

fn (Email) add_content #

fn (mut e Email) add_content(content []Content)

fn (Email) add_headers #

fn (mut e Email) add_headers(headers map[string]string)

struct Personalizations #

@[params]
struct Personalizations {
pub mut:
	to                    []Recipient @[required]
	from                  ?Recipient
	cc                    ?[]Recipient
	bcc                   ?[]Recipient
	subject               ?string
	headers               ?map[string]string
	substitutions         ?map[string]string
	dynamic_template_data ?map[string]string
	custom_args           ?map[string]string
	send_at               ?i64
}