### Install gosparkpost Source: https://github.com/sparkpost/gosparkpost/blob/master/README.rst Install the gosparkpost package from GitHub using the go get command. ```bash $ go get github.com/SparkPost/gosparkpost ``` -------------------------------- ### Clone gosparkpost Repository Source: https://github.com/sparkpost/gosparkpost/blob/master/CONTRIBUTING.md Use `go get` to clone the original gosparkpost repository to your Go workspace. ```go go get https://github.com/SparkPost/gosparkpost ``` -------------------------------- ### Send a Message using gosparkpost Source: https://github.com/sparkpost/gosparkpost/blob/master/README.rst This example demonstrates how to initialize the SparkPost client with an API key from environment variables and send a basic HTML email transmission. Ensure the SPARKPOST_API_KEY environment variable is set. ```go package main import ( "log" "os" sp "github.com/SparkPost/gosparkpost" ) func main() { // Get our API key from the environment; configure. apiKey := os.Getenv("SPARKPOST_API_KEY") cfg := &sp.Config{ BaseUrl: "https://api.sparkpost.com", ApiKey: apiKey, ApiVersion: 1, } var client sp.Client err := client.Init(cfg) if err != nil { log.Fatalf("SparkPost client init failed: %s\n", err) } // Create a Transmission using an inline Recipient List // and inline email Content. tx := &sp.Transmission{ Recipients: []string{"someone@somedomain.com"}, Content: sp.Content{ HTML: "

Hello world

", From: "test@sparkpostbox.com", Subject: "Hello from gosparkpost", }, } id, _, err := client.Send(tx) if err != nil { log.Fatal(err) } // The second value returned from Send // has more info about the HTTP response, in case // you'd like to see more than the Transmission id. log.Printf("Transmission sent with id [%s]\n", id) } ``` -------------------------------- ### Send HTML Email with Attachment Source: https://github.com/sparkpost/gosparkpost/blob/master/cmd/sparks/README.md This example demonstrates sending an HTML email with a file attachment. Ensure the attachment path and filename are correctly specified. ```bash $ sparks -from att@sp.example.com -html 'Did you get that thing I sent you?' \ -to you@example.com.sink.sparkpostmail.com -subject 'that thing' \ -attach image/jpg:thing.jpg:$HOME/test/thing.jpg ``` -------------------------------- ### Cc and Bcc Recipients Example Source: https://github.com/sparkpost/gosparkpost/blob/master/examples/README.md This snippet demonstrates how to structure a transmission to include Cc and Bcc recipients. It shows how to set individual recipient addresses and the common 'header_to' field, as well as the 'cc' header within the content. ```json { "recipients": [ { "address": { "email": "to1@test.com.sink.sparkpostmail.com", "header_to": "to1@test.com.sink.sparkpostmail.com,to2@test.com.sink.sparkpostmail.com" } }, { "address": { "email": "to2@test.com.sink.sparkpostmail.com", "header_to": "to1@test.com.sink.sparkpostmail.com,to2@test.com.sink.sparkpostmail.com" } }, { "address": { "email": "cc1@test.com.sink.sparkpostmail.com", "header_to": "to1@test.com.sink.sparkpostmail.com,to2@test.com.sink.sparkpostmail.com" } }, { "address": { "email": "cc2@test.com.sink.sparkpostmail.com", "header_to": "to1@test.com.sink.sparkpostmail.com,to2@test.com.sink.sparkpostmail.com" } }, { "address": { "email": "bcc1@test.com.sink.sparkpostmail.com", "header_to": "to1@test.com.sink.sparkpostmail.com,to2@test.com.sink.sparkpostmail.com" } }, { "address": { "email": "bcc2@test.com.sink.sparkpostmail.com", "header_to": "to1@test.com.sink.sparkpostmail.com,to2@test.com.sink.sparkpostmail.com" } } ], "content": { "text": "This is a cc/bcc example", "subject": "cc/bcc example message", "from": "test@example.com", "headers": { "cc": "cc1@test.com.sink.sparkpostmail.com,cc2@test.com.sink.sparkpostmail.com" } } } ``` -------------------------------- ### Format Code with go fmt Source: https://github.com/sparkpost/gosparkpost/blob/master/CONTRIBUTING.md Run `go fmt` to ensure your code adheres to Go formatting standards before submitting. ```go go fmt ``` -------------------------------- ### Run Go Tests Source: https://github.com/sparkpost/gosparkpost/blob/master/CONTRIBUTING.md Execute `go test` to run all tests in your current Go environment. Ensure all automated tests pass before submitting a pull request. ```go go test ``` -------------------------------- ### Generate and Preview FBL Report Source: https://github.com/sparkpost/gosparkpost/blob/master/cmd/fblgen/README.md Use fblgen to generate an FBL report from a local email file and preview the sending details. Add the --send flag to actually send the report. ```bash ./fblgen --file ./test.eml --verbose Got domain [sparkpostmail.com] from Return-Path Got MX [smtp.sparkpostmail.com.] for [sparkpostmail.com] Would send FBL from [test@sp.example.com] to [fbl@sparkpostmail.com] via [smtp.sparkpostmail.com.:smtp] ``` -------------------------------- ### Send OOB Bounce and Save to File Source: https://github.com/sparkpost/gosparkpost/blob/master/cmd/oobgen/README.md Use this command to simulate sending an out-of-band bounce and save the full email headers to a local file for inspection. Add the --send flag to actually attempt sending. ```bash $ ./oobgen --file ./test.eml --verbose Got domain [sparkpostmail.com] from Return-Path Got MX [smtp.sparkpostmail.com.] for [sparkpostmail.com] Would send OOB from [test@sp.example.com] to [verp@sparkpostmail.com] via [smtp.sparkpostmail.com.:smtp] ``` -------------------------------- ### Test Port 25 Connection with netcat Source: https://github.com/sparkpost/gosparkpost/blob/master/cmd/fblgen/README.md Verify connectivity to the SparkPost mail server on port 25 using netcat. This helps diagnose potential firewall or ISP blocks. ```bash nc -vz -w 3 smtp.sparkpostmail.com 25 ``` -------------------------------- ### Push Code to Fork Source: https://github.com/sparkpost/gosparkpost/blob/master/CONTRIBUTING.md Push your local changes to your fork using `git push` with the 'fork' remote and the current HEAD. ```git git push fork HEAD ``` -------------------------------- ### Test Port 25 Connection with ncat Source: https://github.com/sparkpost/gosparkpost/blob/master/cmd/fblgen/README.md An alternative method to test port 25 connectivity using ncat, suitable for sending data and checking connection status. ```bash