Module for sending plain-text e-mails via SMTP, using libcurl.
To send an e-mail, create an SMTP server configuration and a mail type first:
integer :: rc
type(mail_type) :: mail
type(mail_server_type) :: server
type(person_type) :: from, to(1)
! Initialise SMTP backend and set SMTP server details.
rc = dm_mail_init()
rc = dm_mail_create(server, 'example.com', 'username', 'password')
! Create sender, receiver, and e-mail.
from = person_type(mail='alice@example.com')
to(1) = person_type(mail='bob@example.com')
rc = dm_mail_create(mail, from=from, to=to, subject='Subject', message='Message')
! Send e-mail and finalise SMTP backend.
rc = dm_mail_send(mail, server)
call dm_mail_shutdown()
The procedure dm_mail_init()
has to be called once per process, and
only if neither the RPC nor the MQTT backend is initialised already.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public, | parameter | :: | MAIL_PLAIN | = | 0 |
No transport-layer security. |
integer, | public, | parameter | :: | MAIL_SSL | = | 1 |
Explicit SSL. |
integer, | public, | parameter | :: | MAIL_TLS | = | 2 |
Implicit TLS (StartTLS). |
Generic function that returns formatted addresses.
Returns e-mail address as allocatable string in the form <address>
or "name" <address>
, depending on whether the person has a name.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(person_type), | intent(in) | :: | person |
Person type. |
Address string.
Returns list of e-mail addresses in allocatable string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(person_type), | intent(in) | :: | persons(:) |
Array of person types. |
List of addresses.
Generic function to create mail or server data type.
Creates new mail type and inserts passed values. The function return
E_INVALID
if given arguments are invalid or incomplete.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(mail_type), | intent(out) | :: |
Mail type. |
|||
type(person_type), | intent(in) | :: | from |
Mail sender. |
||
type(person_type), | intent(in) | :: | to(:) |
Mail recipients. |
||
character(len=*), | intent(in) | :: | subject |
Mail subject. |
||
character(len=*), | intent(in) | :: | message |
Mail message. |
||
type(person_type), | intent(in), | optional | :: | cc(:) |
Mail CC recipients. |
|
type(person_type), | intent(in), | optional | :: | bcc(:) |
Mail BCC recipients. |
Returns SMTP server type. Argument tls
may be one of the following:
MAIL_PLAIN
– No transport-layer security.MAIL_SSL
– Explicit SSL.MAIL_TLS
– Implicit TLS (StartTLS).Parameter MAIL_PLAIN
is used by default. The function returns
E_INVALID
on error.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(mail_server_type), | intent(out) | :: | server |
Mail server type. |
||
character(len=*), | intent(in) | :: | host |
SMTP server host. |
||
character(len=*), | intent(in) | :: | username |
SMTP user name. |
||
character(len=*), | intent(in) | :: | password |
SMTP password. |
||
integer, | intent(in), | optional | :: | port |
SMTP server port (or 0). |
|
integer, | intent(in), | optional | :: | tls |
SMTP transport-layer security. |
|
integer, | intent(in), | optional | :: | timeout |
cURL timeout in seconds. |
|
integer, | intent(in), | optional | :: | connect_timeout |
cURL connection timeout in seconds. |
|
logical, | intent(in), | optional | :: | verify_ssl |
Verify SSL cert. |
Generic routine to print mail and server type.
Prints mail type to standard output or given file unit.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(mail_type), | intent(inout) | :: |
Mail type. |
|||
integer, | intent(in), | optional | :: | unit |
File unit. |
Prints mail server type to standard output or given file unit.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(mail_server_type), | intent(inout) | :: | server |
Mail server type. |
||
integer, | intent(in), | optional | :: | unit |
File unit. |
Opaque SMTP server type that stores connection settings.
Opaque e-mail type that stores sender, recipients, subject, message, and allocation status.
Creates new mail type and inserts passed values. The function return
E_INVALID
if given arguments are invalid or incomplete.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(mail_type), | intent(out) | :: |
Mail type. |
|||
type(person_type), | intent(in) | :: | from |
Mail sender. |
||
type(person_type), | intent(in) | :: | to(:) |
Mail recipients. |
||
character(len=*), | intent(in) | :: | subject |
Mail subject. |
||
character(len=*), | intent(in) | :: | message |
Mail message. |
||
type(person_type), | intent(in), | optional | :: | cc(:) |
Mail CC recipients. |
|
type(person_type), | intent(in), | optional | :: | bcc(:) |
Mail BCC recipients. |
Returns SMTP server type. Argument tls
may be one of the following:
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(mail_server_type), | intent(out) | :: | server |
Mail server type. |
||
character(len=*), | intent(in) | :: | host |
SMTP server host. |
||
character(len=*), | intent(in) | :: | username |
SMTP user name. |
||
character(len=*), | intent(in) | :: | password |
SMTP password. |
||
integer, | intent(in), | optional | :: | port |
SMTP server port (or 0). |
|
integer, | intent(in), | optional | :: | tls |
SMTP transport-layer security. |
|
integer, | intent(in), | optional | :: | timeout |
cURL timeout in seconds. |
|
integer, | intent(in), | optional | :: | connect_timeout |
cURL connection timeout in seconds. |
|
logical, | intent(in), | optional | :: | verify_ssl |
Verify SSL cert. |
Converts cURL easy stack error code to DMPACK error code.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | error_curl |
cURL easy error code. |
Return message associated with given cURL error code as allocatable character string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | error_curl |
cURL error code. |
Error message.
Initialises SMTP backend. The function returns E_MAIL
on error.
Sends SMTP request by calling libcurl.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(mail_type), | intent(inout) | :: |
Mail type. |
|||
type(mail_server_type), | intent(inout) | :: | server |
Mail server type. |
||
character(len=:), | intent(out), | optional, | allocatable | :: | error_message |
Error message. |
integer, | intent(out), | optional | :: | error_curl |
cURL error code. |
|
logical, | intent(in), | optional | :: | debug |
Output debug messages. |
Returns allocatable string of SMTP server URL in the form
smtp[s]://host[:port]/
. Uses the URL API of libcurl to create the
URL. By default, Transport Layer Security is disabled.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | host |
SMTP server host name. |
||
integer, | intent(in), | optional | :: | port |
SMTP server port (up to 5 digits). |
|
logical, | intent(in), | optional | :: | tls |
Transport-layer security. |
URL of SMTP server.
Returns allocatable e-mail string. This function does not verify the allocation state of the given mail type. It has been made public to simplify testing.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(mail_type), | intent(inout) | :: |
Mail type. |
E-mail data.
Callback function to upload payload passed via data
to the
memory chunk in ptr
. Do not call this function directly.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(c_ptr), | intent(in), | value | :: | ptr |
C pointer to a chunk of memory. |
|
integer(kind=c_size_t), | intent(in), | value | :: | sz |
Always 1. |
|
integer(kind=c_size_t), | intent(in), | value | :: | nmemb |
Size of the memory chunk. |
|
type(c_ptr), | intent(in), | value | :: | data |
C pointer to argument passed by caller. |
Function return value.
Cleans up SMTP backend.