6 SMTP: Sending E-Mail

▼ |  | Net: Networking Libraries

1 | HTTP Client
2 | URLs and HTTP
3 | URI Codec: Encoding and Decoding URIs
4 | FTP: Client
5 | Send URL: Opening a Web Browser
6 | SMTP: Sending E-Mail
7 | sendmail: Sending E-Mail
8 | Headers: Parsing and Constructing
9 | Header Field Encoding
10 | IMAP: Reading Mail
11 | POP3: Reading Mail
12 | MIME: Decoding Internet Data
13 | Base 64: Encoding and Decoding
14 | Quoted-Printable: Encoding and Decoding
15 | DNS: Domain Name Service Queries
16 | NNTP: Newsgroup Protocol
17 | TCP: Unit and Signature
18 | TCP Redirect: tcp^ via Channels
19 | SSL Unit: tcp^ via SSL
20 | CGI Scripts
21 | Cookie: Legacy HTTP Client Storage
22 | Git Repository Checkout
| Bibliography
| Index

► | 6 | SMTP: Sending E-Mail

6.1 | SMTP Functions
6.2 | SMTP Unit
6.3 | SMTP Signature

On this page:

6.1 SMTP Functions
smtp-send-message
smtp-sending-end-of-message
6.2 SMTP Unit
smtp@
6.3 SMTP Signature
smtp^

Racket
top contents ← prev up next →

## 6 SMTP: Sending E-Mail🔗ℹ

(require net/smtp) | package: net-lib

The net/smtp module provides tools for sending electronic mail messages using SMTP. The client must provide the address of an SMTP server; in contrast, the net/sendmail module uses a pre-configured sendmail on the local system.

The net/head library defines the format of a header string, which is used by smtp-send-message. The net/head module also provides utilities to verify the formatting of a mail address. The procedures of the net/smtp module assume that the given string arguments are well-formed.

### 6.1 SMTP Functions🔗ℹ

> > procedure
> > (smtp-send-message |  | server-address |  |  |  |
> > |  | from |  |  |  |
> > |  | to |  |  |  |
> > |  | header |  |  |  |
> > |  | message |  |  |  |
> > | [ | #:port-no port-no/k |  |  |  |
> > |  | #:auth-user user |  |  |  |
> > |  | #:auth-passwd pw |  |  |  |
> > |  | #:xoauth2? xoauth2? |  |  |  |
> > |  | #:tcp-connect connect |  |  |  |
> > |  | #:tls-encode encode |  |  |  |
> > |  | port-no]) |  | → |  | void?
> server-address : string?
> from : string?
> to : (listof string?)
> header : string?
> message : (listof (or/c string? bytes?))
> port-no/k : (integer-in 0 65535) = 25
> user : (or/c string? #f) = #f
> pw : (or/c string? #f) = #f
> xoauth2? : any/c = #f
> | connect |  | : |  |
> (string? (integer-in 0 65535)
> . -> . (values input-port? output-port?))
> |  |  | = |  | tcp-connect
> | encode |  | : |  |
> (or/c #f
> (input-port? output-port?
> #:mode 'connect
> #:encrypt 'tls
> #:close-original? #t
> . -> . (values input-port? output-port?)))
> |  |  | = |  | #f
> port-no : (integer-in 0 65535) = port-no/k

Connects to the server at server-address and port-no to send a message. The from argument specifies the mail address of the sender, and to is a list of recipient addresses (including “To:”, “CC”, and “BCC” recipients).

The header argument is the complete message header, which should already include “From:”, “To:”, and “CC:” fields consistent with the given sender and recipients. See also the net/head library for header-creating utilities.

The message argument is the body of the message, where each string or byte string in the list corresponds to a single line of message text. No string in message should contain a carriage return or linefeed character.

The optional port-no argument—which can be specified either with the #:port-no keyword or, for backward compatibility, as an extra argument after keywords—specifies the IP port to use in contacting the SMTP server.

The optional user and pw arguments supply a username and password for authenticated SMTP using the AUTH PLAIN protocol. If xoauth2? is true, then authentication uses the AUTH XOAUTH2 protocol, instead, and pw is used as an access token (which must obtained somehow before calling smtp-send-message).

The optional connect argument supplies a connection procedure to be used in place of tcp-connect. For example, use ssl-connect to connect to the server via SSL.

If the optional encode argument supplies a procedure instead of #f, then the ESMTP STARTTLS protocol is used to initiate SSL communication with the server. The procedure given as the encode argument should be like ports->ssl-ports; it will be called as

> (encode r w #:mode 'connect #:encrypt 'tls #:close-original? #t)

and it should return two values: an input port and an output port. All further SMTP communication uses the returned ports.

For encrypted communication, normally either ssl-connect should be supplied for connect, or ports->ssl-ports should be supplied for encode—one or the other (depending on what the server expects), rather than both.

Changed in version 1.2 of package net-lib: Added the xoauth2? argument.

> parameter (smtp-sending-end-of-message) → (-> any)
> (smtp-sending-end-of-message proc) → void?
> proc : (-> any)

A parameter that determines a send-done procedure to be called after smtp-send-message has completely sent the message. Before the send-done procedure is called, breaking the thread that is executing smtp-send-message cancels the send. After the send-done procedure is called, breaking may or may not cancel the send (and probably will not).

### 6.2 SMTP Unit🔗ℹ

> > > smtp@ and smtp^ are deprecated. They exist for backward-compatibility and will likely be removed in the future. New code should use the net/smtp module.

(require net/smtp-unit) | package: compatibility-lib

> value smtp@ : unit?

Imports nothing, exports smtp^.

### 6.3 SMTP Signature🔗ℹ

(require net/smtp-sig) | package: compatibility-lib

> signature smtp^ : signature

Includes everything exported by the net/smtp module.

top contents ← prev up next →