sendgrid / sendgrid-go Goto Github PK
View Code? Open in Web Editor NEWThe Official Twilio SendGrid Golang API Library
Home Page: https://sendgrid.com
License: MIT License
The Official Twilio SendGrid Golang API Library
Home Page: https://sendgrid.com
License: MIT License
Acceptance Criteria:
Reference:
I'm trying to use the sendgrid-go client to test ISMTPD, but I'm getting an Invalid Request error when I try to send an email. I'm not getting this when I do a direct request using curl. Have you seen this before? If not, I can look into it some more.
func TestHTTPSendWithAPIKey(t *testing.T) {
t.Logf("User should be able to send mail using API Key for authentication")
apiKey := util.AddAPIKey(t, 180)
sg := sendgrid.NewSendGridClientWithApiKey(apiKey.Secret)
sg.APIMail = httpEndpoint + ".json"
sg.Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
message := sendgrid.NewMail()
message.AddTo("[email protected]")
message.SetSubject("SendGrid Testing")
message.SetText("WIN")
message.SetFrom("[email protected]")
assert.NoError(t, sg.Send(message))
util.DeleteAPIKey(t, 180, apiKey.ID)
}
Test results:
--- FAIL: TestHTTPSendWithAPIKey-2 (0.13s)
send_http_test.go:259: User should be able to send mail using API Key for authentication
apikeys.go:45: Added API Key &{Secret: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 ID:jwAv5Ea_TbyH4kfyOGGFpQ Name:ismtpd_acceptance 2015-05-19 19:15:46.26892427 +0000 UTC}
Location: send_http_test.go:278
Error: No error is expected but got sendgrid.go: code:500 error:<nil> body:{"message": "error", "errors": ["Invalid Request"]}
Curl command:
$ curl -X "POST" "http://dock:50350/api/mail.send.json/api/mail.send.json" \
> -H "Authorization: Bearer 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" \
> --data-urlencode "[email protected]" \
> --data-urlencode "subject=test" \
> --data-urlencode "[email protected]" \
> --data-urlencode "text=hello world"
{"message":"success"}
Acceptance Criteria:
Reference:
Remove the dependency to https://github.com/sendgrid/rest, bringing the http client functionality into the library.
Once this one is closed, I can merge this: sendgrid/rest#14
We can't send a mail via v3 API with multibyte content.
func TestSendGrid_bare(t *testing.T) {
m := mail.NewV3Mail()
m.SetFrom(mail.NewEmail("ๆฅๆฌ่ชๅ1", "[email protected]"))
m.Subject = "ๆฅๆฌ่ชใฟใคใใซ"
m.AddContent(mail.NewContent("text/html", "๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ"))
p := mail.NewPersonalization()
p.AddTos(mail.NewEmail("ๆฅๆฌ่ชๅ2", "[email protected]"))
m.AddPersonalizations(p)
request := sendgrid.GetRequest("<my api key>", "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = mail.GetRequestBody(m)
response, err := sendgrid.API(request)
if err != nil {
t.Error(err)
} else if response.StatusCode < 200 || 300 <= response.StatusCode{
t.Error(response.StatusCode)
t.Error(response.Body)
t.Error(response.Headers)
} else {
t.Log(response.StatusCode)
t.Log(response.Body)
t.Log(response.Headers)
}
}
func TestSendGrid_emoji(t *testing.T) {
m := mail.NewV3Mail()
m.SetFrom(mail.NewEmail("foo", "[email protected]"))
m.Subject = "title"
m.AddContent(mail.NewContent("text/plain", "๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ๐ฑ๐ธ"))
p := mail.NewPersonalization()
p.AddTos(mail.NewEmail("fizzbuzz", "[email protected]"))
m.AddPersonalizations(p)
request := sendgrid.GetRequest("<my api key>", "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = mail.GetRequestBody(m)
response, err := sendgrid.API(request)
if err != nil {
t.Error(err)
} else if response.StatusCode < 200 || 300 <= response.StatusCode{
t.Error(response.StatusCode)
t.Error(response.Body)
t.Error(response.Headers)
} else {
t.Log(response.StatusCode)
t.Log(response.Body)
t.Log(response.Headers)
}
}
=== RUN TestSendGrid_bare
--- FAIL: TestSendGrid_bare (0.89s)
sendgrid_test.go:30: 415
sendgrid_test.go:31: {"errors":[{"message":"Unrecognized UTF8 Byte at position 511","field":null,"help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Encoding-Errors"}]}
sendgrid_test.go:32: map[Server:[nginx] Date:[Thu, 23 Jun 2016 04:21:42 GMT] Content-Type:[application/json] Content-Length:[171] Connection:[keep-alive] X-Frame-Options:[DENY]]
=== RUN TestSendGrid_emoji
--- FAIL: TestSendGrid_emoji (0.09s)
sendgrid_test.go:59: 415
sendgrid_test.go:60: {"errors":[{"message":"Unrecognized UTF8 Byte at position 511","field":null,"help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Encoding-Errors"}]}
sendgrid_test.go:61: map[Content-Length:[171] Connection:[keep-alive] X-Frame-Options:[DENY] Server:[nginx] Date:[Thu, 23 Jun 2016 04:21:42 GMT] Content-Type:[application/json]]
I'm posted this issue to SendGrid support. Request # 691762
Yes, actually it's great v3 is here. This is cool. We can all agree on that. What's not cool is the fact that there are zero instructions about using the v2 library in case of emergency.
Why would anyone want to use the old version? Maybe those who've built shizzle around that API - those using the v2 library. Those who just need to ship a quick change w/o taking loadsa time to re-write their whole application.
Guys and girls, please provide some backwards compatibility instructions for those who can't upgrade right now. This, for example, does not help:
BREAKING CHANGE as of 2016.06.14
Put this in your readme please:
import gopkg.in/sendgrid/sendgrid-go.v2
Over and out
It'd be nice if there was support for incoming mail.
Several of you have voiced concerned about the recent breaking changes to the library. As we move forward, there will be more breaking changes. This issue aims to provide a solution that will make those transitions smoother.
So far we have heard the following options:
Please let us know what you think
Thanks!
I wrote and ran this code:
package main
import (
"fmt"
sendgrid "github.com/sendgrid/sendgrid-go"
)
func main() {
mail := sendgrid.NewMail()
mail.AddTo("MY_EMAIL_ADDRESS")
mail.SetFrom("MY_EMAIL_ADDRESS")
mail.SetFromName("deltam")
mail.SetSubject("test mail")
mail.SetText("testing mail send now.")
mail.AddAttachmentFromStream("test.txt", "test,test")
sg := sendgrid.NewSendGridClient("MY_USER_ID", "MY_PASSWORD")
if r := sg.Send(mail); r == nil {
fmt.Println("Email sent!")
} else {
fmt.Println(r)
}
}
I expect received email that has attachment("test.txt"), but attachment is nothing.
I tried official document example(using cURL). Received email has attachment.
Is AddAttachmentFromStream()
along API?
https://sendgrid.com/docs/API_Reference/Web_API/mail.html#-send
files
Parameter is filename.
// AddRecipient will add mail.Address emails to recipients.
func (m *SGMail) AddRecipient(recipient *mail.Address) {
m.To = append(m.To, recipient.Address)
if recipient.Name != "" {
m.ToName = append(m.ToName, recipient.Name)
}
}
The docs say "toname must be an array with the exact number of array elements as the to field".
In the code: response, err := sendgrid.API(request)
, err is nil, even when there are errors. I have to inspect: response.StatusCode
to see if the status is 200 or not and then response.Body
to find the error. I see the response provides an array of errors, so maybe the first error should be returned.
Try to send an email with an invalid API key, check err != nil. Also happened for an invalid email.
400
{"errors":[{"message":"Invalid from email address [email protected]:10433","field":"from","help":null}]}
map[Server:[nginx] Date:[Mon, 07 Nov 2016 14:20:55 GMT] Content-Type:[application/json] Content-Length:[109] Connection:[keep-alive] X-Frame-Options:[DENY]]
Hi,
I'm trying to integrated my application using this Go library.
My objective is to have emails to be sent at a point in time and so I noticed there is a mail_v3.go file which contains a 'SendAt' field; I assume this is the field to populate to achieve my objective.
However, when i try to Send the message, there is a type conflict ("cannot use message (type *sendgrid.SGMailV3) as type *sendgrid.SGMail"). This is understandable as it's using the old sendgrid.go method of sending.
My question is: Is there a new send method for SGMailV3 type? like a sendgrid_v3.go? If there isn't, is there an alternative solution i can use? It would require to use a SendGrid email template.
It just feels like there's a missing piece to send SGMailV3 type mail.
Many Thanks
Acceptance Criteria:
Reference:
Acceptance Criteria:
Reference:
As a result of your feedback and as a continuation of the execution of our long term roadmap, there will be breaking changes coming soon. The last time we had a major breaking change, it was for the implementation of v3 Web API support. We announced those changes here on GitHub along with some instructions on how to test and provide feedback.
The feedback we got was amazing, but we didn't quite get the amount or thoroughness of feedback we were hoping for.
We want to continue improving this iterative process, so we are reaching out to you for feedback in order to determine the optimum way to move forward as this library is designed to be community driven, SendGrid led.
Please take a moment to share your thoughts with us.
Following are some ideas we are considering, we will likely choose one from Execution and one from Communication, but not all of the items below.
Execution (for large changes):
Communication:
How do you prefer to get these announcements? Is this too much? Too little? Please let us know!!
As always, we are listening carefully and are looking forward to working with you.
Note: We will always follow the semver.org convention of using major point releases to signify breaking changes. Please DO NOT auto-update your dependencies. It is important to take a look at the CHANGELOG or releases to find out how the breaking changes will impact your code.
After using this api to send email. Email is SENT / BOUNCED etc.
I have created a event webhook to know what are all the mails that are bounced, so that I can trigger further things within my application.
To correlate between sent mail and webhook, are there any identifiers. From the docs, couldn't identify them. Kindly, let me know if there are anything else that need to done.
Hi, I was just trying out the API and found a bug in the sample that you guys show on the company homepage.
Current version:
// using SendGrid's Go Library - https://github.com/sendgrid/sendgrid-go
package main
import ("github.com/sendgrid/sendgrid-go")
func main() {
sendgrid := sendgrid.NewSendGridClient(api_user, api_key)
message := sendgrid.NewMail() // <-- Error here "sendgrid" here refers to the client and not the lib import
message.AddTo("[email protected]")
message.SetFrom("[email protected]")
message.SetSubject("Sending with SendGrid is Fun")
message.SetHTML("and easy to do anywhere, even with Go")
sendgrid.Send(message)
}
Fixed version:
// using SendGrid's Go Library - https://github.com/sendgrid/sendgrid-go
package main
import ("github.com/sendgrid/sendgrid-go")
func main() {
client := sendgrid.NewSendGridClient(api_user, api_key)
message := sendgrid.NewMail() // <-- Now "sendgrid" refers to the lib import as expected
message.AddTo("[email protected]")
message.SetFrom("[email protected]")
message.SetSubject("Sending with SendGrid is Fun")
message.SetHTML("and easy to do anywhere, even with Go")
client.Send(message)
}
Using "client" as the client name avoids this naming bug.
I know this is not specifically a bug in the driver, but I thought it was better to go via Github than trying to explain this to some support person that can't read Go code...
Best Regards, Robert
How would I go about testing if the API Key works/verified?
We would like to add support for v3 Web API rate limits, as described here.
How would you like this to be implemented? Or please give a thumbs up to express your interest in us implementing to move this task up our queue.
PRs are always welcome as well :)
This pull request is going to be a breaking change.
Adding vendoring will help us manage these breaks easier. We want to avoid surprising breaking changes. Note that we will continue to follow semver.org conventions.
Why delete SGClient out of nowhere and replace it by sending requests to the API? I think the main idea of using a library is to hide the actual calls to the API and wrap them inside a client.
The new v3 package seems to be much more low-level and more difficult to use.
Can you consider writing a higher level wrapper that optimizes for the person writing the code?
See the difference here:
https://github.com/GoogleCloudPlatform/golang-samples/pull/59/files
We are experiencing an issue with Sendgrid's invaluable feature, UniqueArgs, often not being set on the new mail. We have confirmed that we are definitely setting the unique arg to a non-empty string value, but we don't see the unique arg in Sendgrid's Event Notification POSTs, and Sendgrid's logs don't show any such arg from the client.
Why might the mtmsg_id unique arg. be set?
Here is the relevant application code:
(mtContent and mtMessage are structs from the application)
var sgClient = sendgrid.NewSendGridClient("username", "password");
var message = sendgrid.NewMail();
message.AddUniqueArg("mtmsg_id", mtMessage.Id);
message.AddTo(mtMessage.To);
message.AddToName(mtContent.SenderName);
message.SetReplyTo(mtContent.ReplyTo);
message.SetSubject(mtContent.Subject);
message.SetHTML(mtContent.Html);
message.SetText(mtContent.Text);
message.SetFrom(mtMessage.From);
if r := sgClient.Send(message); r == nil {
// Email sent succesfully!
// Successfully sent, set sync success!
} else {
// Failure handling
}
This is in production, so any help is appreciated!
The API won't allow disabling subscription tracking because the Enable parameter won't be sent when it's false, returning the error: "The subscription_tracking enable parameter is required."
Send an email with the following options:
trackingSettings := mail.NewTrackingSettings()
subscriptionTrackingSetting := mail.NewSubscriptionTrackingSetting()
subscriptionTrackingSetting.SetEnable(false)
trackingSettings.SetSubscriptionTracking(subscriptionTrackingSetting)
m.SetTrackingSettings(trackingSettings)
The following error will be returned:
&{400 {"errors":[{"message":"The subscription_tracking enable parameter is required.","field":"tracking_settings.subscription_tracking.enable","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.tracking_settings.subscription_tracking.enable"}]} map[X-Frame-Options:[DENY] Server:[nginx] Date:[Fri, 01 Jul 2016 12:09:18 GMT] Content-Type:[application/json] Content-Length:[270] Connection:[keep-alive]]}
This happens because the Enabled field of the SubscriptionTrackingSetting struct has the JSON options json:"enable,omitempty"
therefore false is not encoded. See http://stackoverflow.com/questions/37756236/json-golang-boolean-omitempty for more information.
Hi @eddiezane
What do you think about simplifying the API for this codebase? Right now we have duplicate methods that look similar to each other:
Example:
AddCc( cc string)
AddCcs ( ccs []string)
And what I'm thinking is we could actually simplify the API's by removing the plural/non-plural named methods with methods that are variadic.
Example:
AddCc(cc...string)
This way a single method can be used either singularly or variadicly (is that even a word?) as in:
http://play.golang.org/p/hwcWipk_VV
Curious to hear your thoughts on this.
The examples are all well and good for extremely simple mail, but how do I send sendgrid templated mail, with substitutions, etc?
I am hoping you don't have me writing json by hand... friends don't let friends write json by hand...
Here is an example of my code to send mail with templates using sendgrid v2:
import "github.com/sendgrid/sendgrid-go"
func main() {
msg := sendgrid.NewMail()
msg.SMTPAPIHeader.AddFilter("templates", "enable", "1")
msg.SMTPAPIHeader.AddFilter("templates", "template_id", "<template id>")
loginLink := fmt.Sprintf("https://%s/dashboard?login&email=%s", domain, url.QueryEscape(emailAddress))
msg.SMTPAPIHeader.AddSubstitution("[link]", link)
// The templates already contain the contents,
// but we must append something for the library to work...
msg.SetText("\n")
msg.SetHTML("\n")
msg.SetSubject("Welcome to ...")
if err := msg.AddTo(emailAddress); err != nil {
panic("Unable to parse email address")
}
msg.SetFromName("...")
if err := msg.SetFrom("[email protected]"); err != nil {
panic("Unable to parse email address")
}
if err := msg.SetReplyTo("[email protected]"); err != nil {
panic("Unable to parse email address")
}
sendgrid.NewSendGridClientWithApiKey(unsubSendgridAPIKey).Send(email)
}
I'm getting non-predictable 406 errors. Code issuing the requests is exactly the same whether it fails or succeeds. This is how the error looks like:
sendgrid.go: code:406 error:<nil> body:<html>
<head><title>406 Not Acceptable</title></head>
<body bgcolor="white">
<center><h1>406 Not Acceptable</h1></center>
<hr><center>nginx</center>
</body>
</html>
This is the code, which is pretty much copied over from README:
sg := sendgrid.NewSendGridClientWithApiKey(os.Getenv("SENDGRID_API_KEY"))
// set http.Client to use the appengine client
sg.Client = urlfetch.Client(*c) //Just perform this swap, and you are good to go.
message := sendgrid.NewMail()
message.AddTo(...)
message.SetFrom(...)
message.SetSubject(...)
message.SetHTML(...)
sg.Send(message)
Any ideas?
Sending transactional templates doesn't seem to be working. If no message text is set, getting a "Missing email body" error.
Could be that it's not set up correctly, too, so here's the basic attempts I've made, where message
is a *SGMail
interface:
message.AddHeader("X-SMTPAPI", `{"filters":{"templates": {"settings": {"enable": 1, "template_id": "<template-id-here>"} } } }`)
and also via the SMTPAPIHeader
option:
templateFilter := smtpapi.Filter{Settings: make(map[string]string)}
templateFilter.Settings["enable"] = "1"
templateFilter.Settings["template_id"] = sendgridTemplate
message.SMTPAPIHeader.SetFilter("templates", &templateFilter)
If do one/both of the above, and also set the text, the text that's set is what is being send, not the template contents.
To be clear, everything is compiling, etc., it's just not using the template.
Also noticing that there is a SGMailV3 struct available, and maybe that's what I need, since it does look like there is support for templates there, but it's not clear how to use it.
Hello,
The example code used in the Golang section on Heroku is out of date. sendgrid. NewSendGridClient() doesn't appear to exist anymore. The examples on the Github seem to work great.
See sendgrid/rest#5
The code is more complex than it needs to be. Simplify it.
The append builtin works with a nil slice as the first argument. The code
if s.Personalizations == nil {
s.Personalizations = make([]*Personalization, 0)
}
s.Personalizations = append(s.Personalizations, p...)
can be written as:
s.Personalizations = append(s.Personalizations, p...)
This pattern occurs several places throughout the code.
The code json.Marshal function returns a newly allocated []byte for the application. There's no need to copy the value using []byte(string(p))
. The code:
b, err := json.Marshal(m)
if err != nil {
fmt.Println(err)
}
return []byte(string(b))
can be simplified to:
b, err := json.Marshal(m)
if err != nil {
fmt.Println(err)
}
return b
If you feel you must copy the []byte for some reason, then use:
bcopy := make([]byte, len(b))
copy(bcopy, b)
This uses one less allocation than []byte(string(b)).
While I am here, I'll note that printing errors to standard out is not an acceptable error handling strategy.
# github.com/sendgrid/sendgrid-go
/home/vagrant/src/goenv/src/github.com/sendgrid/sendgrid-go/sendgrid.go:38: m.JsonString undefined (type SGMail has no field or method JsonString)
/home/vagrant/src/goenv/src/github.com/sendgrid/sendgrid-go/sendgrid_mail.go:54: m.Mail.AddRecipient undefined (type smtpmail.Mail has no field or method AddRecipient)
The programming language is Go, not GO (think SendGrid, not SENDGRID)
:)
My app is running on Google App Engine in Go language. I sent emails out from my app to hotmail and gmail accounts with 202 response with no body from sendgrid server. However, there is no email being sent to any mailbox I have tested.
How can I track the issue furthermore the fix the problem. According to the doc, 202 response means the email is being queued and will be delivered. When I checked it on sendgrid dashboard, there was no email on the queue or was delivered at all.
The code snippet is below. There was no error received just no email getting into the mailbox.
`
sendgrid.DefaultClient.HTTPClient = urlfetch.Client(ctx)
from := mail.NewEmail("ttt", "[email protected]")
subject := "subject"
to := mail.NewEmail("Name", "[email protected]")
content := mail.NewContent("text/html", invitationMessage)
m := mail.NewV3MailInit(from, subject, to, content)
//API_KEY is a const for the API_KEY
request := sendgrid.GetRequest(API_KEY, "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = mail.GetRequestBody(m)
response, err := sendgrid.API(request)
if err != nil {
PrintJsonErrorResponse(err, w, false)
return
} else {
_responseMessage := responseMessage{}
_responseMessage.Msg = strconv.Itoa(response.StatusCode) + " : " + response.Body
_responseMessage.Success = true
jsonResponse, _ := json.Marshal(_responseMessage)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", jsonResponse)
}`
golint is very useful tool.
please try it!
go get -u github.com/golang/lint/golint
golint ./...
helpers/mail/mail_v3.go:28:6: exported type Personalization should have comment or be unexported
Acceptance Criteria:
Reference:
I successfully sent an email using this package - got 2xx and received email as expected. However, the response that comes back did not include rate limit headers (X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset) as described here: https://sendgrid.com/docs/API_Reference/Web_API_v3/How_To_Use_The_Web_API_v3/rate_limits.html
Is there another way to access the rate limit information, or is it not included in the response to every request?
Headers from response:
map[Server:[nginx] Date:[Wed, 25 Jan 2017 19:59:59 GMT] Content-Type:[text/plain; charset=utf-8] Content-Length:[0] Connection:[keep-alive] X-Message-Id:[...] X-Frame-Options:[DENY]]
sendgrid-go Version: v3.3.1
Go Version: 1.7
Thanks!
Acceptance Criteria:
Reference:
Code:
message := sendgrid.NewMail()
message.AddTo("[email protected]")
message.SetSubject("My subject")
message.SetHTML("This is an email")
message.SetFrom("Test")
Yields:
sendgrid.go: code:400 error:<nil> body:{"errors":["Empty from email address (required)"],"message":"error"}
Surely the error message should explain what the error is rather than throwing a generic catch-all?
I'm not sure what you want to do about the error returned with Send but it completely swallows/hides what happens with SendAPI. Maybe returning an appengine/multierror ([]error)?
For testing purposes I've not been able to find a way to mock out any of the sendgrid-go library (or smtpapi-go for that matter). Even defining my own local interfaces and mocking those I am defeated by the public API using specific types (Send in particular). It would be great if there were 2 interfaces:
type SGClient interface {
Send(SGMail) error
}
type SGMail interface {
AddTo(string) error
AddTos([]string) error
AddRecipient(*mail.Address)
...
}
Then the existing SGClient and SGMail should satisfy these interfaces. The AWS Go SDK for example provides an interface for each service and it makes testing very easy. This should be possible without any breaking changes, the only API change would be to SGClient.Send
going from Send(m *sendgrid.SGMail)
to Send(m sendgrid.SGMailInterface)
.
Hi all i tried sending attachment but it didnt work.
Heres my codes:
....
file, err := os.Open("file.go")
fileName := "test.txt"
message.AddAttachment(fileName, file)
err := sg.Send(message)
....
there's no error produced, it actually sent the email but without the attachment.
Thanks in advance!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.