收件人验证。电子邮件验证码示例

收件人验证。电子邮件验证码示例

收件人验证: 电子邮件 Verification Code Examples

Jul 8, 2020

出版商

出版商

Bird

Bird

-

类别

类别

电子邮件

Email

Ready to see Bird
in action?

Ready to see Bird
in action?

收件人验证。电子邮件验证码示例

SparkPost Recipient Validation is now available both for existing SparkPost customers and for new, non-sending customers. It uses powerful data-driven analysis on billions of bounce, delivery, and engagement events daily to train our algorithm, bringing you one of the most powerful data-driven email validation tools on the market, so you can send emails smarter.

这篇文章 explains how you can get the most out of the data you’ll receive back on each validated recipient – you’ll see we classify addresses to be “valid”, “risky”, “neutral”, “undeliverable”, and “typo”. We give you a “reason” code and also a “did_you_mean” for known address typos.


API请求

In the SparkPost web app, you can drag & drop an entire list for validation. You can also use API to validate single addresses, so you can build validation right into your address entry workflow.

A while back we came up with a Python命令行工具 using this API. We talked over what we should do for 其他 languages – and here we are! Let’s get started.

This Github资源库文件夹 has working Recipient Validation API call examples in around a dozen different languages. We try to cover the 最受欢迎的适用语言.

通过所有这些例子的共同工作方式是。

  • Pick up your key from environment variable 钥匙

  • Make an API call to /api/v1/recipient-validation/single/。to validate a recipient

  • 接收回一个响应字符串,包含JSON格式的数据和结果

  • 打印出结果

SparkPost has 图书馆 for some, but not all of the languages covered here. We chose to write these examples “native” instead, so we could a) cover more languages, b) show how simple the underlying code can be, and c) enable you to see clearly the similarities and differences between languages.


Bash / Curl

这赢得了最短代码奖--它只是使用命令行工具 "curl "来进行请求,并直接将回复打印到终端。你可以看到输出是一个字符串,包含JSON;我们实际上并没有解析各个结果属性。


PHP

Trusty PHP has a few different 方法 to make HTTPS API calls. Here we chose to use curl_setopt and curl_exec. (https://www.php.net/manual/en/function.curl-exec.php)

If you prefer HTTP_Request2 or pecl_http, then Postman has a built-in code generator that you can use to create similar examples – just set up a working GET request and use the “Code” button.


Python

This uses the popular requests module, which is high-level and therefore easy to use. This example checks the returned status code, converts the results JSON back into a Python dictionary object, and prints the resulting object rather than just a string.

If you prefer the built-in http.client library, Postman can generate code for that too; it’s not much longer.


Node.js

There are 许多不同的 node.js HTTP(S) libraries. I started with the older request package (using a callback function) but it’s deprecated and no longer actively maintained.  I chose the newer axios package (using 承诺). 

Postman can also give you a Javascript native example and 统一企业, in case you prefer those.

因为这段代码需要访问你的API密钥,我们强烈建议从你的服务器端调用我们的API,而不是从客户端(浏览器/移动设备)端。


Go

Go strives toward a philosophy of “one good way” to do something; in this case, using the built-in “batteries included” libraries net/http, encoding/json and others.

ǞǞǞ length is due mostly 到 explicit error checking clauses if err != nil {}。 everywhere (无一例外 LOL).

我们还用字段标签声明了结果对象结构,以使我们能够 "解密 "返回的JSON字符串。我们将 "results "和 "errors "标签叠加起来,以允许两种返回。

我喜欢Go的速度、类型安全和清晰度,即使代码比我们之前的例子要长。


C#

I’m less familiar with C# – to me, it looks quite Java-like, rather than C-like. I was able to put this together following examples shown in the request library 系统.Net.Http.

Postman can auto-generate example code using 锐意进取, if you prefer that.


Ruby

This was my first attempt at Ruby code; I used the 网友::http library, and followed 这个例子 (which turns out to be very close 到 code that Postman auto-generates).

I ran into one language / library oddity that’s worth explaining. Just setting up using a URI that begins “https://” is not enough, you have to specifically set http.use_ssl = true

如果不这样做,你的代码就会尝试在443端口发出请求--但没有使用SSL/TLS(即纯文本),SparkPost会正确地拒绝授权该请求。不要在家里尝试这样做,因为你的API密钥就在授权头里。这个语言/库的功能让我觉得不安全。


Java

I’ve not written any serious Java before, but it was easy to piece this together by following the general approach used in the SparkPost库 for other GET calls.

Incidentally, using VS Code as my editor / debugger worked really well for all the languages here, giving me syntax highlighting, debugger stepping / variables viewing etc. 输入StreamReaderand 緩衝閱讀器constructs are similar to (and I assume were copied by) Go.


C / C++

This was a trip down memory lane, as I wrote a lot of C code in the 1990s, some still running deep in telecoms networks somewhere. As the C的历史 predates the modern Web, it’s not surprising that library support is a manual task. We need to download (and compile) a recent version of 利布库尔, linking to an OpenSSL library – see the README for actual steps.

与现代语言相比,这感觉是一个很大的工作,特别是当Go(或吕阿,或Python,或任何其他语言)对这样的任务来说足够快。

ǞǞǞ other thing I had forgotten, despite bearing the scars from previous battles, is the scariness of memory allocation! To keep the example simple, I preallocated the URL string length as 1024 characters, and bounds-checked the email address length (using strlen) before we concatenate into it (using strcat).

我们以同样的方式对待授权字符串与串联的API密钥......尽管我们知道一个有效的API密钥永远不会太长......那是没有保护的!来自环境变量的用户输入可能是任何东西。你必须进行防御性的编程。

A more sophisticated developer might use mallocinstead of stack variable allocation, and calculate just how long the joined strings need to be. Having to think about this extra complexity gave me a 在我的左边的二极管上疼痛; it reminded me of the risks that C programmers run every day, trying to avoid buffer overruns and unexpected side-effects. Which brings us to ..


Lua

Lua is known for its easy coexistence alongside a body of C code, and here at SparkPost, we 广泛地使用Lua for Policy customisations inside our Momentum on-premises MTA. You can also use it as a stand-alone scripting language, and it’s pretty nice for that, too.

With Lua 5.3 and the luarocks package manager, we use libraries luasocket and luasec. Showing its C integration heritage, we link to our local OpenSSL library. The luarocks install process calls the 编码 compiler (or whatever C compiler you are using), so adding new libraries takes a while.

The Lua code is quite simple. The characters — mark comments.  The function https.request provides 多个返回值 (a bit like Python and Go). String concatenation is done with the 经营者  .. (instead of + in Python).

The response body from this call is handled with the “ltn12” module – see here. That enables efficient handling of data that could be returned in multiple “chunks”. As that article explains:

表工厂创建了一个汇,将所有获得的数据存储到一个表中。这些数据后来可以通过table.concat库函数有效地串联成一个字符串。

我们的例子只是将表t连接起来并打印出来;你可以用一个过滤器来进行更多的处理。


Perl

While Perl is famous for its 一句话, this is not one of them.  Perl was designed for very fast document search and modification, but is actually capable of so much more.  I once wrote an entire Inventory control suite in Perl.  Go figure.   A n y w a y…

This script makes use of LWP::UserAgent and HTTP::Request and optionally the JSON and Data::Dumper packages depending on how you want to see the output. As with all the other scripts on this page, you should pre-set an environment variable 钥匙to your generated API key that includes the Recipient Validation function. This script hard codes $recipient = ‘test@gmail.com’ but you can easily add command-line input or consume from a file.

After all the variables are populated, we load an HTTP:Request with GET parameters and send it to the LWP:UserAgent.  The resulting “message” is the result of the email validation test as an array.  You can use JSON and DUMPER to display the result or just pass the array on for additional processing.


VB.net

Visual Basic is not visual and it is not basic (IMHO), but it is #6 on the TIOBE语言索引 so here we go.

There are other ways to do this, but the easiest path to success is to use the 视觉工作室 SDK in a Windows platform. Fire up Visual Studio, start a new project and select Visual Basic, then select console.app.  Be sure to use the VB version not the C# version – it is easy to miss that in the SDK.


At this point you can edit lines manually or copy/paste the code 从这里 into VS and save a bunch of time. In order to make this code work, you need to add a Windows environment variable.  The easiest way to do this is to open a command prompt and use setx.exe like this: 

C:\Users\me>setx 钥匙  "142<redacted<redacted>c531c3"

在Windows 10中,这被应用到你的用户环境中,但在当前的命令会话中并不立即可用,所以用 "设置 "来测试它将不起作用,但它将被用于代码中。如果你构建并执行包含在 repo 中的代码,你会看到验证结果。


Rust is a language for systems and web-services programming that is focused on performance, safety and concurrency. As 维基百科 says, Rust has been the “most loved programming language” in the Stack Overflow Developer Survey since 2016.

The Rust code 在我们的Github repo中 uses the 锐克西 library with 苠迮郇郋郅郋迣邽迮郕邽 async, similar to this example from the Rust cookbook. (That’s not a typo, the 锐克西 library name is spelled like that). We’ve included a cargo package manager 配置文件, so you can build and run with:

cd rust_recipient_validation cargo run

这将把软件包编译成可执行代码,并运行它。

在0.10s内完成dev [unoptimized + debuginfo] target(s) 运行`target/debug/rust_recipient_validation` 状态: 200 OK 身体: : (等)

The code uses std:environmentto read the SPARKPOST_API_KEY environment variable. A match clause handles the case where the key is undefined. If all is well, a new reqwest::Client is created and an async call issued, followed by an .等待? (see here). Async, rather than the simpler blocking call, seems to be needed to set request headers. Response body text is read with a second .等待?, as per 这个例子.


Summary

在这篇文章中,我们已经走过了许多语言的收件人验证代码例子。这里是我们对你的要求。

Let us know if you think we missed your favorite language. We may not have as many examples as 斐波那契项目, but we’d love to add some more. Also, if you think our examples can be improved, let us know!

Your new standard in Marketing, Pay & Sales. It's Bird

The right message -> to the right person ->right time.

Your new standard in Marketing, Pay & Sales. It's Bird

The right message -> to the right person ->right time.