Skip to content

Instantly share code, notes, and snippets.

@mimosa
Last active November 28, 2015 11:45
Show Gist options
  • Save mimosa/a91364c780069a8e643a to your computer and use it in GitHub Desktop.
Save mimosa/a91364c780069a8e643a to your computer and use it in GitHub Desktop.
YunPian + SendCloud
  • 模板创建:
  • 测试代码:
# 测试数据
def example
  {
    "mimosa@aliyun.com" => {
      user_id: 1,
      username: 'Mimosa',
      bio: '一个名符其实的二货。'
    },
    "howl.wong@outlook.com" => {
      user_id: 2,
      username: 'Howl',
      bio: nil
    },
    "howl.wong@gmail.com" => {
      user_id: 3,
      username: 'Howl王',
      bio: '别看我外表粗狂,其实怀揣着一颗少女的❤️。'
    }
  }
end

# 动态内容测试
def test(from='女神·经<post@alert.g-secret.com>')
  @mail_sender.send(
    example, 
    '下发给%username%的系统邮件',
    '<p>亲爱的%username%:</p><p>您好!这是一封系统测试邮件;</p><p>测试动态参数:%bio%,对了吗?</p><p>GSecret(女神·经)</p>',
    from
  )
end

# 静态邮件测试
def test1(from='女神·经<post@alert.g-secret.com>')
  @mail_sender.send(
    example.keys, 
    '下发给小恶心的系统邮件',
    '<p>亲爱的小恶魔:</p><p>您好!这是一封系统测试邮件;</p><p>测试动态参数:雪菜肉丝面,对了吗?</p><p>GSecret(女神·经)</p>',
    from
  )
end

# 直接调用模板名称进行测试
def test2(from='女神·经<post@alert.g-secret.com>')
  @mail_sender.send_template(
    example, 
    'test',
    from
  )
end
  • 调用:
# 邮件
def mail
  @mail_sender ||= SendCloud.new(
    'user', 
    'key', 
    'https://api.sendcloud.net/apiv2'
  )
end

# 短信
def sms
  @sms_sender ||= YunPian.new(
    'key',
    'https://yunpian.com/v1'
  )
end

sms.send(18016245161, '您的验证码是ABCD', '女神·经')

mail()

test()

test1()

test2()
# -*- encoding: utf-8 -*-
class Mailgun < RestCli
def initialize(key, endpoint)
connection.url_prefix = endpoint
connection.basic_auth('api', key)
end
def send(to, subject, body, from)
params = {
from: from,
subject: subject,
text: body,
html: body,
to: to
}
send_mail(params)
end
def set_domain(domain)
@domain = domain
end
alias_method :domain=, :set_domain
private
def send_mail(params)
return false if @domain.nil?
post("#{@domain}/messages", params)
end
end
# -*- encoding: utf-8 -*-
class RestCli
def initialize(endpoint)
connection.url_prefix = endpoint
end
def get(path, params={})
parser connection.get(path, params)
end
def post(path, params={})
parser connection.post(path, params)
end
private
def connection
@connection ||= Faraday.new( parallel_manager: Typhoeus::Hydra.new( max_concurrency: 100 ), ssl: { verify: false } ) do |req|
req.adapter :typhoeus
end
end
def parser(resp)
MultiJson.load(resp.body) rescue nil if resp.status.between?(200,201)
end
end
# -*- encoding: utf-8 -*-
class SendCloud < RestCli
def initialize(key, secret, endpoint)
@params = {
apiUser: key,
apiKey: secret
}
connection.url_prefix = endpoint
end
def send_template(mails, templ_name, from)
params = {
from: from,
templateInvokeName: templ_name,
xsmtpapi: gen_xsmtpapi(mails),
useNotification: true
}
resp = send_mail(params, 'mail/sendtemplate')
resp && resp['result']
end
def send(mails, subject, body, from)
params = {
from: from,
subject: subject,
html: body,
xsmtpapi: gen_xsmtpapi(mails),
useNotification: true
}
resp = send_mail(params)
resp && resp['result']
end
private
def gen_xsmtpapi(mails)
xsmtpapi = {
to: [],
sub: {}
}
mails.each do |to, attrs|
xsmtpapi[:to] << to
unless attrs.nil?
attrs.each do |key, val|
key = "%#{key}%"
xsmtpapi[:sub][key] = [] unless xsmtpapi[:sub].has_key?(key)
xsmtpapi[:sub][key] << (val || '')
end
end
end
xsmtpapi.to_json
end
def send_mail(params, path='mail/send')
puts @params.merge(params)
post(
path,
@params.merge(params)
)
end
end
# -*- encoding: utf-8 -*-
class YunPian < RestCli
def initialize(key, endpoint)
@params = {
apikey: key
}
connection.url_prefix = endpoint
end
def send(mobile, content, sign)
message = "#{content}【#{sign}】"
resp = send_sms(mobile, message)
return false if resp.nil?
resp['msg'] == 'OK'
end
private
def send_sms(mobile, message)
post('sms/send.json', @params.merge(
mobile: mobile,
text: message
))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment