The Bolg

Back to coding blogs 🧏🏾‍♂️

so over this weekend, i did two things

1) setup smtp mail server on my VPS

2) test out sending email using that same mail server using google’s smtp code!

This was super cool process for me, but I have no fucking clue what I was doing when setting up the email server.

I followed these two guides primarily:

main postfix + dovecat setup: https://www.linode.com/docs/guides/email-with-postfix-dovecot-and-mysql/

then i got some SPF and DKIM error in syslog, so I followed another guide to add those from this reputable sounding website: https://www.linuxbabe.com/mail-server/setting-up-dkim-and-spf

and voila, i was able to send emails from inside my VPS using this command

echo "Email body text" | sudo mail -s "Email subject line" test-v3duf8709@srv1.mail-tester.com -aFrom:mailman@mail.shaikzhafir.com

Next, I wanted to programtically send the email via my go web application, and in my goal to use as little dependencies as possible, I will try to use the inbuilt go smtp server library to send the email. its as simple as

	auth := smtp.PlainAuth("", "mailman@mail.shaikzhafir.com", "<password>", "mail.shaikzhafir.com")

	if ok, _ := c.Extension("STARTTLS"); ok {
		tlsConfig := &tls.Config{
			ServerName: "mail.shaikzhafir.com",
		}
		if err := c.StartTLS(tlsConfig); err != nil {
			log.Fatal(err)
		}
	}

	msg := []byte("To: shaikzhafir@gmail.com\r\n" +
		"Subject: Drake vs kendrick secret information\r\n" +
		"\r\n" +
		"nah bro its nothing\r\n")

	err = smtp.SendMail("mail.shaikzhafir.com:25", auth, "mailman@mail.shaikzhafir.com", []string{"shaikzhafir@gmail.com"}, msg)
	if err != nil {
		log.Fatalf("error sending mail %+v", err)
	}

Success!

loading...

Now i will make it such that whoever logs into my web application, will be sent an email, capped at 1 email max per day per account to prevent spam.

Next up is the fun part, the actual web application logic! will start by defining the database tables.