mail.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package mail provides the means of sending email from an
  6. App Engine application.
  7. Example:
  8. msg := &mail.Message{
  9. Sender: "romeo@montague.com",
  10. To: []string{"Juliet <juliet@capulet.org>"},
  11. Subject: "See you tonight",
  12. Body: "Don't forget our plans. Hark, 'til later.",
  13. }
  14. if err := mail.Send(c, msg); err != nil {
  15. log.Errorf(c, "Alas, my user, the email failed to sendeth: %v", err)
  16. }
  17. */
  18. package mail // import "google.golang.org/appengine/mail"
  19. import (
  20. "net/mail"
  21. "github.com/golang/protobuf/proto"
  22. "golang.org/x/net/context"
  23. "google.golang.org/appengine/internal"
  24. bpb "google.golang.org/appengine/internal/base"
  25. pb "google.golang.org/appengine/internal/mail"
  26. )
  27. // A Message represents an email message.
  28. // Addresses may be of any form permitted by RFC 822.
  29. type Message struct {
  30. // Sender must be set, and must be either an application admin
  31. // or the currently signed-in user.
  32. Sender string
  33. ReplyTo string // may be empty
  34. // At least one of these slices must have a non-zero length,
  35. // except when calling SendToAdmins.
  36. To, Cc, Bcc []string
  37. Subject string
  38. // At least one of Body or HTMLBody must be non-empty.
  39. Body string
  40. HTMLBody string
  41. Attachments []Attachment
  42. // Extra mail headers.
  43. // See https://cloud.google.com/appengine/docs/standard/go/mail/
  44. // for permissible headers.
  45. Headers mail.Header
  46. }
  47. // An Attachment represents an email attachment.
  48. type Attachment struct {
  49. // Name must be set to a valid file name.
  50. Name string
  51. Data []byte
  52. ContentID string
  53. }
  54. // Send sends an email message.
  55. func Send(c context.Context, msg *Message) error {
  56. return send(c, "Send", msg)
  57. }
  58. // SendToAdmins sends an email message to the application's administrators.
  59. func SendToAdmins(c context.Context, msg *Message) error {
  60. return send(c, "SendToAdmins", msg)
  61. }
  62. func send(c context.Context, method string, msg *Message) error {
  63. req := &pb.MailMessage{
  64. Sender: &msg.Sender,
  65. To: msg.To,
  66. Cc: msg.Cc,
  67. Bcc: msg.Bcc,
  68. Subject: &msg.Subject,
  69. }
  70. if msg.ReplyTo != "" {
  71. req.ReplyTo = &msg.ReplyTo
  72. }
  73. if msg.Body != "" {
  74. req.TextBody = &msg.Body
  75. }
  76. if msg.HTMLBody != "" {
  77. req.HtmlBody = &msg.HTMLBody
  78. }
  79. if len(msg.Attachments) > 0 {
  80. req.Attachment = make([]*pb.MailAttachment, len(msg.Attachments))
  81. for i, att := range msg.Attachments {
  82. req.Attachment[i] = &pb.MailAttachment{
  83. FileName: proto.String(att.Name),
  84. Data: att.Data,
  85. }
  86. if att.ContentID != "" {
  87. req.Attachment[i].ContentID = proto.String(att.ContentID)
  88. }
  89. }
  90. }
  91. for key, vs := range msg.Headers {
  92. for _, v := range vs {
  93. req.Header = append(req.Header, &pb.MailHeader{
  94. Name: proto.String(key),
  95. Value: proto.String(v),
  96. })
  97. }
  98. }
  99. res := &bpb.VoidProto{}
  100. if err := internal.Call(c, "mail", method, req, res); err != nil {
  101. return err
  102. }
  103. return nil
  104. }
  105. func init() {
  106. internal.RegisterErrorCodeMap("mail", pb.MailServiceError_ErrorCode_name)
  107. }