...

Source file src/gitlab.hexacode.org/go-libs/mailer/mailer.go

Documentation: gitlab.hexacode.org/go-libs/mailer

     1  /*
     2   * Hexacode Mailer
     3   * Package: gitlab.hexacode.org/go-libs/mailer
     4   * Maintainer: Azzis Arswendo <azzis@hexacode.org>
     5   *
     6   * Copyright (C) 2023 Hexacode Teknologi Indonesia
     7   * All Rights Reserved
     8   */
     9  
    10  // Example:
    11  //
    12  // ```go
    13  //
    14  // package main
    15  //
    16  // import (
    17  //
    18  //	"gitlab.hexacode.org/go-libs/hctypes"
    19  //	"gitlab.hexacode.org/go-libs/mailer"
    20  //
    21  // )
    22  //
    23  //	func main() {
    24  //	  // Create a new template
    25  //	  // Please see https://github.com/noirbizarre/gonja for more details template
    26  //	  tmpl, err := mailer.NewTemplate("Hello {{ name }}!")
    27  //	  if err != nil {
    28  //	    panic(err)
    29  //	  }
    30  //
    31  //	  // Create a new sender
    32  //	  sender := mailer.NewSender("Example Sender", "example-sender@gmail.com", "password", "smtp.gmail.com", 587, tmpl)
    33  //
    34  //	  // Send an email
    35  //	  err = sender.SendEmail("example-recipient@gmail.com", "Example Subject", hctypes.Dict{"name": "John Doe"})
    36  //	  if err != nil {
    37  //	    panic(err)
    38  //	  }
    39  //	}
    40  //
    41  // ```
    42  package mailer
    43  
    44  import (
    45  	"fmt"
    46  
    47  	"github.com/go-mail/mail"
    48  	"gitlab.hexacode.org/go-libs/hctypes"
    49  )
    50  
    51  // Sender represents a mail sender
    52  type Sender struct {
    53  	sender_name    string    // The name of the sender
    54  	sender_address string    // The email address of the sender
    55  	password       string    // The password of the sender
    56  	smtp_host      string    // The smtp host (eg: smtp.gmail.com)
    57  	smtp_port      int       // The smtp port (eg: 587)
    58  	Template       *Template // The template *Template
    59  }
    60  
    61  // NewSender creates a new Sender
    62  //
    63  // sender_name: The name of the sender
    64  // sender_address: The email address of the sender
    65  // password: The password of the sender
    66  // smtp_host: The smtp host (eg: smtp.gmail.com)
    67  // smtp_port: The smtp port (eg: 587)
    68  // template: The template *Template
    69  // Returns: *Sender
    70  func NewSender(sender_name, sender_address, password, smtp_host string, smtp_port int, template *Template) *Sender {
    71  	return &Sender{
    72  		sender_name:    sender_name,
    73  		sender_address: sender_address,
    74  		password:       password,
    75  		smtp_host:      smtp_host,
    76  		smtp_port:      smtp_port,
    77  		Template:       template,
    78  	}
    79  }
    80  
    81  // SendEmail sends an email
    82  //
    83  // to: The email address of the recipient
    84  // subject: The subject of the email
    85  // query: The query of the email template
    86  // Returns: error
    87  func (s *Sender) SendEmail(to, subject string, query hctypes.Dict) error {
    88  	from := fmt.Sprintf("%s <%s>", s.sender_name, s.sender_address)
    89  
    90  	msg := mail.NewMessage()
    91  	msg.SetHeader("From", from)
    92  	msg.SetHeader("To", to)
    93  	msg.SetHeader("Subject", subject)
    94  	body, err := s.Template.Render(query)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	msg.SetBody("text/html", body)
   100  
   101  	dial := mail.NewDialer(s.smtp_host, s.smtp_port, s.sender_address, s.password)
   102  	return dial.DialAndSend(msg)
   103  }
   104  

View as plain text