山坡网需要能够每周给注册用户发送一封名为“本周最热书籍”的邮件,而之前一直使用的腾讯企业邮箱罢工了,提示说发送请求太多太密集。

一番寻找之后发现了大家口碑不错的搜狐SendCloud服务,看了看文档,价格实惠用起来也方便,于是准备使用它做邮件发送服务器。按照文档的配置一步步走下来发现在发送邮件的时候竟然出错了,错误提示是“unencrypted connection”,奇怪了。

由于用的是smtp包的PLAIN认证方式,所以打开源代码看了看(SublimeText3+GoSublime里ctrl+. ctrl+a输入包名和结构名直接查看源代码,谁用谁喜欢),发现这里要求使用加密连接,否则就会出上述错误。恩,也能理解,毕竟这里明文发送密码了。关键代码如下。

auth := smtp.PlainAuth("", Config.Username, Config.Password, Config.Host)
smtp.SendMail(addr, auth, from, to, []byte(self.String()))

问题明白之后思路也出来了,自己写一个不需要加密链接的PLAIN认证就好了。这里提一下smtp包的设计,看下面这段代码。

// Auth is implemented by an SMTP authentication mechanism.
type Auth interface {
    // Start begins an authentication with a server.
    // It returns the name of the authentication protocol
    // and optionally data to include in the initial AUTH message
    // sent to the server. It can return proto == "" to indicate
    // that the authentication should be skipped.
    // If it returns a non-nil error, the SMTP client aborts
    // the authentication attempt and closes the connection.
    Start(server *ServerInfo) (proto string, toServer []byte, err error)

    // Next continues the authentication. The server has just sent
    // the fromServer data. If more is true, the server expects a
    // response, which Next should return as toServer; otherwise
    // Next should return toServer == nil.
    // If Next returns a non-nil error, the SMTP client aborts
    // the authentication attempt and closes the connection.
    Next(fromServer []byte, more bool) (toServer []byte, err error)
}

 

// SendMail connects to the server at addr, switches to TLS if
// possible, authenticates with the optional mechanism a if possible,
// and then sends an email from address from, to addresses to, with
// message msg.
func SendMail(addr string, a Auth, from string, to []string, msg []byte) error

smtp.SendMail的第二个参数是一个Auth接口,用来实现多种认证方式。标准库中实现了两种认证方式,PLAIN和CRAMMD5Auth,关于这部分知识大家可以自行参考smtp协议中认证部分的定义。这里就不赘述了。

搞清楚了原理就动手吧。直接把标准库中PLAIN的实现拿过来,删除其中需要加密函数的部分,如下红字部分。

type plainAuth struct {
    identity, username, password string
    host                         string
}

func UnEncryptedPlainAuth(identity, username, password, host string) Auth {
    return &plainAuth{identity, username, password, host}
}

func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) {
    if !server.TLS {
        advertised := false
        for _, mechanism := range server.Auth {
            if mechanism == "PLAIN" {
                advertised = true
                break
            }
        }
        if !advertised {
            return "", nil, errors.New("unencrypted connection")
        }
    }
    if server.Name != a.host {
        return "", nil, errors.New("wrong host name")
    }
    resp := []byte(a.identity + "\x00" + a.username + "\x00" + a.password)
    return "PLAIN", resp, nil
}

func (a *plainAuth) Next(fromServer []byte, more bool) ([]byte, error) {
    return nil, nil
}

把发送邮件的代码改成下面这样,再试试看。

auth := UnEncryptedPlainAuth("", Config.Username, Config.Password, Config.Host)
smtp.SendMail(addr, auth, from, to, []byte(self.String()))

恩,还是出错,这次的错误变成“unrecognized command”,看来是SendCloud的服务器并不支持这种验证方式。于是我打开它的文档,发现smtp使用介绍的页面有几种语言的范例代码,看了看Python的代码后发现SendCloud应该用的是Login认证。好吧,之前是犯了经验主义错误了。

再次打开smtp协议的定义,翻到WikiPedia上smtp的(这里标红是因为wiki上的文档也是会过期的)LOGIN认证的文档,上面说,采用LOGIN认证服务器和客户端应该会产生如下对话,下面S代表服务器,C代表客户端。

C:auth login ------------------------------------------------- 进行用户身份认证
S:334 VXNlcm5hbWU6 ----------------------------------- BASE64编码“Username:”
C:Y29zdGFAYW1heGl0Lm5ldA== ----------------------------------- 用户名,使用BASE64编码
S:334 UGFzc3dvcmQ6 -------------------------------------BASE64编码"Password:"
C:MTk4MjIxNA== ----------------------------------------------- 密码,使用BASE64编码
S:235 auth successfully -------------------------------------- 身份认证成功

看起来挺简单,照着写了一个LoginAuth。

type loginAuth struct {
  username, password string
}

func LoginAuth(username, password string) smtp.Auth {
  return &loginAuth{username, password}
}

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
  return "LOGIN", []byte{}, nil
}

func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
  if more {
    switch string(fromServer) {
    case "Username:":
      return []byte(a.username), nil
    case "Password:":
      return []byte(a.password), nil
    }
  }
  return nil, nil
}

把发送邮件的代码改成下面这样。

auth := LoginAuth(Config.Username, Config.Password)
smtp.SendMail(addr, auth, from, to, []byte(self.String()))

运行,还报错,这次错误信息是 Authentication Failed,认证失败。这说明Login认证的方式是对的,但登录失败了。再三确定账号和密码的正确之后我决定用WireShark抓包看看过程。

注意看,AUTH LOGIN之后来了两条334 Password,咦?这里不应该是先来Username接着来Password的吗?为什么是来了两次Password。难道是LOGIN协议改了?

为了确认登陆过程,我用SendCloud文档中Python的代码跑了一遍,终于发现了不同。原来,在发送AUTH LOGIN之后需要带上Username。修改LoginAuth的Start函数。

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
  return "LOGIN", []byte{}, nil
  return "LOGIN", []byte(a.username), nil
}

好了!邮件发送成功!大约花了30分钟,我从一个完全不懂SMTP协议的人完成了LOGIN协议的补充。感叹一下Go的简单,标准库没有黑盒子一样的厚重感,薄的一捅就透,一看就懂。

Go语言实战 - 使用SendCloud群发邮件的更多相关文章

  1. R入门<三>-R语言实战第4章基本数据管理摘要

    入门书籍:R语言实战 进度:1-4章 摘要: 1)实用的包 forecast:用于做时间序列预测的,有auto.arima函数 RODBC:可以用来读取excel文件.但据说R对csv格式适应更加良好 ...

  2. R语言实战(三)基本图形与基本统计分析

    本文对应<R语言实战>第6章:基本图形:第7章:基本统计分析 =============================================================== ...

  3. R语言实战(二)数据管理

    本文对应<R语言实战>第4章:基本数据管理:第5章:高级数据管理 创建新变量 #建议采用transform()函数 mydata <- transform(mydata, sumx ...

  4. R语言实战(一)介绍、数据集与图形初阶

    本文对应<R语言实战>前3章,因为里面大部分内容已经比较熟悉,所以在这里只是起一个索引的作用. 第1章       R语言介绍 获取帮助函数 help(), ? 查看函数帮助 exampl ...

  5. Swift语言实战晋级

    Swift语言实战晋级基本信息作者: 老镇 丛书名: 爱上Swift出版社:人民邮电出版社ISBN:9787115378804上架时间:2014-12-26出版日期:2015 年1月开本:16开页码: ...

  6. swift语言实战晋级-第9章 游戏实战-跑酷熊猫-9-10 移除平台与视差滚动

    9.9 移除场景之外的平台 用为平台是源源不断的产生的,如果不注意销毁,平台就将越积越多,虽然在游戏场景中看不到.几十个还看不出问题,那几万个呢?几百万个呢? 所以我们来看看怎么移除平台,那什么样的平 ...

  7. swift语言实战晋级-第9章 游戏实战-跑酷熊猫-7-8 移动平台的算法

    在上个小节,我们完成了平台的产生.那么我们来实现一下让平台移动.平台的移动,我们只需要在平台工厂类中写好移动的方法,然后在GameScene类中统一控制就行了. 在GameScene类中,有个upda ...

  8. Swift语言实战晋级-第9章 游戏实战-跑酷熊猫-5-6 踩踏平台是怎么炼成的

    在游戏中,有很多分来飞去的平台,这个平台长短不一.如果每种长度都去创建一张图片那是比较繁琐的事情.实际上,我们只用到3张图.分别是平台的,平台的中间部分,平台的右边.关键是平台的中间部分,两张中间部分 ...

  9. Swift语言实战晋级-第9章 游戏实战-跑酷熊猫-4 熊猫的跳和打滚

    之前我们学会了跑的动作,现在我们可以利用同样的方法来实现了跳和打滚的动画. …… class Panda : SKSpriteNode { …… //跳的纹理集合 let jumpAtlas = SK ...

随机推荐

  1. ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core

    背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...

  2. ASP.NET路由模型解析

    大家好,我又来吹牛逼了 ~-_-~ 转载请注明出处:来自吹牛逼之<ASP.NET路由模型解析> 背景:很多人知道Asp.Net中路由怎么用的,却不知道路由模型内部的运行原理,今天我就给大家 ...

  3. Sublime Text3配置在可交互环境下运行python快捷键

    安装插件 在Sublime Text3下面写代码感觉很不错,但是写Python的时候遇到了一些问题. 用Sublime Text3打开python文件,或者在Sublime Text3下写好pytho ...

  4. In-Memory:内存优化表 DMV

    在内存优化表的DMV中,有两个对象ID(Object ID): xtp_object_id 是内部的内存优化表(Internal Memory-Optimized Table)的ID,在对象的整个生命 ...

  5. 异常处理汇总 ~ 修正果带着你的Net飞奔吧!

    经验库开源地址:https://github.com/dunitian/LoTDotNet 异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983 ...

  6. 利用Node.js的Net模块实现一个命令行多人聊天室

    1.net模块基本API 要使用Node.js的net模块实现一个命令行聊天室,就必须先了解NET模块的API使用.NET模块API分为两大类:Server和Socket类.工厂方法. Server类 ...

  7. Javascript实用方法

    这篇我主要记录一些在工作中常用的.实用的方法. String trim 字符串方法中的trim主要用来去空格使用,很多时候,在后台做参数处理的时候,我们都会使用该方法,比如在获取用户输入的账户时 va ...

  8. MSSQL 事务,视图,索引,存储过程,触发器

    事务 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行. 在数据库系统上执行并发操作时事务是作为最小的控制单元来使用的.这特别适用于多用户同时操作的数据 ...

  9. can't run roscore 并且 sudo 指令返回 unable to resolve host

    I'm using ubuntu14 LTS. Problems: 1. When run roscore, got a mistake and an advice to ping the local ...

  10. 【swift】BlockOperation和GCD实用代码块

    //BlockOperation // // ViewController.swift import UIKit class ViewController: UIViewController { @I ...