Class: Submail::MailSend

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/submail/mail_send.rb

Instance Method Summary collapse

Methods included from Helper

#create_signatrue, #get_timestamp, #http_get, #http_post, #http_request

Constructor Details

#initialize(config) ⇒ MailSend

Returns a new instance of MailSend.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/submail/mail_send.rb', line 5

def initialize(config)
  @to = []
  @addressbook = []
  @from = ""
  @fromname = ""
  @reply = ""
  @cc = []
  @bcc = []
  @subject = ""
  @text = ""
  @html = ""
  @vars ={}
  @links = {}
  @headers = {}
  @config = config
end

Instance Method Details

#add_addressbook(addressbook) ⇒ Object



29
30
31
32
# File 'lib/submail/mail_send.rb', line 29

def add_addressbook(addressbook)
  @addressbook = []
  @addressbook << addressbook
end

#add_bcc(address, name) ⇒ Object



50
51
52
53
54
55
# File 'lib/submail/mail_send.rb', line 50

def add_bcc(address, name)
  bcc = {}
  bcc["address"] = address
  bcc["name"] = name
  @bcc << bcc
end

#add_cc(address, name) ⇒ Object



43
44
45
46
47
48
# File 'lib/submail/mail_send.rb', line 43

def add_cc(address, name)
  cc = {}
  cc["address"] = address
  cc["name"] = name
  @cc << cc
end

#add_header(key, value) ⇒ Object



77
78
79
# File 'lib/submail/mail_send.rb', line 77

def add_header(key, value)
  @headers[key] = value
end


73
74
75
# File 'lib/submail/mail_send.rb', line 73

def add_link(key, value)
  @links[key] = value
end

#add_to(address, name) ⇒ Object



22
23
24
25
26
27
# File 'lib/submail/mail_send.rb', line 22

def add_to(address, name)
  to = {}
  to["address"] = address
  to["name"] = name
  @to << to
end

#add_var(key, value) ⇒ Object



69
70
71
# File 'lib/submail/mail_send.rb', line 69

def add_var(key, value)
  @vars[key] = value
end

#build_requestObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/submail/mail_send.rb', line 81

def build_request
  request = {}
  if @to.length != 0
    to = []
    @to.each do |k| 
      to << "%s<%s>" %[k["name"], k["address"]]
    end
    request["to"] = to.join(",")
  end
  if @addressbook.length != 0
    request["addressbook"] = @addressbook.join(",")
  end
  if @from != ""
    request["from"] = @from
  end
  if @fromname != ""
    request["from_name"] = @fromname
  end
  if @reply != ""
    request["reply"] = @reply
  end
  if @cc.length != 0
    cc = []
    @cc.each do |k| 
      cc << "%s<%s>" %[k["name"], k["address"]]
    end
    request["cc"] = cc.join(",")
  end
  if @bcc.length != 0
    bcc = []
    @bcc.each do |k| 
      bcc << "%s<%s>" %[k["name"], k["address"]]
    end
    request["bcc"] = bcc.join(",")
  end
  if @subject != ""
    request["subject"] = @subject
  end
  if @text != ""
    request["text"] = @text
  end
  if @html != ""
    request["html"] = @html
  end
  if @vars.length != 0
    request["vars"] = JSON.generate @vars
  end
  if @links.length != 0
    request["links"] = JSON.generate @links
  end
  
  if @headers.length != 0
    request["headers"] = JSON.generate @headers
  end
  request
end

#mail_sendObject



138
139
140
141
142
143
144
145
# File 'lib/submail/mail_send.rb', line 138

def mail_send
  request = self.build_request()
  url = "https://api.submail.cn/mail/send.json"
  request["appid"] = @config["appid"]
  request["timestamp"] = get_timestamp()
  request["signature"] = create_signatrue(request, @config)
  JSON.parse http_post(url, request)
end

#set_html(html) ⇒ Object



65
66
67
# File 'lib/submail/mail_send.rb', line 65

def set_html(html)
  @html = html
end

#set_reply(reply) ⇒ Object



39
40
41
# File 'lib/submail/mail_send.rb', line 39

def set_reply(reply)
  @reply = reply
end

#set_sender(from, fromname) ⇒ Object



34
35
36
37
# File 'lib/submail/mail_send.rb', line 34

def set_sender(from, fromname)
  @from = from
  @fromname = fromname
end

#set_subject(subject) ⇒ Object



57
58
59
# File 'lib/submail/mail_send.rb', line 57

def set_subject(subject)
  @subject = subject
end

#set_text(text) ⇒ Object



61
62
63
# File 'lib/submail/mail_send.rb', line 61

def set_text(text)
  @text = text
end