-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update email configuration in config.go and add email sender test
- Loading branch information
Showing
6 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package mail | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"html/template" | ||
"io/ioutil" | ||
"mime" | ||
"mime/multipart" | ||
"net/smtp" | ||
"net/textproto" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const ( | ||
smtpAuthAddress = "smtp.office365.com" | ||
smtpServerAddress = "smtp.office365.com:587" | ||
) | ||
|
||
type EmailSender interface { | ||
SendEmail(subject string, templatePath string, to []string, cc []string, bcc []string, attachFiles []string, data map[string]string) error | ||
} | ||
|
||
type GmailSender struct { | ||
name string | ||
fromEmail string | ||
password string | ||
} | ||
|
||
func NewGmailSender(name string, fromEmail string, password string) EmailSender { | ||
return &GmailSender{name, fromEmail, password} | ||
} | ||
|
||
// Function to read and apply a template with data | ||
func loadTemplate(templatePath string, data map[string]string) (string, error) { | ||
tmplContent, err := ioutil.ReadFile(templatePath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
tmpl, err := template.New("email").Parse(string(tmplContent)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var content bytes.Buffer | ||
err = tmpl.Execute(&content, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return content.String(), nil | ||
} | ||
|
||
func (sender *GmailSender) SendEmail(subject string, templatePath string, to []string, cc []string, bcc []string, attachFiles []string, data map[string]string) error { | ||
// Load the email content from the template with data | ||
content, err := loadTemplate(templatePath, data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Build the email body | ||
var msg bytes.Buffer | ||
writer := multipart.NewWriter(&msg) | ||
|
||
// Set up the main headers | ||
msg.WriteString(fmt.Sprintf("From: %s\r\n", sender.fromEmail)) | ||
msg.WriteString(fmt.Sprintf("To: %s\r\n", strings.Join(to, ", "))) | ||
if len(cc) > 0 { | ||
msg.WriteString(fmt.Sprintf("Cc: %s\r\n", strings.Join(cc, ", "))) | ||
} | ||
msg.WriteString(fmt.Sprintf("Subject: %s\r\n", subject)) | ||
msg.WriteString("MIME-Version: 1.0\r\n") | ||
msg.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=%s\r\n", writer.Boundary())) | ||
|
||
// Add the HTML content as part of the multipart message | ||
htmlPart, err := writer.CreatePart(textproto.MIMEHeader{ | ||
"Content-Type": {"text/html; charset=UTF-8"}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
htmlPart.Write([]byte(content)) | ||
|
||
// Attach files | ||
for _, file := range attachFiles { | ||
if err := attachFile(writer, file); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
writer.Close() | ||
|
||
// Send the email | ||
auth := LoginAuth(sender.fromEmail, sender.password) | ||
return smtp.SendMail(smtpServerAddress, auth, sender.fromEmail, append(to, cc...), msg.Bytes()) | ||
} | ||
|
||
// Function to attach a file to the email | ||
func attachFile(writer *multipart.Writer, filePath string) error { | ||
fileData, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, fileName := filepath.Split(filePath) | ||
part, err := writer.CreatePart(textproto.MIMEHeader{ | ||
"Content-Type": {mime.TypeByExtension(filepath.Ext(filePath))}, | ||
"Content-Disposition": {fmt.Sprintf("attachment; filename=\"%s\"", fileName)}, | ||
"Content-Transfer-Encoding": {"base64"}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
part.Write([]byte(base64.StdEncoding.EncodeToString(fileData))) | ||
|
||
return nil | ||
} | ||
|
||
type loginAuth struct { | ||
username, password string | ||
} | ||
|
||
func LoginAuth(username, password string) smtp.Auth { | ||
return &loginAuth{username, password} | ||
} | ||
|
||
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) { | ||
return "LOGIN", []byte{}, nil | ||
} | ||
|
||
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { | ||
if more { | ||
switch string(fromServer) { | ||
case "Username:": | ||
return []byte(a.username), nil | ||
case "Password:": | ||
return []byte(a.password), nil | ||
default: | ||
return nil, errors.New("Unknown fromServer") | ||
} | ||
} | ||
return nil, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mail | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/arya2004/xyfin/utils" | ||
) | ||
|
||
|
||
|
||
func TestSendEmailWithGmail(t *testing.T) { | ||
|
||
config, err := utils.LoadConfig("..") | ||
|
||
if err != nil { | ||
fmt.Println("Failed to load config:", err) | ||
|
||
} | ||
|
||
sender := NewGmailSender("Xphyrus", config.EmailSenderAddress, config.EmailSenderPassword) | ||
to := []string{"[email protected]"} | ||
cc := []string{} | ||
bcc := []string{} | ||
attachFiles := []string{} // Add file paths if you have attachments | ||
|
||
data := map[string]string{ | ||
"Name": "John Doe", | ||
} | ||
|
||
err = sender.SendEmail("Welcome to Our Service!", "./welcome_template.html", to, cc, bcc, attachFiles, data) | ||
if err != nil { | ||
fmt.Println("Failed to send email:", err) | ||
} else { | ||
fmt.Println("Email sent successfully!") | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Welcome</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
background-color: #f4f4f4; | ||
color: #333; | ||
line-height: 1.6; | ||
} | ||
.container { | ||
width: 80%; | ||
margin: 0 auto; | ||
background-color: #fff; | ||
padding: 20px; | ||
border-radius: 8px; | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | ||
} | ||
h1 { | ||
color: #007BFF; | ||
} | ||
p { | ||
margin-bottom: 20px; | ||
} | ||
.footer { | ||
font-size: 0.9em; | ||
color: #777; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1>Welcome to Our Service!</h1> | ||
<p>Dear {{.Name}},</p> | ||
<p>Thank you for joining us. We're excited to have you on board!</p> | ||
<p>If you have any questions, feel free to reach out to our support team at any time.</p> | ||
<p>Best regards,</p> | ||
<p>The Team</p> | ||
<div class="footer"> | ||
<p>If you didn't sign up for this account, please ignore this email or contact support.</p> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains 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