smtp-client
SMTP Client C Library
SMTPMail Class Reference

#include <SMTPMail.h>

+ Collaboration diagram for SMTPMail:

Public Member Functions

 SMTPMail (void)
 
 ~SMTPMail (void)
 
void open (const char *const server, const char *const port, enum smtp_connection_security connection_security, enum smtp_flag flags, const char *const cafile)
 
void auth (enum smtp_authentication_method auth_method, const char *const user, const char *const pass)
 
void mail (const char *const body)
 
void close (void)
 
int status_code_get (void)
 
void status_code_set (enum smtp_status_code new_status_code)
 
void header_add (const char *const key, const char *const value)
 
void header_clear_all (void)
 
void address_add (enum smtp_address_type type, const char *const email, const char *const name)
 
void address_clear_all (void)
 
void attachment_add_path (const char *const name, const char *const path)
 
void attachment_add_fp (const char *const name, FILE *fp)
 
void attachment_add_mem (const char *const name, const void *const data, ssize_t datasz)
 
void attachment_clear_all (void)
 

Private Member Functions

void throw_bad_status_code (void)
 

Private Attributes

struct smtpsmtp
 
enum smtp_status_code rc
 

Detailed Description

Thin CPP wrapper class over the smtp-client C library.

Definition at line 53 of file SMTPMail.h.

Constructor & Destructor Documentation

◆ SMTPMail()

SMTPMail::SMTPMail ( void  )

Create the SMTPMail object.

Definition at line 22 of file SMTPMail.cpp.

22  {
23 }

◆ ~SMTPMail()

SMTPMail::~SMTPMail ( void  )

Free the SMTPMail object.

Definition at line 25 of file SMTPMail.cpp.

25  {
26 }

Member Function Documentation

◆ address_add()

void SMTPMail::address_add ( enum smtp_address_type  type,
const char *const  email,
const char *const  name 
)

Add a FROM, TO, CC, or BCC address destination to this SMTP context.

See smtp_address_add.

Parameters
[in]typeSee smtp_address_type.
[in]emailThe email address of the party. Must consist only of printable characters excluding the angle brackets (<) and (>).
[in]nameName or description of the party. Must consist only of printable characters, excluding the quote characters. If set to NULL, no name will get associated with this email.

Definition at line 78 of file SMTPMail.cpp.

References smtp_address_add().

Referenced by main().

80  {
81  this->rc = smtp_address_add(this->smtp, type, email, name);
82  this->throw_bad_status_code();
83 }
enum smtp_status_code rc
Definition: SMTPMail.h:233
enum smtp_status_code smtp_address_add(struct smtp *const smtp, enum smtp_address_type type, const char *const email, const char *const name)
Definition: smtp.c:3347
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ address_clear_all()

void SMTPMail::address_clear_all ( void  )

Free all memory related to the address list.

See smtp_address_clear_all.

Definition at line 85 of file SMTPMail.cpp.

References smtp_address_clear_all().

85  {
87 }
void smtp_address_clear_all(struct smtp *const smtp)
Definition: smtp.c:3401
Definition: smtp.c:150
+ Here is the call graph for this function:

◆ attachment_add_fp()

void SMTPMail::attachment_add_fp ( const char *const  name,
FILE *  fp 
)

Add an attachment using a file pointer.

See smtp_attachment_add_fp.

Parameters
[in]nameFilename of the attachment shown to recipients.
[in]fpFile pointer already opened by the caller.

Definition at line 95 of file SMTPMail.cpp.

References smtp_attachment_add_fp().

96  {
97  this->rc = smtp_attachment_add_fp(this->smtp, name, fp);
98  this->throw_bad_status_code();
99 }
enum smtp_status_code rc
Definition: SMTPMail.h:233
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
enum smtp_status_code smtp_attachment_add_fp(struct smtp *const smtp, const char *const name, FILE *fp)
Definition: smtp.c:3439
+ Here is the call graph for this function:

◆ attachment_add_mem()

void SMTPMail::attachment_add_mem ( const char *const  name,
const void *const  data,
ssize_t  datasz 
)

Add an attachment with the data pulled from memory.

See smtp_attachment_add_mem.

Parameters
[in]nameFilename of the attachment shown to recipients. Must consist only of printable characters excluding the quote characters (') and ("), or the space character ( ).
[in]dataRaw attachment data stored in memory.
[in]dataszNumber of bytes in data, or -1 if data null-terminated.

Definition at line 101 of file SMTPMail.cpp.

References smtp_attachment_add_mem().

103  {
104  this->rc = smtp_attachment_add_mem(this->smtp, name, data, datasz);
105  this->throw_bad_status_code();
106 }
enum smtp_status_code smtp_attachment_add_mem(struct smtp *const smtp, const char *const name, const void *const data, size_t datasz)
Definition: smtp.c:3462
enum smtp_status_code rc
Definition: SMTPMail.h:233
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
+ Here is the call graph for this function:

◆ attachment_add_path()

void SMTPMail::attachment_add_path ( const char *const  name,
const char *const  path 
)

Add a file attachment from a path.

See smtp_attachment_add_mem.

Parameters
[in]nameFilename of the attachment shown to recipients.
[in]pathPath of file location to read from.

Definition at line 89 of file SMTPMail.cpp.

References smtp_attachment_add_path().

90  {
91  this->rc = smtp_attachment_add_path(this->smtp, name, path);
92  this->throw_bad_status_code();
93 }
enum smtp_status_code smtp_attachment_add_path(struct smtp *const smtp, const char *const name, const char *const path)
Definition: smtp.c:3416
enum smtp_status_code rc
Definition: SMTPMail.h:233
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
+ Here is the call graph for this function:

◆ attachment_clear_all()

void SMTPMail::attachment_clear_all ( void  )

Remove all attachments from the SMTP client context.

See smtp_attachment_clear_all.

Definition at line 108 of file SMTPMail.cpp.

References smtp_attachment_clear_all().

108  {
110 }
void smtp_attachment_clear_all(struct smtp *const smtp)
Definition: smtp.c:3515
Definition: smtp.c:150
+ Here is the call graph for this function:

◆ auth()

void SMTPMail::auth ( enum smtp_authentication_method  auth_method,
const char *const  user,
const char *const  pass 
)

Authenticate the user using one of the methods listed in smtp_authentication_method.

See smtp_auth.

Parameters
[in]auth_methodSee smtp_authentication_method.
[in]userServer authentication user name.
[in]passServer authentication user password.

Definition at line 42 of file SMTPMail.cpp.

References smtp_auth().

Referenced by main().

44  {
45  this->rc = smtp_auth(this->smtp, auth_method, user, pass);
46  this->throw_bad_status_code();
47 }
enum smtp_status_code rc
Definition: SMTPMail.h:233
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
enum smtp_status_code smtp_auth(struct smtp *const smtp, enum smtp_authentication_method auth_method, const char *const user, const char *const pass)
Definition: smtp.c:3026
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ close()

void SMTPMail::close ( void  )

Close the SMTP connection and frees all resources held by the SMTP context.

See smtp_close.

Definition at line 54 of file SMTPMail.cpp.

References smtp_close().

Referenced by main().

54  {
55  this->rc = smtp_close(this->smtp);
56  this->throw_bad_status_code();
57 }
enum smtp_status_code smtp_close(struct smtp *smtp)
Definition: smtp.c:3168
enum smtp_status_code rc
Definition: SMTPMail.h:233
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ header_add()

void SMTPMail::header_add ( const char *const  key,
const char *const  value 
)

Add a key/value header to the header list in the SMTP context.

See smtp_header_add.

Parameters
[in]keyKey name for new header. It must consist only of printable US-ASCII characters except colon.
[in]valueValue for new header. It must consist only of printable US-ASCII, space, or horizontal tab. If set to NULL, this will prevent the header from printing out.

Definition at line 68 of file SMTPMail.cpp.

References smtp_header_add().

Referenced by main().

69  {
70  this->rc = smtp_header_add(this->smtp, key, value);
71  this->throw_bad_status_code();
72 }
enum smtp_status_code smtp_header_add(struct smtp *const smtp, const char *const key, const char *const value)
Definition: smtp.c:3278
enum smtp_status_code rc
Definition: SMTPMail.h:233
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ header_clear_all()

void SMTPMail::header_clear_all ( void  )

Free all memory related to email headers.

See smtp_header_clear_all.

Definition at line 74 of file SMTPMail.cpp.

References smtp_header_clear_all().

74  {
76 }
void smtp_header_clear_all(struct smtp *const smtp)
Definition: smtp.c:3332
Definition: smtp.c:150
+ Here is the call graph for this function:

◆ mail()

void SMTPMail::mail ( const char *const  body)

Sends an email using the addresses, attachments, and headers defined in the current SMTP context.

See smtp_mail.

Parameters
[in]bodyNull-terminated string to send in the email body.

Definition at line 49 of file SMTPMail.cpp.

References smtp_mail().

Referenced by main().

49  {
50  this->rc = smtp_mail(this->smtp, body);
51  this->throw_bad_status_code();
52 }
enum smtp_status_code smtp_mail(struct smtp *const smtp, const char *const body)
Definition: smtp.c:3062
enum smtp_status_code rc
Definition: SMTPMail.h:233
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ open()

void SMTPMail::open ( const char *const  server,
const char *const  port,
enum smtp_connection_security  connection_security,
enum smtp_flag  flags,
const char *const  cafile 
)

Open a connection to an SMTP server.

See smtp_open.

Parameters
[in]serverServer name or IP address.
[in]portServer port number.
[in]connection_securitySee smtp_connection_security.
[in]flagsSee smtp_flag.
[in]cafilePath to certificate file, or NULL to use certificates in the default path.

Definition at line 28 of file SMTPMail.cpp.

References smtp_open().

Referenced by main().

32  {
33  this->rc = smtp_open(server,
34  port,
35  connection_security,
36  flags,
37  cafile,
38  &this->smtp);
39  this->throw_bad_status_code();
40 }
enum smtp_flag flags
Definition: smtp.c:154
const char * cafile
Definition: smtp.c:221
enum smtp_status_code rc
Definition: SMTPMail.h:233
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
enum smtp_status_code smtp_open(const char *const server, const char *const port, enum smtp_connection_security connection_security, enum smtp_flag flags, const char *const cafile, struct smtp **smtp)
Definition: smtp.c:2987
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ status_code_get()

int SMTPMail::status_code_get ( void  )

Get the current status/error code described in smtp_status_code.

See smtp_status_code_get.

Returns
smtp_status_code.

Definition at line 59 of file SMTPMail.cpp.

References smtp_status_code_get().

59  {
60  return smtp_status_code_get(this->smtp);
61 }
Definition: smtp.c:150
enum smtp_status_code smtp_status_code_get(const struct smtp *const smtp)
Definition: smtp.c:3217
+ Here is the call graph for this function:

◆ status_code_set()

void SMTPMail::status_code_set ( enum smtp_status_code  new_status_code)

Set the error status of the SMTP client context.

See smtp_status_code_set.

Parameters
[in]new_status_codeSee smtp_status_code.

Definition at line 63 of file SMTPMail.cpp.

References smtp_status_code_set().

63  {
64  this->rc = smtp_status_code_set(this->smtp, new_status_code);
65  this->throw_bad_status_code();
66 }
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code new_status_code)
Definition: smtp.c:3231
enum smtp_status_code rc
Definition: SMTPMail.h:233
void throw_bad_status_code(void)
Definition: SMTPMail.cpp:112
Definition: smtp.c:150
+ Here is the call graph for this function:

◆ throw_bad_status_code()

void SMTPMail::throw_bad_status_code ( void  )
private

Throw SMTPMailException if the last smtp-client library function failed.

Definition at line 112 of file SMTPMail.cpp.

References SMTP_STATUS_OK, and SMTPMailException::SMTPMailException().

112  {
113  if(this->rc != SMTP_STATUS_OK){
114  throw SMTPMailException(this->rc);
115  }
116 }
enum smtp_status_code rc
Definition: SMTPMail.h:233
+ Here is the call graph for this function:

Field Documentation

◆ rc

enum smtp_status_code SMTPMail::rc
private

Store the last smtp-client library function return code.

Definition at line 233 of file SMTPMail.h.

◆ smtp

struct smtp* SMTPMail::smtp
private

SMTP client context.

Definition at line 228 of file SMTPMail.h.


The documentation for this class was generated from the following files: