[转]emailjs-smtp-client
本文转自:https://github.com/emailjs/emailjs-smtp-client/blob/master/README.md
SMTP Client
SMTP Client allows you to connect to and stream data to a SMTP server in the browser.
StringEncoding API
This module requires TextEncoder
and TextDecoder
to exist as part of the StringEncoding API (see: MDN whatwg.org). Firefox 19+ is basically the only browser that supports this at the time of writing, while Chromium in canary, not stable. Luckily, there is a polyfill!
Depending on your browser, you might need this polyfill for ArrayBuffer #slice, e.g. phantomjs.
TCPSocket API
There is a shim that brings Mozilla-flavored version of the Raw Socket API to other platforms.
If you are on a platform that uses forge instead of a native TLS implementation (e.g. chrome.socket), you have to set the .oncert(pemEncodedCertificate) handler that passes the TLS certificate that the server presents. It can be used on a trust-on-first-use basis for subsequent connection.
If forge is used to handle TLS traffic, you may choose to handle the TLS-related load in a Web Worker. Please use tlsWorkerPath to point to tcp-socket-tls-worker.js
!
Please take a look at the tcp-socket documentation for more information!
Installation
npm:
npm install --save emailjs-smtp-client
Quirks
STARTTLS
is currently not supported- Only
PLAIN
,USER
andXOAUTH2
authentication mechanisms are supported.XOAUTH2
expects a ready to use access token, no tokens are generated automatically.
Usage
AMD
Require emailjs-smtp-client.js as emailjs-smtp-client
Global context
Include files emailjs-smtp-client-response-parser.js and emailjs-smtp-client.js on the page.
<script src="emailjs-smtp-client-response-parser.js"></script> <script src="emailjs-smtp-client.js"></script>
This exposes global variable emailjs-smtp-client
API
Create SmtpClient
object with:
var client = new SmtpClient(host, port, options)
where
- host is the hostname to connect to (defaults to "localhost")
- port is the port to connect to
- options is an optional options object (see below)
Connection options
The following connection options can be used with simplesmtp.connect
:
- useSecureTransport Boolean Set to true, to use encrypted connection
- name String Client hostname for introducing itself to the server
- auth Object Authentication options. Depends on the preferred authentication method
- user is the username for the user (also applies to OAuth2)
- pass is the password for the user if plain auth is used
- xoauth2 is the OAuth2 access token to be used instead of password. If both password and xoauth2 token are set, the token is preferred.
- authMethod String Force specific authentication method (eg.
"PLAIN"
for usingAUTH PLAIN
or"XOAUTH2"
forAUTH XOAUTH2
) - ca (optional) (only in conjunction with this TCPSocket shim) if you use TLS with forge, pin a PEM-encoded certificate as a string. Please refer to the tcp-socket documentation for more information!
- tlsWorkerPath (optional) (only in conjunction with this TCPSocket shim) if you use TLS with forge, this path indicates where the file for the TLS Web Worker is located. Please refer to the tcp-socket documentation for more information!
- disableEscaping Boolean If set to true, do not escape dots on the beginning of the lines
- logLength Number How many messages between the client and the server to log. Set to false to disable logging. Defaults to 6
- ignoreTLS – if set to true, do not issue STARTTLS even if the server supports it
- requireTLS – if set to true, always use STARTTLS before authentication even if the host does not advertise it. If STARTTLS fails, do not try to authenticate the user
- lmtp - if set to true use LMTP commands instead of SMTP commands
Default STARTTLS support is opportunistic – if the server advertises STARTTLS in EHLO response, the client tries to use it. If STARTTLS is not advertised, the clients sends passwords in the plain. You can use ignoreTLS
and requireTLS
to change this behavior by explicitly enabling or disabling STARTTLS usage.
XOAUTH2
To authenticate using XOAUTH2, use the following authentication config
var config = {auth: {user: 'username', xoauth2: 'access_token' } };
See XOAUTH2 docs for more info.
Connection events
Once a connection is set up the following events can be listened to:
- onidle - the connection to the SMTP server has been successfully set up and the client is waiting for an envelope. NB! this event is emitted multiple times - if an e-mail has been sent and the client has nothing to do,
onidle
is emitted again. - onready
(failedRecipients)
- the envelope is passed successfully to the server and a message stream can be started. The argument is an array of e-mail addresses not accepted as recipients by the server. If none of the recipient addresses is accepted,onerror
is emitted instead. - ondone
(success)
- the message was sent - onerror
(err)
- An error occurred. The connection will be closed shortly afterwards, so expect anonclose
event as well - onclose
(isError)
- connection to the client is closed. IfisError
is true, the connection is closed because of an error
Example:
client.onidle = function(){console.log("Connection has been established"); // this event will be called again once a message has been sent // so do not just initiate a new message here, as infinite loops might occur }
Sending an envelope
When an onidle
event is emitted, an envelope object can be sent to the server. This includes a string from
and a single string or an array of strings for to
property.
Envelope can be sent with client.useEnvelope(envelope)
// run only once as 'idle' is emitted again after message delivery var alreadySending = false;client.onidle = function(){ if(alreadySending){ return; } alreadySending = true; client.useEnvelope({ from: "me@example.com", to: ["receiver1@example.com", "receiver2@example.com"] }); }
The to
part of the envelope must include all recipients from To:
, Cc:
and Bcc:
fields.
If envelope setup up fails, an error is emitted. If only some (not all) recipients are not accepted, the mail can still be sent. An onready
event is emitted when the server has accepted the from
and at least one to
address.
client.onready = function(failedRecipients){if(failedRecipients.length){ console.log("The following addresses were rejected: ", failedRecipients); } // start transfering the e-mail }
Sending a message
When onready
event is emitted, it is possible to start sending mail. To do this you can send the message with client.send
calls (you also need to call client.end()
once the message is completed).
send
method returns the state of the downstream buffer - if it returns true
, it is safe to send more data, otherwise you should (but don't have to) wait for the ondrain
event before you send more data.
NB! you do not have to escape the dots in the beginning of the lines by yourself (unless you specificly define so with disableEscaping
option).
client.onready = function(){client.send("Subject: test\r\n"); client.send("\r\n"); client.send("Message body"); client.end(); }
Once the message is delivered an ondone
event is emitted. The event has an parameter which indicates if the message was accepted by the server (true
) or not (false
).
client.ondone = function(success){if(success){console.log("The message was transmitted successfully with "+response);}
}
Logging
At any time you can access the traffic log between the client and the server from the client.log
array.
client.ondone = function(success){// show the last message console.log(client.log.slice(-1)); }
Closing the connection
Once you have done sending messages and do not want to keep the connection open, you can gracefully close the connection with client.quit()
or non-gracefully (if you just want to shut down the connection and do not care for the server) with client.close()
.
If you run quit
or close
in the ondone
event, then the next onidle
is never called.
Get your hands dirty
git clone git@github.com:whiteout-io/smtpclient.git
cd smtpclient
npm install && npm test
To run the integration tests against a local smtp server
grunt smtp
add the test folder as a chrome app (chrome settings -> extensions -> check 'developer mode' -> load unpacked extension)
[转]emailjs-smtp-client相关推荐
- nodejs使用emailjs发送邮箱邮件
以QQ邮箱为例 设置路径:设置->账户->开启POP3/SMTP服务,开启后会获得授权码 上面授权码已经拿到 下面开始来安装emailjs插件 1.安装emailjs插件 cnpm i e ...
- golang 使用ssl连接smtp发送邮件
发送邮件的流程是,先有一个邮件发送器,然后才能发邮件.对于发送器,可以用qq来申请成可以发邮件测试代码 https://jingyan.baidu.com/article/4b07be3cb2f741 ...
- C# 使用163 SMTP发送邮件
读前须知: 1查看文章内容前,先查看发表时间,太老的代码难免有问题. 2.代码仅供参考,不保证完全正确.美观等. 这篇文章主要介绍了C#使用163.com SMTP发送邮件,需要的朋友可以参考下 us ...
- C语言实现SMTP发邮件
文章目录 前言 SMTP SMTP模型 SMTP事务 SMTP命令 命令的返回值 纯手动发送Email 开启邮箱smtp服务 使用telnet连接登录smtp 开始邮件事务 C代码实现SMTP发送邮件 ...
- net core邮件服务器,C#发送电子邮件(SMTP)及outlook.com账号之概要
这是关于c#发送电子邮件(SMTP)的技术笔记,以"简报"形式呈现. 因为最后成功通过outlook.com发送了邮件,所以,我觉得还是有必要 记录一下其中的要点. 一.技术核心 ...
- mutt配置文件_CentOS 下配置以 smtp 方式(msmtp)使用 mutt
mutt 默认使用 sendmail 发邮件,发送到邮箱时容易因为发件人不可信而被拒绝,将其更改为 smtp 方式可以解决这个问题,需要事先准备好 smtp 信息(发送邮件的smtp服务器.邮箱帐号. ...
- 向smtp发送邮件失败
今天,我的程序向smtp发送邮件失败,得到错误 Unable to read data from the transport connection: net_io_connectionclosed. ...
- smtplib python_smtplib —SMTP协议客户端
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引" ...
- nodejs 向手机发送强提醒,定时向手机发送提醒
思路: 准备两个邮箱,利用nodejs使用A邮箱向B邮箱发送邮件,手机系统邮件绑定B邮箱,开启邮件铃声,整个链路就完成了.可以使用crontab开启定时任务. 1. 邮箱准备 邮件通知需要准备两个邮箱 ...
最新文章
- 有哪些可以免登录的视频会议软件/服务?
- 多线程中堆和栈区别的深入解析
- 服务器机房新风系统,某机房新风系统设计方案参考
- leetcode - 474. 一和零
- 无限极评论怎么删除php,TP5 无限极评论回复
- php中提示Undefined index的解决方法
- java里氏替换原则例子_java 设计原则(六)里氏替换原则
- 大数据在零售业的应用
- 控制理论与控制工程算计算机相关专业吗,控制理论与控制工程专业介绍
- C语言——简单三子棋
- Linux内核学习(八):linux内核配置与模块
- 如何通过NPS分析,全方位帮助企业实现客户体验升级?
- mysql驱动加载失败怎么办_为什么mysql加载驱动会报错呢输出的结果是加载驱动失败...
- 水星路由器是linux系统,Mercury水星无线路由器设置教程(Windows XP系统)
- win10 office提示‘VBE6EXT.OLB不能被加载‘怎么办
- 车载syu一android密码,教务系统找回密码
- LeCo-238. 除自身以外数组的乘积
- 情人节撩妹装逼小方法,一学就会
- 初级模拟电路:2-4 限幅器
- 星起航:跨境电商行业卖家可利用新技术打造成熟供应链