github.com/decred/politeia@v1.4.0/politeiawww/legacy/cmsemail.go (about)

     1  // Copyright (c) 2018-2020 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package legacy
     6  
     7  import (
     8  	"net/url"
     9  	"strings"
    10  	"text/template"
    11  	"time"
    12  )
    13  
    14  const (
    15  	// GUI routes. These are used in notification emails to direct the
    16  	// user to the correct GUI pages.
    17  	guiRouteDCCDetails = "/dcc/{token}"
    18  )
    19  
    20  // emailUserCMSInvite emails the invitation link for the Contractor Management
    21  // System to the provided user email address.
    22  func (p *Politeiawww) emailUserCMSInvite(email, token string) error {
    23  	link, err := p.createEmailLink(guiRouteRegisterNewUser, "", token, "")
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	tplData := userCMSInvite{
    29  		Email: email,
    30  		Link:  link,
    31  	}
    32  
    33  	subject := "Welcome to the Contractor Management System"
    34  	body, err := createBody(userCMSInviteTmpl, tplData)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	recipients := []string{email}
    39  
    40  	return p.mail.SendTo(subject, body, recipients)
    41  }
    42  
    43  // emailUserDCCApproved emails the link to invite a user that has been approved
    44  // by the other contractors from a DCC proposal.
    45  func (p *Politeiawww) emailUserDCCApproved(email string) error {
    46  	tplData := userDCCApproved{
    47  		Email: email,
    48  	}
    49  
    50  	subject := "Congratulations, You've been approved!"
    51  	body, err := createBody(userDCCApprovedTmpl, tplData)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	recipients := []string{email}
    56  
    57  	return p.mail.SendTo(subject, body, recipients)
    58  }
    59  
    60  // emailDCCSubmitted sends email regarding the DCC New event. Sends email
    61  // to the provided email addresses.
    62  func (p *Politeiawww) emailDCCSubmitted(token string, emails []string) error {
    63  	route := strings.Replace(guiRouteDCCDetails, "{token}", token, 1)
    64  	l, err := url.Parse(p.cfg.WebServerAddress + route)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	tplData := dccSubmitted{
    70  		Link: l.String(),
    71  	}
    72  
    73  	subject := "New DCC Submitted"
    74  	body, err := createBody(dccSubmittedTmpl, tplData)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	return p.mail.SendTo(subject, body, emails)
    80  }
    81  
    82  // emailDCCSupportOppose sends emails regarding dcc support/oppose event.
    83  // Sends emails to the provided email addresses.
    84  func (p *Politeiawww) emailDCCSupportOppose(token string, emails []string) error {
    85  	route := strings.Replace(guiRouteDCCDetails, "{token}", token, 1)
    86  	l, err := url.Parse(p.cfg.WebServerAddress + route)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	tplData := dccSupportOppose{
    92  		Link: l.String(),
    93  	}
    94  
    95  	subject := "New DCC Support/Opposition Submitted"
    96  	body, err := createBody(dccSupportOpposeTmpl, tplData)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	return p.mail.SendTo(subject, body, emails)
   102  }
   103  
   104  // emailInvoiceStatusUpdate sends email for the invoice status update event.
   105  // Send email for the provided user email address.
   106  func (p *Politeiawww) emailInvoiceStatusUpdate(invoiceToken, userEmail string) error {
   107  	tplData := invoiceStatusUpdate{
   108  		Token: invoiceToken,
   109  	}
   110  
   111  	subject := "Invoice status has been updated"
   112  	body, err := createBody(invoiceStatusUpdateTmpl, tplData)
   113  	if err != nil {
   114  		return err
   115  	}
   116  	recipients := []string{userEmail}
   117  
   118  	return p.mail.SendTo(subject, body, recipients)
   119  }
   120  
   121  // emailInvoiceNotifications emails users that have not yet submitted an
   122  // invoice for the given month/year
   123  func (p *Politeiawww) emailInvoiceNotifications(email, username, subject string, tmpl *template.Template) error {
   124  	// Set the date to the first day of the previous month.
   125  	newDate := time.Date(time.Now().Year(), time.Now().Month()-1, 1, 0, 0, 0, 0, time.UTC)
   126  	tplData := invoiceNotification{
   127  		Username: username,
   128  		Month:    newDate.Month().String(),
   129  		Year:     newDate.Year(),
   130  	}
   131  	body, err := createBody(tmpl, &tplData)
   132  	if err != nil {
   133  		return err
   134  	}
   135  	recipients := []string{email}
   136  
   137  	return p.mail.SendTo(subject, body, recipients)
   138  }
   139  
   140  // emailInvoiceNewComment sends email for the invoice new comment event. Send
   141  // email to the provided user email address.
   142  func (p *Politeiawww) emailInvoiceNewComment(userEmail string) error {
   143  	var tplData interface{}
   144  	subject := "New Invoice Comment"
   145  
   146  	body, err := createBody(invoiceNewCommentTmpl, tplData)
   147  	if err != nil {
   148  		return err
   149  	}
   150  	recipients := []string{userEmail}
   151  
   152  	return p.mail.SendTo(subject, body, recipients)
   153  }
   154  
   155  // User CMS invite - Send to user being invited
   156  type userCMSInvite struct {
   157  	Email string // User email
   158  	Link  string // Registration link
   159  }
   160  
   161  const userCMSInviteText = `
   162  You are invited to join Decred as a contractor! To complete your registration, you will need to use the following link and register on the CMS site:
   163  
   164  {{.Link}}
   165  
   166  You are receiving this email because {{.Email}} was used to be invited to Decred's Contractor Management System.
   167  If you do not recognize this, please ignore this email.
   168  `
   169  
   170  var userCMSInviteTmpl = template.Must(
   171  	template.New("userCMSInvite").Parse(userCMSInviteText))
   172  
   173  // User DCC approved - Send to approved user
   174  type userDCCApproved struct {
   175  	Email string // User email
   176  }
   177  
   178  const userDCCApprovedText = `
   179  Congratulations! Your Decred Contractor Clearance Proposal has been approved! 
   180  
   181  You are now a fully registered contractor and may now submit invoices.  You should also be receiving an invitation to the contractors room on matrix.  
   182  If you have any questions please feel free to ask them there.
   183  
   184  You are receiving this email because {{.Email}} was used to be invited to Decred's Contractor Management System.
   185  If you do not recognize this, please ignore this email.
   186  `
   187  
   188  var userDCCApprovedTmpl = template.Must(
   189  	template.New("userDCCApproved").Parse(userDCCApprovedText))
   190  
   191  // DCC submitted - Send to admins
   192  type dccSubmitted struct {
   193  	Link string // DCC gui link
   194  }
   195  
   196  const dccSubmittedText = `
   197  A new DCC has been submitted.
   198  
   199  {{.Link}}
   200  
   201  Regards,
   202  Contractor Management System
   203  `
   204  
   205  var dccSubmittedTmpl = template.Must(
   206  	template.New("dccSubmitted").Parse(dccSubmittedText))
   207  
   208  // DCC support/oppose - Send to admins
   209  type dccSupportOppose struct {
   210  	Link string // DCC gui link
   211  }
   212  
   213  const dccSupportOpposeText = `
   214  A DCC has received new support or opposition.
   215  
   216  {{.Link}}
   217  
   218  Regards,
   219  Contractor Management System
   220  `
   221  
   222  var dccSupportOpposeTmpl = template.Must(
   223  	template.New("dccSupportOppose").Parse(dccSupportOpposeText))
   224  
   225  // Invoice status update - Send to invoice owner
   226  type invoiceStatusUpdate struct {
   227  	Token string // Invoice token
   228  }
   229  
   230  const invoiceStatusUpdateText = `
   231  An invoice's status has been updated, please login to cms.decred.org to review the changes.
   232  
   233  Updated Invoice Token: {{.Token}}
   234  
   235  Regards,
   236  Contractor Management System
   237  `
   238  
   239  var invoiceStatusUpdateTmpl = template.Must(
   240  	template.New("invoiceStatusUpdate").Parse(invoiceStatusUpdateText))
   241  
   242  // Invoice notifications
   243  var invoiceFirstNotificationTmpl = template.Must(
   244  	template.New("first_invoice_notification").Parse(invoiceFirstText))
   245  var invoiceSecondNotificationTmpl = template.Must(
   246  	template.New("second_invoice_notification").Parse(invoiceSecondText))
   247  var invoiceFinalNotificationTmpl = template.Must(
   248  	template.New("final_invoice_notification").Parse(invoiceFinalText))
   249  
   250  type invoiceNotification struct {
   251  	Username string
   252  	Month    string
   253  	Year     int
   254  }
   255  
   256  const invoiceFirstText = `
   257  {{.Username}},
   258  
   259  Please submit your invoice for {{.Month}} {{.Year}}.
   260  
   261  Regards,
   262  Contractor Management System
   263  `
   264  
   265  const invoiceSecondText = `
   266  {{.Username}},
   267  
   268  You have not yet submitted an invoice for {{.Month}} {{.Year}}.
   269  
   270  Regards,
   271  Contractor Management System`
   272  
   273  const invoiceFinalText = `
   274  {{.Username}},
   275  
   276  You have not yet submitted an invoice for {{.Month}} {{.Year}}.  This is the final warning you will receive, if you delay further, you may not be included in this month's payout.
   277  
   278  Regards,
   279  Contractor Management System
   280  `
   281  
   282  // Invoice new comment - Send to invoice owner
   283  const invoiceNewCommentText = `
   284  An administrator has submitted a new comment to your invoice, please login to cms.decred.org to view the message.
   285  `
   286  
   287  var invoiceNewCommentTmpl = template.Must(
   288  	template.New("invoiceNewComment").Parse(invoiceNewCommentText))