/** Same as smtp_send_mail, but doesn't copy from, to, subject and body into * an internal buffer to save memory. * WARNING: the above data must stay untouched until the callback function is * called (unless the function returns != ERR_OK) */ err_t smtp_send_mail_static(const char *from, const char* to, const char* subject, const char* body, smtp_result_fn callback_fn, void* callback_arg) { struct smtp_session* s; size_t len; s = (struct smtp_session*)SMTP_STATE_MALLOC(sizeof(struct smtp_session)); if (s == NULL) { return ERR_MEM; } memset(s, 0, sizeof(struct smtp_session)); /* initialize the structure */ s->from = from; len = strlen(from); LWIP_ASSERT("string is too long", len <= 0xffff); s->from_len = (u16_t)len; s->to = to; len = strlen(to); LWIP_ASSERT("string is too long", len <= 0xffff); s->to_len = (u16_t)len; s->subject = subject; len = strlen(subject); LWIP_ASSERT("string is too long", len <= 0xffff); s->subject_len = (u16_t)len; s->body = body; len = strlen(body); LWIP_ASSERT("string is too long", len <= 0xffff); s->body_len = (u16_t)len; s->callback_fn = callback_fn; s->callback_arg = callback_arg; /* call the actual implementation of this function */ return smtp_send_mail_alloced(s); }
/** @ingroup smtp * Send an email via the currently selected server, username and password. * * @param from source email address (must be NULL-terminated) * @param to target email address (must be NULL-terminated) * @param subject email subject (must be NULL-terminated) * @param body email body (must be NULL-terminated) * @param callback_fn callback function * @param callback_arg user argument to callback_fn * @returns - ERR_OK if structures were allocated and no error occured starting the connection * (this does not mean the email has been successfully sent!) * - another err_t on error. */ err_t smtp_send_mail(const char* from, const char* to, const char* subject, const char* body, smtp_result_fn callback_fn, void* callback_arg) { struct smtp_session* s; size_t from_len = strlen(from); size_t to_len = strlen(to); size_t subject_len = strlen(subject); size_t body_len = strlen(body); size_t mem_len = sizeof(struct smtp_session); char *sfrom, *sto, *ssubject, *sbody; LWIP_ASSERT_CORE_LOCKED(); mem_len += from_len + to_len + subject_len + body_len + 4; if (mem_len > 0xffff) { /* too long! */ return ERR_MEM; } /* Allocate memory to keep this email's session state */ s = (struct smtp_session *)SMTP_STATE_MALLOC((mem_size_t)mem_len); if (s == NULL) { return ERR_MEM; } /* initialize the structure */ memset(s, 0, mem_len); s->from = sfrom = (char*)s + sizeof(struct smtp_session); s->from_len = (u16_t)from_len; s->to = sto = sfrom + from_len + 1; s->to_len = (u16_t)to_len; s->subject = ssubject = sto + to_len + 1; s->subject_len = (u16_t)subject_len; s->body = sbody = ssubject + subject_len + 1; s->body_len = (u16_t)body_len; /* copy source and target email address */ /* cast to size_t is a hack to cast away constness */ MEMCPY(sfrom, from, from_len + 1); MEMCPY(sto, to, to_len + 1); MEMCPY(ssubject, subject, subject_len + 1); MEMCPY(sbody, body, body_len + 1); s->callback_fn = callback_fn; s->callback_arg = callback_arg; /* call the actual implementation of this function */ return smtp_send_mail_alloced(s); }
/** Same as smtp_send_mail_static, but uses a callback function to send body data */ err_t smtp_send_mail_bodycback(const char *from, const char* to, const char* subject, smtp_bodycback_fn bodycback_fn, smtp_result_fn callback_fn, void* callback_arg) { struct smtp_session* s; size_t len; LWIP_ASSERT_CORE_LOCKED(); s = (struct smtp_session*)SMTP_STATE_MALLOC(sizeof(struct smtp_session)); if (s == NULL) { return ERR_MEM; } memset(s, 0, sizeof(struct smtp_session)); s->bodydh = (struct smtp_bodydh_state*)SMTP_BODYDH_MALLOC(sizeof(struct smtp_bodydh_state)); if (s->bodydh == NULL) { SMTP_STATE_FREE(s); return ERR_MEM; } memset(s->bodydh, 0, sizeof(struct smtp_bodydh_state)); /* initialize the structure */ s->from = from; len = strlen(from); LWIP_ASSERT("string is too long", len <= 0xffff); s->from_len = (u16_t)len; s->to = to; len = strlen(to); LWIP_ASSERT("string is too long", len <= 0xffff); s->to_len = (u16_t)len; s->subject = subject; len = strlen(subject); LWIP_ASSERT("string is too long", len <= 0xffff); s->subject_len = (u16_t)len; s->body = NULL; LWIP_ASSERT("string is too long", len <= 0xffff); s->callback_fn = callback_fn; s->callback_arg = callback_arg; s->bodydh->callback_fn = bodycback_fn; s->bodydh->state = BDH_SENDING; /* call the actual implementation of this function */ return smtp_send_mail_alloced(s); }