إرسال رسالة
رسالة واحدة الآن أو لاحقًا، مع مرفقات، أو مئة في طلب واحد.
إرسال رسالة
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]> أو خريطة فيها 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 errيأخذ content قيمة []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 لكل عنصر بدل الاعتماد على خطأ.