0

Here is th code:

func sendMail() error{

m := gomail.NewMessage()
m.SetHeader("From", From)
m.SetHeader("To", "bacobas.f@gmail.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
m.Attach("./video.mp4")
//m.Attach("./Autorizzazione.pdf")

d := gomail.NewDialer(SmtpHost, SmtpPort, From, PswFrom)

// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
    return err
}
return nil}

If attach "Autorizzazione.pdf" it works. Also if I don't attach anthing. But if i try to attach this .mp4 file the output is Error: gomail: could not send email 1: write tcp <my LAN wi-fi ip address>:54988->142.250.147.109:587: wsasend: An existing connection was forcibly closed by the remote host.

This function is called inside an HandleFunc() in my little http server and the return of this function is basically ""printed"" as a return of that api

Can someone help me please?

  • 1
    How big is the file? – ceejayoz Sep 10 '22 at 20:27
  • maybe the mail server does not allow mp3 files to be sent ... what happens if you change the file name extension? – jsotola Sep 10 '22 at 20:31
  • I would suggest that the remote end (mail server) is closing the connection due to the attachment being too large. Note that mail is not a suitable medium to transfer large files since both sender side mail servers and recipient size mail servers have limits on how large mails can be. With gmail this seems to be 25 MB, which due to encoding overhead means around max 19MB for the attachment. – Steffen Ullrich Sep 10 '22 at 20:37
  • Thank you all! I was hoping that isn't due to the .mp4 file's size. However I don't understand how the above error could refer to an error due to the file's size too large. It talk about some connection closing the host... – Francesco Bassignana Sep 10 '22 at 21:14
  • The remote host might close the connection _because_ the file is too large. – some-user Sep 11 '22 at 12:10

1 Answers1

0

Does the following example work for you?

Source: Sending Email And Attachment With GO (Golang) Using SMTP, Gmail, and OAuth2

package gomail

import (
   "errors"
   "fmt"
   "net/smtp"
)

var emailAuth smtp.Auth

func SendEmailSMTP(to []string, data interface{}, template string) (bool, error) {
   emailHost := "smtp.gmail.com"
   emailFrom := "yourEmail@gmail.com"
   emailPassword := "yourEmailPassword"
   emailPort := 587

   emailAuth = smtp.PlainAuth("", emailFrom, emailPassword, emailHost)

   emailBody, err := parseTemplate(template, data)
   if err != nil {
      return false, errors.New("unable to parse email template")
   }

   mime := "MIME-version: 1.0;\nContent-Type: text/plain; charset=\"UTF-8\";\n\n"
   subject := "Subject: " + "Test Email" + "!\n"
   msg := []byte(subject + mime + "\n" + emailBody)
   addr := fmt.Sprintf("%s:%s", emailHost, emailPort)

   if err := smtp.SendMail(addr, emailAuth, emailFrom, to, msg); err != nil {
      return false, err
   }
   return true, nil
}
suchislife
  • 4,251
  • 10
  • 47
  • 78