ارسال یک ایمیل
یک پیام، اکنون یا بعداً، با پیوست، یا صد پیام در یک درخواست.
فرستادن یک پیام
sent, err := client.Emails.Send(ctx, openemail.Body{ "from": "Acme Billing <[email protected]>", "to": []string{"[email protected]", "[email protected]"}, "replyTo": "[email protected]", "subject": "Your September invoice", "html": "<p>The invoice is attached.</p>", "text": "The invoice is attached.", "tags": openemail.Body{"invoice": "inv_2026_09_4192"},})if err != nil { return err} fmt.Println(sent.ID(), sent.String("status"))to، cc و bcc یک نشانی یا یک فهرست میگیرند، و نشانی متنی مانند Ada <[email protected]> یا یک map با email و name است. بدنه از html و text، از یک template ذخیرهشده یا از یک draftId میآید.
هر ارسال یک کلید یکتایی دارد. کلاینت برای هر فراخوانی یکی میسازد و هنگام تلاش دوباره همان را به کار میبرد، پس درخواستِ دوبارهامتحانشده پیام نخست را بازپخش میکند. openemail.WithIdempotencyKey را بدهید تا این میان فرایندها و راهاندازیهای دوباره هم برقرار بماند.
پیوستها
invoice, err := os.ReadFile("invoice.pdf")if err != nil { return err} _, err = client.Emails.Send(ctx, openemail.Body{ "from": "[email protected]", "to": "[email protected]", "subject": "Your September invoice", "text": "The invoice is attached.", "attachments": []openemail.Body{ {"filename": "invoice.pdf", "content": invoice, "contentType": "application/pdf"}, {"fileId": "file_3f9c2a7b1e4d8f60a5c7b92d"}, },}) return errcontent یک []byte یا یک io.Reader میگیرد که کلاینت آن را به base64 رمزگذاری میکند، یا متنی که از پیش base64 است. fileId پروندهای را نام میبرد که با client.Files.Upload بارگذاری شده، و پروندهٔ بزرگتر از 5 مگابایت همینطور فرستاده میشود.
فرستادن در آینده
scheduled, err := client.Emails.Send(ctx, openemail.Body{ "from": "[email protected]", "to": "[email protected]", "subject": "Your invoice is due tomorrow", "text": "A reminder that the invoice is due tomorrow.", "scheduledAt": time.Now().Add(24 * time.Hour),})if err != nil { return err} if _, err := client.Emails.Reschedule(ctx, scheduled.ID(), time.Now().Add(48*time.Hour)); err != nil { return err} cancelled, err := client.Emails.Cancel(ctx, scheduled.ID())if err != nil { return err} fmt.Println(cancelled.String("status"))scheduledAt یک time.Time، یک لحظهٔ ISO 8601 یا مدتی مانند PT1H میگیرد. هر time.Time در هر جای بدنه بهصورت یک لحظهٔ ISO 8601 در UTC فرستاده میشود. پیام زمانبندیشده را تا پیش از رفتنش میتوان تغییر داد یا لغو کرد.
ارسال دستهای
result, err := client.Emails.SendBatch(ctx, []openemail.Body{ {"from": "[email protected]", "to": "[email protected]", "subject": "Order AC-4192 has shipped", "text": "It is on its way."}, {"from": "[email protected]", "to": "[email protected]", "subject": "Order AC-4193 has shipped", "text": "It is on its way."},})if err != nil { return err} fmt.Println(result.Sent, "sent,", result.Failed, "failed") for _, item := range result.Items { if item.String("status") == "error" { fmt.Println(item.Int("index"), item.Object("error").String("message")) }}یک دسته تا 100 پیام جا دارد و هرگز همه یا هیچ نیست: یک نشانی نادرست فقط مورد خودش را ناموفق میکند و بقیه همچنان میروند. بهجای تکیه بر خطا، Failed و status هر مورد را بررسی کنید.