Class: Easyemail

Inherits:
Object
  • Object
show all
Defined in:
lib/easyemail.rb,
lib/easyemail/version.rb

Defined Under Namespace

Classes: Mailer

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#email(subject, content) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/easyemail.rb', line 60

def email subject, content
  # content支持html

  if @config && @to
    if @to.respond_to? :each
      @to.each do | e |
        Mailer.send_email(@from, e, subject, content).deliver
      end
    else
      Mailer.send_email(@from, @to, subject, content).deliver
    end
  else
    raise "check smtp_settings and receiver!"
  end
end

#load_smtp_settings_from_yaml(path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/easyemail.rb', line 26

def load_smtp_settings_from_yaml path
  smtp = YAML.load_file(path)
  if smtp["provider"]
    # 如果指定了邮件服务商 就直接跳转到该服务商
    # 待选列表 163, hhu, qq, gmail
    self.send("smtp_settings_for_#{smtp["provider"]}", smtp["user_name"], smtp["password"])
  else
    self.smtp_settings smtp
  end
end

#smtp_settings(smtp) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/easyemail.rb', line 37

def smtp_settings smtp
  # common settings for smtp, all provided by user.
  begin
    ActionMailer::Base.smtp_settings = {
      address: smtp['address'],
      port: smtp["port"],
      authentication: smtp["authentication"],
      user_name: smtp['user_name'],
      password: smtp['password'],
      enable_starttls_auto: smtp["enable_starttls_auto"]
    }
    @from = smtp["user_name"]
    @config = true
  rescue
    raise "wrong parameters for smtp settings."
  end
end

#smtp_settings_for_163(user_name, password) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/easyemail.rb', line 76

def smtp_settings_for_163 user_name, password
  smtp = {
    "address" => "smtp.163.com",
    "port" => 25,
    "authentication" => "login",
    "user_name" => user_name,
    "password" => password,
    "enable_starttls_auto" => true
  }
  self.smtp_settings smtp
end

#smtp_settings_for_gmail(user_name, password) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/easyemail.rb', line 112

def smtp_settings_for_gmail user_name, password
  smtp = {
    "address" => "smtp.gmail.com",
    "port" => 587,
    "authentication" => "login",
    "user_name" => user_name,
    "password" => password,
    "enable_starttls_auto" => true
  }
  self.smtp_settings smtp
end

#smtp_settings_for_hhu(user_name, password) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/easyemail.rb', line 88

def smtp_settings_for_hhu user_name, password
  smtp = {
    "address" => "mail.hhu.edu.cn",
    "port" => 25,
    "authentication" => "login",
    "user_name" => user_name,
    "password" => password,
    "enable_starttls_auto" => false
  }
  self.smtp_settings smtp
end

#smtp_settings_for_qq(user_name, password) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/easyemail.rb', line 100

def smtp_settings_for_qq user_name, password
  smtp = {
    "address" => "smtp.qq.com",
    "port" => 25,
    "authentication" => "login",
    "user_name" => user_name,
    "password" => password,
    "enable_starttls_auto" => false
  }
  self.smtp_settings smtp
end

#to=(to) ⇒ Object



55
56
57
58
# File 'lib/easyemail.rb', line 55

def to= to
  # 支持群发
  @to = to
end