Halo!
Ini adalah email test dari sistem billing Care IT.
Jika Anda menerima email ini, berarti sistem email berfungsi dengan baik.
Terima kasih!
package services import ( "errors" "fmt" "net/smtp" "os" "strings" "backendcareit/database" "backendcareit/models" "gorm.io/gorm" ) // SendEmail - Ngirim email pake SMTP bro func SendEmail(to, subject, body string) error { // Ambil konfigurasi dari env variable dulu, lebih aman from := os.Getenv("EMAIL_FROM") password := os.Getenv("EMAIL_PASSWORD") smtpHost := os.Getenv("SMTP_HOST") smtpPort := os.Getenv("SMTP_PORT") // Kalau env variable gak ada, pake default value (biar kompatibel sama versi lama) if from == "" { from = "careit565@gmail.com" } if password == "" { password = "gkhz bjax uamw xydf" } if smtpHost == "" { smtpHost = "smtp.gmail.com" } if smtpPort == "" { smtpPort = "587" } if from == "" || password == "" || smtpHost == "" || smtpPort == "" { return fmt.Errorf("konfigurasi email tidak lengkap. Pastikan EMAIL_FROM, EMAIL_PASSWORD, SMTP_HOST, dan SMTP_PORT sudah di-set") } // Setup authentication auth := smtp.PlainAuth("", from, password, smtpHost) // Format email message msg := []byte(fmt.Sprintf("To: %s\r\n", to) + fmt.Sprintf("Subject: %s\r\n", subject) + "MIME-Version: 1.0\r\n" + "Content-Type: text/html; charset=UTF-8\r\n" + "\r\n" + body + "\r\n") // Send email addr := fmt.Sprintf("%s:%s", smtpHost, smtpPort) err := smtp.SendMail(addr, auth, from, []string{to}, msg) if err != nil { return fmt.Errorf("gagal mengirim email: %w", err) } return nil } // SendEmailToMultiple - Ngirim email ke banyak orang sekaligus func SendEmailToMultiple(to []string, subject, body string) error { from := os.Getenv("EMAIL_FROM") password := os.Getenv("EMAIL_PASSWORD") smtpHost := os.Getenv("SMTP_HOST") smtpPort := os.Getenv("SMTP_PORT") if from == "" { from = "asikmahdi@gmail.com" } if password == "" { password = "njom rhxb prrj tuoj" } if smtpHost == "" { smtpHost = "smtp.gmail.com" } if smtpPort == "" { smtpPort = "587" } if from == "" || password == "" || smtpHost == "" || smtpPort == "" { return fmt.Errorf("konfigurasi email tidak lengkap") } if len(to) == 0 { return fmt.Errorf("daftar penerima email tidak boleh kosong") } // Setup authentication auth := smtp.PlainAuth("", from, password, smtpHost) // Rapihin header To buat semua penerima toHeader := strings.Join(to, ", ") // Format email message msg := []byte(fmt.Sprintf("To: %s\r\n", toHeader) + fmt.Sprintf("Subject: %s\r\n", subject) + "MIME-Version: 1.0\r\n" + "Content-Type: text/html; charset=UTF-8\r\n" + "\r\n" + body + "\r\n") // Kirim email ke semua orang sekaligus addr := fmt.Sprintf("%s:%s", smtpHost, smtpPort) err := smtp.SendMail(addr, auth, from, to, msg) if err != nil { return fmt.Errorf("gagal mengirim email: %w", err) } return nil } // SendEmailTest - Cuma buat test kirim email ke teman-teman func SendEmailTest() error { to := []string{"stylohype685@gmail.com", "pasaribumonica2@gmail.com", "yestondehaan607@gmail.com"} subject := "Test Email - Sistem Billing Care IT" body := `
Halo!
Ini adalah email test dari sistem billing Care IT.
Jika Anda menerima email ini, berarti sistem email berfungsi dengan baik.
Terima kasih!
Yth. Dr. %s,
Berikut adalah informasi billing sign untuk pasien yang Anda tangani:
Terima kasih atas perhatiannya.