Created
July 25, 2023 07:47
-
-
Save 19ajikamaludin/9e905eaaed043aa7456abd5fb64a4ae7 to your computer and use it in GitHub Desktop.
send email with attachment in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package smtpserver | |
| import ( | |
| "bytes" | |
| "crypto/tls" | |
| "encoding/base64" | |
| "fmt" | |
| "mime/multipart" | |
| "net/smtp" | |
| ) | |
| func SendEmail(config *configs.Configs, to, subject, body string, attachments map[string][]byte) error { | |
| from := config.Env.GetKey("smtp.mailfromaddress") | |
| // Setup headers | |
| headers := make(map[string]string) | |
| headers["From"] = from | |
| headers["To"] = to | |
| headers["Subject"] = subject | |
| // Setup message | |
| var message bytes.Buffer | |
| for key, value := range headers { | |
| message.WriteString(fmt.Sprintf("%s: %s\n", key, value)) | |
| } | |
| // Create a multipart writer for the email body | |
| multipartWriter := multipart.NewWriter(&message) | |
| message.WriteString("MIME-Version: 1.0\r\n") | |
| message.WriteString("Content-Type: multipart/mixed; boundary=" + multipartWriter.Boundary() + "\r\n\r\n") | |
| // Add the text body part | |
| message.WriteString("--" + multipartWriter.Boundary() + "\r\n") | |
| message.WriteString("Content-Type: text/plain; charset=utf-8\r\n") | |
| message.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n") | |
| message.WriteString(body + "\r\n") | |
| // Attach each file to the email | |
| for name, attachment := range attachments { | |
| message.WriteString("--" + multipartWriter.Boundary() + "\r\n") | |
| message.WriteString("Content-Type: application/octet-stream\r\n") | |
| message.WriteString("Content-Transfer-Encoding: base64\r\n") | |
| message.WriteString("Content-Disposition: attachment; filename=\"" + name + "\"\r\n\r\n") | |
| // Convert data to base64 and write to the message buffer | |
| encoder := base64.NewEncoder(base64.StdEncoding, &message) | |
| encoder.Write(attachment) | |
| encoder.Close() | |
| message.WriteString("\r\n") | |
| } | |
| multipartWriter.Close() | |
| host := config.Env.GetKey("smtp.host") | |
| servername := fmt.Sprintf("%s:%s", host, config.Env.GetKey("smtp.port")) | |
| // Connect to the SMTP Server | |
| auth := smtp.PlainAuth("", config.Env.GetKey("smtp.username"), config.Env.GetKey("smtp.password"), host) | |
| // TLS config | |
| tlsconfig := &tls.Config{ | |
| InsecureSkipVerify: true, | |
| ServerName: host, | |
| } | |
| // Here is the key, you need to call tls.Dial instead of smtp.Dial | |
| // for smtp servers running on 465 that require an ssl connection | |
| // from the very beginning (no starttls) | |
| c, err := smtp.Dial(servername) | |
| if err != nil { | |
| return err | |
| } | |
| if err = c.StartTLS(tlsconfig); err != nil { | |
| return err | |
| } | |
| // Auth | |
| if err = c.Auth(auth); err != nil { | |
| return err | |
| } | |
| err = smtp.SendMail(servername, auth, from, []string{to}, message.Bytes()) | |
| if err != nil { | |
| return err | |
| } | |
| return nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment