Function | Package: net.post-office | ToC | DocOverview | CGDoc | RelNotes | FAQ | Index | PermutedIndex
| Allegro CL version 9.0 Minimally revised from 8.2. 8.2 version

## send-smtp

Arguments: mail-server from to &rest message

This function requests that a SMTP server send a message. The return value of this function is undefined. If this function returns rather than signaling an error, then it has "succeeded", where "success" means the SMTP server claimed that it received the message.

mail-server specifies the mail server and also additional information. It can specify the port number, details of an SSL connection, and whether to initiate STARTTLS negotiations. The value of the argument can be a string or an IP address (to specify just a server or a server and a port number), a cons (to specify a server and a port number) or a list (to provide value for all options). See below for details.

If the server specified by mail-server requires authentication, you should use send-smtp-auth, which works just like this function but has additional required arguments login and password.

### The from and to arguments

The mail-server is contacted and asked to send a message (a string) from the email address specified by from to the email address or list of addresses specified by to. The email addresses must be of the form "foo" or "foo@bar.com". You can not use addresses like "Joe <foo@bar.com>" or "(Joe) foo@bar.com".

### The message arguments

The message sent is a concatenation of all of the message arguments (there can be as many as you like). messages should be strings or streams in any combination. (message cannot be a `mime-part-constructed` object, though see MIME messages below.) When a message is a stream, note the stream is read until an EOF is reached, but the stream is not closed nor is the file pointer reset.

A header is not prepended to the message. This means that the application program can build its own header if it wants to include in that header more than send-letter supports (e.g. a Mime encoded attachment). If no header is provided then some mail servers (e.g. sendmail) will notice this fact and will automatically create a header.

The text of the message should be lines separated by #\newline's. The smtp interface will automatically insert the necessary #\returns's when it transmits the message to the mail server.

#### MIME messages

As said above, message cannot be a `mime-part-constructed` object (see MIME support in imap.htm) but you can do something like this:

    (setf p1 (make-mime-part
    	   :text "This is a test message"
    	   :headers '(("From" . "Test User <joe@example.com>")
    		      ("To" . "Recipient <jimmy@yahoo.com>")
    		      ("Subject" . "This is a test email"))))
    (with-mime-part-constructed-stream (s p1)
       (send-smtp mail-server "joe@example.com" "jimmy@yahoo.com" s))

### The mail-server argument

The mail-server argument serves several functions. In all cases, it specifies the mail server. It can also specify a port number. And it can specify SSL and STARTTLS details. We discuss each use in turn.

#### Using mail-server to specify the server only

The value can be a string naming a machine, or an IP address (in dotted form, as an integer for IPv4 addresses, or an IPv6 address structure).

By default, this function makes a connection to port 25 on mail-server.

#### Specifing a server and a port number

If you need to connect to a port other than the default (port 25), you can specify the mail-server argument in one of the following two ways to specify the alternate port number (and, as described below, an alternate port can be specified along with SSL and STARTTLS information):

1. As a cons: if mail-server is specified as a cons, the car should be the name or IP address of the mail server (i.e. a value as described above for mail-server) and the cdr should be the alternate port number. e.g. you could specify `'("smtp.example.com" . 99)` and port 99 will be used.
2. As a colon-separate string: the text before the colon specifies the hostname or IP address of the mail server (i.e. a value as described above for mail-server) and the text after the colon specifies the alternate port number. e.g. `"smtp.example.com:99"`. Note that this form is not suitable if the mail server is specified as an IPv6 dotted address (because the dotted address uses colons as separators).

#### Specifying the server, the port, and SSL and STARTTLS information

The most general form of the mail-server argument is a list of the form:

    (server-name &key (port 25) (ssl nil) (starttls nil) ...ssl-client-keywords...)

where

- server-name is as described above for specifying the server only (i.e. a string or an IP address).
- port allows for another way to specify the port to connect to. The default port for tcp connections is 25. When ssl is non-`nil`, the default port is 465.
- If ssl is non-`nil`, make-ssl-client-stream will be called after establishing a connection to the smtp server. All ssl-client-keywords are passed to make-ssl-client-stream as well.
- If starttls is non-`nil`, it allows for negotiation of a secure connection (TLSv1) if the server supports it. If the server supports both auth and starttls, starttls will be negotiated before attempting authentication.
- ...ssl-client-keywords...: all keyword args to make-ssl-client-stream are accepted. e.g. :method :ciphers, certificate related args, etc. They are only used if ssl is non-`nil`.

No error is signalled if both ssl and starttls are non-`nil`. The SSL connection is established before any starttls negotiation would begin.

### An example

    (use-package :net.post-office)
    (defparameter *smtp-host* "localhost")
    (defparameter *smtp-user* "someuser")
    (defparameter *smtp-password* "somepassword")

    ;; ssl with authentication
    (let ((part (make-mime-part
                 :text "This is a test message sent via Secure smtp server with authentication."
                 :headers '(("From" . "Test User <joe@example.com>")
                            ("To" . "Recipient <jimmy@yahoo.com>")
                            ("Subject" . "This is a test email")))))
      (with-mime-part-constructed-stream (s part)
        (send-smtp-auth (list *smtp-host* :ssl t :method :sslv3+)
                         *smtp-user*
                         "jimmy@yahoo.com"
                         *smtp-user* *smtp-password*
                         s)))

    ;; with starttls negotiation
    (let ((part (make-mime-part
                 :text "This is a test message sent possibly via a TLSv1 encrypted channel."
                 :headers '(("From" . "Test User <joe@example.com>")
                            ("To" . "Recipient <jimmy@yahoo.com>")
                            ("Subject" . "This is a test email")))))
      (with-mime-part-constructed-stream (s part)
        (send-smtp (list *smtp-host* :starttls t)
                         *smtp-user*
                         "jimmy@yahoo.com"
                         s)))

See imap.htm for more information.

Copyright (c) 1998-2019, Franz Inc. Oakland, CA., USA. All rights reserved.
This page has had minimal revisions compared to the 8.2 page.
Created 2019.8.20.

|  | ToC | DocOverview | CGDoc | RelNotes | FAQ | Index | PermutedIndex
| Allegro CL version 9.0 Minimally revised from 8.2. 8.2 version