Allow applications to override Content-Type header Applications must generate the appropriate MIME sections (if needed) when overriding the Content-Type header, and attachments added to the SMTP context will get ignored. - Applications can now set the Content-Type header - Add an HTML example program to the README - Extracted both example programs from the README and add to the build system - Corrected the size of the header list in smtp_header_exists function Fixes #3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
diff --git a/Makefile b/Makefile
index d1f1216..f56e77d 100644
--- a/Makefile
+++ b/Makefile
@@ -158,6 +158,8 @@ all: $(BDIR)/debug/libsmtp.a \
$(BDIR)/doc/html/index.html \
$(BDIR)/debug/test \
$(BDIR)/debug/clang_test \
+ $(BDIR)/release/example_simple \
+ $(BDIR)/release/example_html \
$(BDIR)/release/test_nossl
clean:
@@ -185,7 +187,7 @@ test: all \
test_unit
$(SCAN_BUILD)
$(VALGRIND_MEMCHECK) $(BDIR)/debug/test
- $(VALGRIND_MEMCHECK) $(BDIR)/debug/clang_test
+ ##$(VALGRIND_MEMCHECK) $(BDIR)/debug/clang_test
$(VALGRIND_MEMCHECK) $(BDIR)/release/test_nossl
$(BDIR)/release/test_cpp_wrapper
@@ -283,6 +285,18 @@ $(BDIR)/debug/clang_smtp.o: src/smtp.c | $(BDIR)/debug
$(BDIR)/debug/clang_test.o: test/test.c | $(BDIR)/debug
$(COMPILE.c.clang) $(CDEF_POSIX) -Isrc/
+$(BDIR)/release/example_simple: $(BDIR)/release/example_simple.o \
+ $(BDIR)/release/smtp.o
+ $(LINK.c.release) -lssl -lcrypto
+$(BDIR)/release/example_simple.o: test/example_simple.c | $(BDIR)/release
+ $(COMPILE.c.release) -Isrc
+
+$(BDIR)/release/example_html: $(BDIR)/release/example_html.o \
+ $(BDIR)/release/smtp.o
+ $(LINK.c.release) -lssl -lcrypto
+$(BDIR)/release/example_html.o: test/example_html.c | $(BDIR)/release
+ $(COMPILE.c.release) -Isrc
+
$(BDIR)/release/test_nossl: $(BDIR)/release/smtp_nossl.o \
$(BDIR)/release/test_nossl.o
$(LINK.c.release)
diff --git a/README.md b/README.md
index 905fbbe..ce5ce92 100644
--- a/README.md
+++ b/README.md
@@ -120,6 +120,77 @@ prior functions, the error will continue to propagate through any future
function calls until either closing the SMTP context using smtp_close or by
resetting the error condition using smtp_status_code_clear.
+The following example demonstrates how to send an HTML email by overriding the
+Content-Type header. When overriding this header, any attachments added using
+the smtp_attachment_add* functions will get ignored. The application must
+generate the appropriate MIME sections (if needed) when overriding this
+header.
+
+```C
+#include <stdio.h>
+#include "smtp.h"
+#define MAIL_SERVER "localhost"
+#define MAIL_PORT "25"
+#define MAIL_CONNECTION_SECURITY SMTP_SECURITY_NONE
+#define MAIL_FLAGS SMTP_DEBUG
+#define MAIL_CAFILE NULL
+#define MAIL_AUTH SMTP_AUTH_NONE
+#define MAIL_USER "mail@somnisoft.com"
+#define MAIL_PASS "password"
+#define MAIL_FROM "mail@somnisoft.com"
+#define MAIL_FROM_NAME "From Name"
+#define MAIL_SUBJECT "Subject Line"
+#define MAIL_TO "mail@somnisoft.com"
+#define MAIL_TO_NAME "To Name"
+int main(void){
+ struct smtp *smtp;
+ enum smtp_status_code rc;
+ const char *const email_body =
+ "<html>\n"
+ " <head><title>HTML Email</title></head>\n"
+ " <body>\n"
+ " <h1>H1</h1>\n"
+ " <h2>H2</h1>\n"
+ " <h3>H3</h1>\n"
+ " <h4>H4</h1>\n"
+ " <h5>H5</h1>\n"
+ " <h6>H6</h1>\n"
+ " </body>\n"
+ "</html>\n";
+ smtp_open(MAIL_SERVER,
+ MAIL_PORT,
+ MAIL_CONNECTION_SECURITY,
+ MAIL_FLAGS,
+ MAIL_CAFILE,
+ &smtp);
+ smtp_auth(smtp,
+ MAIL_AUTH,
+ MAIL_USER,
+ MAIL_PASS);
+ smtp_address_add(smtp,
+ SMTP_ADDRESS_FROM,
+ MAIL_FROM,
+ MAIL_FROM_NAME);
+ smtp_address_add(smtp,
+ SMTP_ADDRESS_TO,
+ MAIL_TO,
+ MAIL_TO_NAME);
+ smtp_header_add(smtp,
+ "Subject",
+ MAIL_SUBJECT);
+ smtp_header_add(smtp,
+ "Content-Type",
+ "text/html");
+ smtp_mail(smtp, email_body);
+ rc = smtp_close(smtp);
+ if(rc != SMTP_STATUS_OK){
+ fprintf(stderr, "smtp failed: %s\n", smtp_status_code_errstr(rc));
+ return 1;
+ }
+ return 0;
+}
+```
+
## Technical Documentation
See the [Technical Documentation](https://www.somnisoft.com/smtp-client/technical-documentation/index.html) generated from Doxygen.
diff --git a/src/smtp.c b/src/smtp.c
index 8298110..b166d2c 100644
--- a/src/smtp.c
+++ b/src/smtp.c
@@ -2323,6 +2323,47 @@ smtp_date_rfc_2822(char *const date){
}
/**
+ * Search function used by bsearch, allowing the caller to check for
+ * headers with existing keys.
+ *
+ * @param v1 String to search for in the list.
+ * @param v2 The @ref smtp_header to compare.
+ * @retval 0 If the keys match.
+ * @retval !0 If the keys do not match.
+ */
+static int
+smtp_header_cmp_key(const void *const v1,
+ const void *const v2){
+ const char *key;
+ const struct smtp_header *header2;
+
+ key = v1;
+ header2 = v2;
+ return strcmp(key, header2->key);
+}
+
+/**
+ * Determine if the header key has already been defined in this context.
+ *
+ * @param[in] smtp SMTP client context.
+ * @param[in] key Header key value to search for.
+ * @retval 1 If the header already exists in this context.
+ * @retval 0 If the header does not exist in this context.
+ */
+static int
+smtp_header_exists(const struct smtp *const smtp,
+ const char *const key){
+ if(bsearch(key,
+ smtp->header_list,
+ smtp->num_headers,
+ sizeof(*smtp->header_list),
+ smtp_header_cmp_key) == NULL){
+ return 0;
+ }
+ return 1;
+}
+
+/**
* Minimum length of buffer required to hold the MIME boundary test:
* mimeXXXXXXXXXX
* 123456789012345
@@ -2364,35 +2405,25 @@ smtp_gen_mime_boundary(char *const boundary){
*
* @param[in] smtp SMTP client context.
* @param[in] boundary MIME boundary text.
- * @param[in] body Email body text.
+ * @param[in] body_dd Email body with double dots added at the
+ * beginning of each line.
* @return See @ref smtp_status_code.
*/
static enum smtp_status_code
smtp_print_mime_header_and_body(struct smtp *const smtp,
const char *const boundary,
- const char *const body){
+ const char *const body_dd){
/* Buffer size for the static MIME text used below. */
const size_t MIME_TEXT_BUFSZ = 1000;
- char *data_double_dot;
size_t data_double_dot_len;
char *data_header_and_body;
char *concat;
- /*
- * Insert an extra dot for each line that begins with a dot. This will
- * prevent data in the body parameter from prematurely ending the DATA
- * segment.
- */
- if((data_double_dot = smtp_str_replace("\r\n.", "\r\n..", body)) == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
-
- data_double_dot_len = strlen(data_double_dot);
+ data_double_dot_len = strlen(body_dd);
if(smtp_si_add_size_t(data_double_dot_len,
MIME_TEXT_BUFSZ,
&data_double_dot_len) ||
(data_header_and_body = malloc(data_double_dot_len)) == NULL){
- free(data_double_dot);
return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
}
@@ -2413,12 +2444,11 @@ smtp_print_mime_header_and_body(struct smtp *const smtp,
"Content-Type: text/plain; charset=\"UTF-8\"\r\n"
"\r\n");
concat = smtp_stpcpy(concat,
- data_double_dot);
+ body_dd);
smtp_stpcpy(concat,
"\r\n"
"\r\n");
- free(data_double_dot);
smtp_puts(smtp, data_header_and_body);
free(data_header_and_body);
return smtp->status_code;
@@ -2506,20 +2536,21 @@ smtp_print_mime_end(struct smtp *const smtp,
*
* This includes the MIME sections for the email body and attachments.
*
- * @param[in] smtp SMTP client context.
- * @param[in] body Null-terminated string to send in the email body.
+ * @param[in] smtp SMTP client context.
+ * @param[in] body_dd Email body with double dots added at the
+ * beginning of each line.
* @return See @ref smtp_status_code.
*/
static enum smtp_status_code
smtp_print_mime_email(struct smtp *const smtp,
- const char *const body){
+ const char *const body_dd){
char boundary[SMTP_MIME_BOUNDARY_LEN];
size_t i;
struct smtp_attachment *attachment;
smtp_gen_mime_boundary(boundary);
- if(smtp_print_mime_header_and_body(smtp, boundary, body) != SMTP_STATUS_OK){
+ if(smtp_print_mime_header_and_body(smtp, boundary, body_dd) != SMTP_STATUS_OK){
return smtp->status_code;
}
@@ -2536,6 +2567,51 @@ smtp_print_mime_email(struct smtp *const smtp,
}
/**
+ * Print the email data provided by the user without MIME formatting.
+ *
+ * @param[in,out] smtp SMTP client context.
+ * @param[in] body_dd Email body with double dots added at the
+ * beginning of each line.
+ * @return See @ref smtp_status_code.
+ */
+static enum smtp_status_code
+smtp_print_nomime_email(struct smtp *const smtp,
+ const char *const body_dd){
+ return smtp_puts_terminate(smtp, body_dd);
+}
+
+/**
+ * Send the email body to the mail server.
+ *
+ * @param[in,out] smtp SMTP client context.
+ * @param[in] body Email body text.
+ * @return See @ref smtp_status_code.
+ */
+static enum smtp_status_code
+smtp_print_email(struct smtp *const smtp,
+ const char *const body){
+ char *body_double_dot;
+
+ /*
+ * Insert an extra dot for each line that begins with a dot. This will
+ * prevent data in the body parameter from prematurely ending the DATA
+ * segment.
+ */
+ if((body_double_dot = smtp_str_replace("\n.", "\n..", body)) == NULL){
+ return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
+ }
+
+ if(smtp_header_exists(smtp, "Content-Type")){
+ smtp_print_nomime_email(smtp, body_double_dot);
+ }
+ else{
+ smtp_print_mime_email(smtp, body_double_dot);
+ }
+ free(body_double_dot);
+ return smtp->status_code;
+}
+
+/**
* Convert a header into an RFC 5322 formatted string and send it to the
* SMTP server.
*
@@ -2736,47 +2812,6 @@ smtp_header_cmp(const void *v1,
}
/**
- * Search function used by bsearch which allows the caller to check for
- * headers with existing keys.
- *
- * @param v1 String to search for in the list.
- * @param v2 The @ref smtp_header to compare.
- * @retval 0 If the keys match.
- * @retval !0 If the keys do not match.
- */
-static int
-smtp_header_cmp_key(const void *const v1,
- const void *const v2){
- const char *key;
- const struct smtp_header *header2;
-
- key = v1;
- header2 = v2;
- return strcmp(key, header2->key);
-}
-
-/**
- * Determine if the header key has already been defined in this context.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] key Header key value to search for.
- * @retval 1 If the header already exists in this context.
- * @retval 0 If the header does not exist in this context.
- */
-static int
-smtp_header_exists(const struct smtp *const smtp,
- const char *const key){
- if(bsearch(key,
- smtp->header_list,
- smtp->num_headers,
- sizeof(*smtp->address_list),
- smtp_header_cmp_key) == NULL){
- return 0;
- }
- return 1;
-}
-
-/**
* Validate characters in the email header key.
*
* Must consist only of printable US-ASCII characters except colon.
@@ -3113,7 +3148,7 @@ smtp_mail(struct smtp *const smtp,
}
}
- if(smtp_print_mime_email(smtp, body) != SMTP_STATUS_OK){
+ if(smtp_print_email(smtp, body)){
return smtp->status_code;
}
diff --git a/src/smtp.h b/src/smtp.h
index 8d606f1..cd59b9b 100644
--- a/src/smtp.h
+++ b/src/smtp.h
@@ -270,6 +270,13 @@ smtp_auth(struct smtp *const smtp,
* The 'Date' header will automatically get generated here if it hasn't
* already been set using @ref smtp_header_add.
*
+ * If the application overrides the default 'Content-Type' header, then
+ * this function will output the @p body as raw data just below the email
+ * headers, and it will not output the attachments added using the
+ * smtp_attachment_add_* functions. In other words, the application must
+ * create its own MIME sections (if needed) when overriding the
+ * 'Content-Type' header.
+ *
* @param[in] smtp SMTP client context.
* @param[in] body Null-terminated string to send in the email body.
* @return See @ref smtp_status_code.
@@ -341,6 +348,8 @@ smtp_status_code_errstr(enum smtp_status_code status_code);
* If adding a header with an existing key, this will insert instead of
* replacing the existing header. See @ref smtp_header_clear_all.
*
+ * See @ref smtp_mail when overriding the default 'Content-Type' header.
+ *
* @param[in] smtp SMTP client context.
* @param[in] key Key name for new header. It must consist only of
* printable US-ASCII characters except colon.
diff --git a/test/example_html.c b/test/example_html.c
new file mode 100644
index 0000000..da6e69d
--- /dev/null
+++ b/test/example_html.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include "smtp.h"
+#define MAIL_SERVER "localhost"
+#define MAIL_PORT "25"
+#define MAIL_CONNECTION_SECURITY SMTP_SECURITY_NONE
+#define MAIL_FLAGS SMTP_DEBUG
+#define MAIL_CAFILE NULL
+#define MAIL_AUTH SMTP_AUTH_NONE
+#define MAIL_USER "mail@somnisoft.com"
+#define MAIL_PASS "password"
+#define MAIL_FROM "mail@somnisoft.com"
+#define MAIL_FROM_NAME "From Name"
+#define MAIL_SUBJECT "Subject Line"
+#define MAIL_TO "mail@somnisoft.com"
+#define MAIL_TO_NAME "To Name"
+int main(void){
+ struct smtp *smtp;
+ enum smtp_status_code rc;
+ const char *const email_body =
+ "<html>\n"
+ " <head><title>HTML Email</title></head>\n"
+ " <body>\n"
+ " <h1>H1</h1>\n"
+ " <h2>H2</h1>\n"
+ " <h3>H3</h1>\n"
+ " <h4>H4</h1>\n"
+ " <h5>H5</h1>\n"
+ " <h6>H6</h1>\n"
+ " </body>\n"
+ "</html>\n";
+ smtp_open(MAIL_SERVER,
+ MAIL_PORT,
+ MAIL_CONNECTION_SECURITY,
+ MAIL_FLAGS,
+ MAIL_CAFILE,
+ &smtp);
+ smtp_auth(smtp,
+ MAIL_AUTH,
+ MAIL_USER,
+ MAIL_PASS);
+ smtp_address_add(smtp,
+ SMTP_ADDRESS_FROM,
+ MAIL_FROM,
+ MAIL_FROM_NAME);
+ smtp_address_add(smtp,
+ SMTP_ADDRESS_TO,
+ MAIL_TO,
+ MAIL_TO_NAME);
+ smtp_header_add(smtp,
+ "Subject",
+ MAIL_SUBJECT);
+ smtp_header_add(smtp,
+ "Content-Type",
+ "text/html");
+ smtp_mail(smtp, email_body);
+ rc = smtp_close(smtp);
+ if(rc != SMTP_STATUS_OK){
+ fprintf(stderr, "smtp failed: %s\n", smtp_status_code_errstr(rc));
+ return 1;
+ }
+ return 0;
+}
diff --git a/test/example_simple.c b/test/example_simple.c
new file mode 100644
index 0000000..cf17f86
--- /dev/null
+++ b/test/example_simple.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include "smtp.h"
+#define MAIL_SERVER "mail.example.com"
+#define MAIL_PORT "25"
+#define MAIL_CONNECTION_SECURITY SMTP_SECURITY_STARTTLS
+#define MAIL_FLAGS (SMTP_DEBUG | \
+ SMTP_NO_CERT_VERIFY) /* Do not verify cert. */
+#define MAIL_CAFILE NULL
+#define MAIL_AUTH SMTP_AUTH_PLAIN
+#define MAIL_USER "mail@example.com"
+#define MAIL_PASS "password"
+#define MAIL_FROM "mail@example.com"
+#define MAIL_FROM_NAME "From Name"
+#define MAIL_SUBJECT "Subject Line"
+#define MAIL_BODY "Email Body"
+#define MAIL_TO "to@example.com"
+#define MAIL_TO_NAME "To Name"
+int main(void)
+{
+ struct smtp *smtp;
+ int rc;
+ rc = smtp_open(MAIL_SERVER,
+ MAIL_PORT,
+ MAIL_CONNECTION_SECURITY,
+ MAIL_FLAGS,
+ MAIL_CAFILE,
+ &smtp);
+ rc = smtp_auth(smtp,
+ MAIL_AUTH,
+ MAIL_USER,
+ MAIL_PASS);
+ rc = smtp_address_add(smtp,
+ SMTP_ADDRESS_FROM,
+ MAIL_FROM,
+ MAIL_FROM_NAME);
+ rc = smtp_address_add(smtp,
+ SMTP_ADDRESS_TO,
+ MAIL_TO,
+ MAIL_TO_NAME);
+ rc = smtp_header_add(smtp,
+ "Subject",
+ MAIL_SUBJECT);
+ rc = smtp_attachment_add_mem(smtp,
+ "test.txt",
+ "Test email attachment.",
+ -1);
+ rc = smtp_mail(smtp,
+ MAIL_BODY);
+ rc = smtp_close(smtp);
+ if(rc != SMTP_STATUS_OK){
+ fprintf(stderr, "smtp failed: %s\n", smtp_status_code_errstr(rc));
+ return 1;
+ }
+ return 0;
+}
diff --git a/test/test.c b/test/test.c
index 62423da..c580680 100644
--- a/test/test.c
+++ b/test/test.c
@@ -3303,6 +3303,169 @@ smtp_func_test_all_nodebug(struct smtp_test_config *const config){
}
/**
+ * Send an HTML email.
+ *
+ * @param[in] config SMTP server context.
+ */
+static void
+smtp_func_test_html(struct smtp_test_config *const config){
+ enum smtp_status_code rc;
+ const char *const html_body =
+ "<html>\n"
+ " <head><title>HTML Email Example</title></head>\n"
+ " <body>\n"
+ " <table style='border: 1px solid black; background-color: #a0a0a0'>\n"
+ " <caption>smtp_connection_security</caption>\n"
+ " <tr>\n"
+ " <th>Code</th>\n"
+ " <th>Description</th>\n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>SMTP_SECURITY_STARTTLS</td>\n"
+ " <td>Use STARTTLS</td>\n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>SMTP_SECURITY_TLS</td>\n"
+ " <td>Direct TLS connection</td>\n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>SMTP_SECURITY_NONE</td>\n"
+ " <td>No encryption</td>\n"
+ " </tr>\n"
+ " </table>\n"
+ " </body>\n"
+ "</html>";
+
+ rc = smtp_open(config->server,
+ config->port,
+ SMTP_TEST_DEFAULT_CONNECTION_SECURITY,
+ SMTP_TEST_DEFAULT_FLAGS,
+ SMTP_TEST_DEFAULT_CAFILE,
+ &config->smtp);
+ assert(rc == SMTP_STATUS_OK);
+
+ smtp_header_clear_all(config->smtp);
+
+ rc = smtp_header_add(config->smtp,
+ "Subject",
+ "SMTP Test: HTML Email (Content-Type: text/html)");
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_header_add(config->smtp,
+ "Content-Type",
+ "text/html");
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_address_add(config->smtp,
+ SMTP_ADDRESS_FROM,
+ config->email_from,
+ SMTP_TEST_DEFAULT_FROM_NAME);
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_address_add(config->smtp,
+ SMTP_ADDRESS_TO,
+ config->email_to,
+ SMTP_TEST_DEFAULT_TO_NAME);
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_mail(config->smtp, html_body);
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_close(config->smtp);
+ assert(rc == SMTP_STATUS_OK);
+}
+
+/**
+ * Send an HTML email with a plaintext fallback.
+ *
+ * @param[in] config SMTP server context.
+ */
+static void
+smtp_func_test_html_with_plaintext(struct smtp_test_config *const config){
+ enum smtp_status_code rc;
+ const char *const html_body =
+ "This is a multi-part message in MIME format.\r\n"
+ "...\n"
+ "..\n"
+ ".\n"
+ ".\r"
+ ".\n"
+ "\r\n"
+ "--FEDCBA\r\n"
+ "This is a multi-part message in MIME format.\r\n"
+ "--ABCDEF\r\n"
+ "Content-Type: text/plain; charset=\"utf-8\"\r\n"
+ "Content-Transfer-Encoding: 8bit\r\n"
+ "\r\n"
+ "Plaintext section.\r\n"
+ "\r\n"
+ "--ABCDEF\r\n"
+ "Content-Type: text/html; charset=\"utf-8\"\r\n"
+ "Content-Transfer-Encoding: 8bit\r\n"
+ "\r\n"
+ "<html>\n"
+ " <head><title>HTML/Plaintext Email</title></head>\n"
+ " <body>\n"
+ " <h1>HTML section</h1>\n"
+ " <h2>Heading 2</h2>\n"
+ " </body>\n"
+ "</html>\n"
+ "...\n"
+ "..\n"
+ ".\n"
+ ".";
+
+ rc = smtp_open(config->server,
+ config->port,
+ SMTP_TEST_DEFAULT_CONNECTION_SECURITY,
+ SMTP_TEST_DEFAULT_FLAGS,
+ SMTP_TEST_DEFAULT_CAFILE,
+ &config->smtp);
+ assert(rc == SMTP_STATUS_OK);
+
+ smtp_header_clear_all(config->smtp);
+
+ rc = smtp_header_add(config->smtp,
+ "Subject",
+ "SMTP Test: HTML Email (with plaintext)");
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_header_add(config->smtp,
+ "Content-Type",
+ "multipart/alternative; boundary=\"ABCDEF\"");
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_address_add(config->smtp,
+ SMTP_ADDRESS_FROM,
+ config->email_from,
+ SMTP_TEST_DEFAULT_FROM_NAME);
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_address_add(config->smtp,
+ SMTP_ADDRESS_TO,
+ config->email_to,
+ SMTP_TEST_DEFAULT_TO_NAME);
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_mail(config->smtp, html_body);
+ assert(rc == SMTP_STATUS_OK);
+
+ rc = smtp_close(config->smtp);
+ assert(rc == SMTP_STATUS_OK);
+}
+
+/**
+ * Send HTML emails, overriding the Content-Type.
+ *
+ * @param[in] config SMTP server context.
+ */
+static void
+smtp_func_test_all_html(struct smtp_test_config *const config){
+ smtp_func_test_html(config);
+ smtp_func_test_html_with_plaintext(config);
+}
+
+/**
* Test failure or error conditions not covered by any of the other failure
* tests.
*
@@ -4882,6 +5045,7 @@ smtp_func_test_postfix(void){
smtp_func_test_all_body(&config);
smtp_func_test_all_write(&config);
smtp_func_test_all_nodebug(&config);
+ smtp_func_test_all_html(&config);
}