#draft

$Id: 252

$SOId: 5912

Sometimes you need to send an email from your Go program.

It could be an alert your backend server sends to notify you that something bad happen.

It could be a notification email your cron job sends when it's finished.

It could be account registration verification e-mail for your web service. Those are called transactional emails.

It could a marketing email.

When sending emails you have 2 choices:

And we can't talk about sending email without talking about spam. Spam is why it's hard to send legitimate email and even harder to send spam email (a paradox, I know).

Because there is so much spam email, most email services (like gmail) distrust open smtp servers so setting up smtp server that won't be blacklisted is very difficult.

If you send a lot of email via your gmail (or similar account), you can be blocked by email provider.

Similarly, if you send a lot of low quality emails via service like SendGrid, they will block your account too.

All this to say that you should be conservative and stay away from sending large numbers of low quality emails if you don't want to be categorized as a spammer and blocked.

In our examples we'll use https://mailinator.com which is a service useful for debugging email sending. Mailinator provides public, throw-away email addresses. You can send an email to [email protected] and see the email in a browser UI.

Sending via smtp server using net/smtp

Go has net/smtp package for sending emails. You need to know the address of smtp server (e.g. gmail's smtp server is smtp.gmail.com).