Class: Makit::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/email.rb

Overview

Provides email functionality for sending notifications and reports.

This class handles SMTP configuration and email delivery, with built-in support for Gmail SMTP and graceful handling when the mail gem is not available.

Examples:

Basic usage

email = Makit::Email.new
email.configure_gmail_defaults
email.send(subject: "Build Complete", body: "Your build finished successfully")

Instance Method Summary collapse

Instance Method Details

#configure_gmail_defaultsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/makit/email.rb', line 22

def configure_gmail_defaults
  unless MAIL_AVAILABLE
    raise LoadError, "Mail gem is not available. Add 'mail' to your Gemfile to use email functionality."
  end

  Mail.defaults do
    delivery_method :smtp, {
      address: "smtp.gmail.com",
      port: 587,
      domain: "gmail.com",
      user_name: ENV.fetch("SMTP_USERNAME", nil),
      password: ENV.fetch("SMTP_PASSWORD", nil),
      authentication: "plain",
      enable_starttls_auto: true,
    }
  end
end

#send(subject:, body:) ⇒ Object



63
64
65
# File 'lib/makit/email.rb', line 63

def send(subject:, body:)
  send_to(to: ENV.fetch("MAKIT_EMAIL_RECIPIENT", nil), subject: subject, body: body)
end

#send_test_email(subject:, body:, to: ENV.fetch("MAKIT_EMAIL_RECIPIENT", nil)) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/makit/email.rb', line 40

def send_test_email(subject:, body:, to: ENV.fetch("MAKIT_EMAIL_RECIPIENT", nil))
  unless MAIL_AVAILABLE
    raise LoadError, "Mail gem is not available. Add 'mail' to your Gemfile to use email functionality."
  end

  raise ArgumentError, "Recipient email not set. Please set MAKIT_EMAIL_RECIPIENT environment variable" unless to

  mail = Mail.new do
    from ENV.fetch("SMTP_USERNAME", nil)
    to to
    subject subject
    body body
  end

  begin
    mail.deliver!
    true
  rescue StandardError => e
    puts "Failed to send email: #{e.message}"
    false
  end
end

#send_to(to:, subject:, body:) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/makit/email.rb', line 67

def send_to(to:, subject:, body:)
  unless MAIL_AVAILABLE
    raise LoadError, "Mail gem is not available. Add 'mail' to your Gemfile to use email functionality."
  end

  raise ArgumentError, "Recipient email not set" unless to

  mail = Mail.new do
    from ENV.fetch("SMTP_USERNAME", nil)
    to to
    subject subject
    body body
  end

  begin
    mail.deliver!
    true
  rescue StandardError => e
    puts "Failed to send email: #{e.message}"
    false
  end
end