Sending Email GUI Client- Fyne Golang Coding Tutorial
You need to install the following package:
go get gopkg.in/mail.v2
Let create UI. We need the following widgets
Two labels widgets are required
- App Heading/Title
- Showing Error & Success Message
Four Entry widgets are required
- Sender email
- Receiver email
- Email Subject
- Message/Email body
Button for sending the email.
- create a button widget and label it Send email
Sending Email GUI Client – Part #1
Sending Email GUI Client – Part # 2
Source Code
[sourcecode language=”go” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false” title=”Source Codemain.go”]
package main
import (
"fmt"
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
gomail "gopkg.in/mail.v2"
)
func main() {
a := app.New()
w := a.NewWindow("Email Sender Client")
// lets create UI for all fields
title := canvas.NewText("Email Sender Client..", color.Black)
title.TextSize = 22 // font size is increased
title.Alignment = fyne.TextAlignCenter
message_status := widget.NewLabel("Enter / fill all fields")
sender := widget.NewEntry()
sender.SetPlaceHolder("Enter Sender Email")
receiver := widget.NewEntry()
receiver.SetPlaceHolder("Enter Receiver Email")
subject := widget.NewEntry()
subject.SetPlaceHolder("Enter Subject")
message_body := widget.NewMultiLineEntry()
message_body.SetPlaceHolder("Enter Message…")
// create our first button
send_btn := widget.NewButton("Send Email", func() {
message := gomail.NewMessage()
// message from/ sender email
// remove all static values
message.SetHeader("From", sender.Text)
message.SetHeader("To", receiver.Text)
message.SetHeader("Subject", subject.Text)
message.SetBody("text/plain", message_body.Text)
// put/type smtp client email host(server),port, username,password
// you can use gmail and any other server.
dailer := gomail.NewDialer("smtp-relay.sendinblue.com", 587, "[email protected]", "wvVgD2Ut47TjkiOX")
err := dailer.DialAndSend(message)
if err != nil {
fmt.Println(err)
// message/ error in message status
message_status.Text = err.Error()
message_status.Refresh()
} else {
fmt.Println("Mail Sent successfully…")
// message/ error in message status
message_status.Text = "Mail Sent successfully…"
message_status.Refresh()
}
})
w.Resize(fyne.NewSize(400, 400))
// send all entries in container
w.SetContent(container.NewVBox(
title, // title should be centered
sender,
receiver,
subject,
message_body,
send_btn,
message_status,
))
w.ShowAndRun()
}
[easy_media_download url=”https://blogvali.com/wp-content/uploads/2022/11/Sending-Email-GUI-Client-Fyne-Golang-Coding-Tutorial-75.zip” text=”Download Code” color=”red_darker”]