Skip to content

Commit 19b085f

Browse files
committed
feat: Allow plaintext e-mails for SMTP
Adds `--plaintext` flag which allows sending e-mails in plaintext with SMTP method. Also adds counterpart POP_PLAINTEXT. Not implemented for the Resend method.
1 parent cdaa725 commit 19b085f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

email.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (m Model) sendEmailCmd() tea.Cmd {
4242
bcc := strings.Split(m.Bcc.Value(), ToSeparator)
4343
switch m.DeliveryMethod {
4444
case SMTP:
45-
err = sendSMTPEmail(to, cc, bcc, m.From.Value(), m.Subject.Value(), m.Body.Value(), attachments)
45+
err = sendSMTPEmail(to, cc, bcc, m.From.Value(), m.Subject.Value(), m.Body.Value(), plaintext, attachments)
4646
case Resend:
4747
err = sendResendEmail(to, cc, bcc, m.From.Value(), m.Subject.Value(), m.Body.Value(), attachments)
4848
default:
@@ -63,7 +63,7 @@ const gmailSuffix = "@gmail.com"
6363
const gmailSMTPHost = "smtp.gmail.com"
6464
const gmailSMTPPort = 587
6565

66-
func sendSMTPEmail(to, cc, bcc []string, from, subject, body string, attachments []string) error {
66+
func sendSMTPEmail(to, cc, bcc []string, from, subject, body string, plaintext bool, attachments []string) error {
6767
server := mail.NewSMTPClient()
6868

6969
var err error
@@ -115,7 +115,7 @@ func sendSMTPEmail(to, cc, bcc []string, from, subject, body string, attachments
115115
html := bytes.NewBufferString("")
116116
convertErr := goldmark.Convert([]byte(body), html)
117117

118-
if convertErr != nil {
118+
if (plaintext) || (convertErr != nil) {
119119
email.SetBody(mail.TextPlain, body)
120120
} else {
121121
email.SetBody(mail.TextHTML, html.String())

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
// email body.
2121
const PopUnsafeHTML = "POP_UNSAFE_HTML"
2222

23+
// PopPlaintext control whether the message should be sent in plaintext.
24+
// Boolean, default `false`
25+
const PopPlaintext = "POP_PLAINTEXT"
26+
2327
// ResendAPIKey is the environment variable that enables Resend as a delivery
2428
// method and uses it to send the email.
2529
const ResendAPIKey = "RESEND_API_KEY" //nolint:gosec
@@ -56,6 +60,7 @@ var (
5660
bcc []string
5761
subject string
5862
body string
63+
plaintext bool
5964
attachments []string
6065
preview bool
6166
unsafe bool
@@ -119,7 +124,7 @@ var rootCmd = &cobra.Command{
119124
var err error
120125
switch deliveryMethod {
121126
case SMTP:
122-
err = sendSMTPEmail(to, cc, bcc, from, subject, body, attachments)
127+
err = sendSMTPEmail(to, cc, bcc, from, subject, body, plaintext, attachments)
123128
case Resend:
124129
err = sendResendEmail(to, cc, bcc, from, subject, body, attachments)
125130
default:
@@ -201,6 +206,8 @@ func init() {
201206
rootCmd.Flags().StringSliceVarP(&attachments, "attach", "a", []string{}, "Email's attachments")
202207
rootCmd.Flags().StringSliceVarP(&to, "to", "t", []string{}, "Recipients")
203208
rootCmd.Flags().StringVarP(&body, "body", "b", "", "Email's contents")
209+
envPlaintext := os.Getenv(PopPlaintext) == "true"
210+
rootCmd.Flags().BoolVar(&plaintext, "plaintext", envPlaintext, "Whether to send email in plaintext")
204211
envFrom := os.Getenv(PopFrom)
205212
rootCmd.Flags().StringVarP(&from, "from", "f", envFrom, "Email's sender"+commentStyle.Render("($"+PopFrom+")"))
206213
rootCmd.Flags().StringVarP(&subject, "subject", "s", "", "Email's subject")

0 commit comments

Comments
 (0)