int main() { int rc, fd; char buffie[512] ; fd = smtp_open(NULL, WANTED_SMTP_PORT, NULL); if (fd < 0) goto kut; set_envelope(fd, "nngs@localhost", "nngs@localhost"); rc = set_data(fd); if (rc < 0) goto kut; add_header(fd, "Subject", "Kuttje (met twee thee)"); add_header(fd, "Reply-To", "nngs@localhost"); add_header(fd, NULL, 0); add_data(fd, "En een droge sherry, alstublieft!\n", 0); while(fgets(buffie, sizeof buffie, stdin)) { rc = add_data(fd, buffie, 0); /* sleep(1); */ if (rc < 0) break; } add_data(fd, NULL, 0); sleep(10); close(fd); kut: exit(0); }
static int smtp_mail(FILE *fp, char *to, char *subj) { int rc, fd; char buffie[1024] ; fd = smtp_open(conffile.smtp_mta, conffile.smtp_portnum, conffile.smtp_helo); if (fd < 0) { return child_perror("Smtp_open"); } rc = set_envelope(fd, conffile.smtp_from, to); if (rc < 0) { return child_perror("Smtp_envelope"); } rc = set_data(fd); if (rc < 0) { return child_perror("Smtp_data"); } if (subj) add_header(fd, "Subject", subj); if (conffile.smtp_reply_to) add_header(fd, "Reply-To", conffile.smtp_reply_to); add_header(fd, "X-NNGS-SMTP", "Yes, Baby!" ); add_header(fd, NULL, 0); while(fgets(buffie, sizeof buffie, fp)) { rc = add_data(fd, buffie, 0); if (rc < 0) { return child_perror("Smtp_loop"); } } add_data(fd, NULL, 0); close(fd); return 0; }
int main(int argc, char **argv, char **envp) { struct in_addr addr; void *handle; if (argc != 2) { show_usage(argv[0], 1); } printf("sendmail: To: %s\n", argv[1]); printf("sendmail: From: %s\n", g_sender); printf("sendmail: Subject: %s\n", g_subject); printf("sendmail: Body: %s\n", g_msg_body); net_ipaddr(addr.s_addr, 127, 0, 0, 1); handle = smtp_open(); if (handle) { smtp_configure(handle, g_host_name, &addr.s_addr); smtp_send(handle, argv[1], NULL, g_sender, g_subject, g_msg_body, strlen(g_msg_body)); smtp_close(handle); } return 0; }
int sendmail_main(int argc, char *argv[]) { struct in_addr addr; #if defined(CONFIG_EXAMPLES_SENDMAIL_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif void *handle; printf("sendmail: To: %s\n", g_recipient); printf("sendmail: From: %s\n", g_sender); printf("sendmail: Subject: %s\n", g_subject); printf("sendmail: Body: %s\n", g_msg_body); /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_SENDMAIL_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; uip_setmacaddr("eth0", mac); #endif /* Set up our host address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_SENDMAIL_IPADDR); uip_sethostaddr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_SENDMAIL_DRIPADDR); uip_setdraddr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_SENDMAIL_NETMASK); uip_setnetmask("eth0", &addr); /* Then send the mail */ uip_ipaddr(addr.s_addr, 127, 0, 0, 1); handle = smtp_open(); if (handle) { smtp_configure(handle, g_host_name, &addr.s_addr); smtp_send(handle, g_recipient, NULL, g_sender, g_subject, g_msg_body, strlen(g_msg_body)); smtp_close(handle); } return 0; }
bool smtp_client::open() { if (stream_.opened()) { acl_assert(client_ != NULL); acl_assert(client_->conn == stream_.get_vstream()); reuse_ = true; return true; } reuse_ = false; client_ = smtp_open(addr_, conn_timeout_, rw_timeout_, 1024); if (client_ == NULL) { logger_error("connect %s error: %s", addr_, last_serror()); return false; } // 打开流对象,只所以使用 stream_ 主要为了使用 SSL 通信 stream_.open(client_->conn); // 如果设置了 SSL 通信方式,则需要打开 SSL 通信接口 if (ssl_conf_) { polarssl_io* ssl = new polarssl_io(*ssl_conf_, false); if (stream_.setup_hook(ssl) == ssl) { logger_error("open ssl client error!"); ssl->destroy(); return false; } } return true; }
int return_mail(FILE *fp, email_hdr_t *eh, int flags, char *errstr) { int i; int len; FILE *nfp; char *p; char *incl; char *bincl; char *subj; char **sig; char **body; char *copy_addr; char *rtn_addr; char *tret; char *echo; char buf[CDDBBUFSIZ]; char subj_buf[CDDBBUFSIZ]; struct stat sbuf; int charset = -1; /* Create a temporary file for the mail. */ tret = cddbd_mktemp(); if(flags & MF_FAIL) { strncpy(buf, errstr, sizeof(buf)); buf[sizeof(buf) - 1] = '\0'; if((p = (char *)strchr(buf, '\n')) != NULL) *p = '\0'; } /* Return address is currently always the admin. */ rtn_addr = admin_email; switch(eh->eh_class) { case EC_COMMAND: /* Command response. */ if(flags & MF_FAIL) { cddbd_log(LOG_ERR | LOG_MAIL, "Email command failed: %s", buf); cddbd_snprintf(subj_buf, sizeof(subj_buf), cmd_rej_subj, eh->eh_serial); sig = gen_rej_sig; body = gen_rej_body; incl = gen_incl; bincl = gen_incl_blank; copy_addr = bounce_email; } else { cddbd_snprintf(subj_buf, sizeof(subj_buf), cmd_ok_subj, eh->eh_serial); sig = 0; body = 0; incl = null_incl; bincl = null_incl_blank; copy_addr = 0; } subj = subj_buf; break; case EC_SUBMIT: /* We only send mail for rejections and test submissions. */ if(flags & MF_FAIL) { if(flags & MF_TEST) { cddbd_log(LOG_ERR | LOG_MAIL, "Test email submission failed: %s", buf); subj = test_rej_subj; } else { cddbd_log(LOG_ERR | LOG_MAIL, "Email submission failed: %s", buf); subj = sub_rej_subj; } sig = gen_rej_sig; body = sub_rej_body; copy_addr = bounce_email; } else { sig = test_ok_sig; body = test_ok_body; subj = test_ok_subj; copy_addr = test_email; } incl = gen_incl; bincl = gen_incl_blank; charset = eh->eh_charset; break; default: /* subj = gen_rej_subj; body = gen_rej_body; sig = gen_rej_sig; incl = gen_incl; bincl = gen_incl_blank; copy_addr = bounce_email; break; */ /* Because of the increasing spam problem, we shouldn't be sending people all this mail which responds to spam sent to the freedb submission address. */ cddbd_freetemp(tret); return EE_OK; } if(eh->eh_flags & EH_ECHO) echo = eh->eh_echo; else echo = 0; if((nfp = fopen(tret, "w+")) == NULL) { cddbd_log(LOG_ERR | LOG_MAIL, "Can't create mail tmp file %s (%d)", tret, errno); return EE_ERROR; } /* Write out the note string. */ if(eh->eh_flags & EH_NOTE) { if(fprintf(nfp, note, eh->eh_note) == EOF) { cddbd_log(LOG_ERR | LOG_MAIL, "Can't write mail tmp file %s (%d)", tret, errno); fclose(nfp); cddbd_freetemp(tret); return EE_ERROR; } } /* Write out the err string. */ for(i = 0; body && body[i]; i++) { if(fprintf(nfp, body[i], errstr) == EOF) { cddbd_log(LOG_ERR | LOG_MAIL, "Can't write mail tmp file %s (%d)", tret, errno); fclose(nfp); cddbd_freetemp(tret); return EE_ERROR; } } rewind(fp); /* Write out the mail body. */ while(fgets(buf, sizeof(buf), fp) != NULL) { strip_crlf(buf); /* remove blank lines, skip nfp write */ if(is_blank(buf, 0)) p = bincl; else p = incl; /* zeke - remove the ## comment lines, exit - we are done */ if(is_DblHash(buf)) break; if(fprintf(nfp, p, buf) == EOF) { cddbd_log(LOG_ERR | LOG_MAIL, "Can't write mail tmp file %s (%d)", tret, errno); fclose(nfp); cddbd_freetemp(tret); return EE_ERROR; } } /*!zeke - correct comment follows ???? */ /* Write out the err string. */ for(i = 0; sig && sig[i]; i++) { if(fprintf(nfp, sig[i], admin_email) == EOF) { cddbd_log(LOG_ERR | LOG_MAIL, "Can't write mail tmp file %s (%d)", tret, errno); fclose(nfp); cddbd_freetemp(tret); return EE_ERROR; } } fflush(nfp); if(fstat(fileno(nfp), &sbuf)) len = 0; else len = sbuf.st_size; rewind(nfp); /* Figure out if we need to encode the mail. */ if(!(flags & (MF_ENC | MF_MULTI))) { while(fgets(buf, sizeof(buf), nfp) != NULL) { if(is_rfc_1521_mappable((unsigned char *)buf, 1, 0)) { flags |= MF_ENC; break; } } } rewind(nfp); if(!smtp_open()) { fclose(nfp); cddbd_freetemp(tret); return EE_ERROR; } if(!smtp_transmit(nfp, charset, subj, eh->eh_rcpt, eh->eh_to, rtn_addr, echo, flags, len, 0)) { fclose(nfp); cddbd_freetemp(tret); return EE_ERROR; } if(copy_addr != 0 && copy_addr[0] != '\0') { rewind(nfp); cddbd_snprintf(buf, sizeof(buf), "%s (fwd)", subj); if(!smtp_transmit(nfp, charset, buf, copy_addr, copy_addr, rtn_addr, echo, flags, len, 0)) { fclose(nfp); cddbd_freetemp(tret); return EE_ERROR; } } smtp_close(); fclose(nfp); cddbd_freetemp(tret); return EE_OK; }
/* ssmtp() -- send the message (exactly one) from stdin to the mailhub SMTP port */ int ssmtp(char *argv[]) { char b[(BUF_SZ + 2)], *buf = b+1, *p, *q; #ifdef MD5AUTH char challenge[(BUF_SZ + 1)]; #endif struct passwd *pw; int i, sock; uid_t uid; bool_t minus_v_save, leadingdot, linestart = True; int timeout = 0; int bufsize = sizeof(b)-1; b[0] = '.'; outbytes = 0; ht = &headers; uid = getuid(); if((pw = getpwuid(uid)) == (struct passwd *)NULL) { die("Could not find password entry for UID %d", uid); } get_arpadate(arpadate); if(read_config() == False) { log_event(LOG_INFO, "%s not found", config_file); } if((p = strtok(pw->pw_gecos, ";,"))) { if((gecos = strdup(p)) == (char *)NULL) { die("ssmtp() -- strdup() failed"); } } revaliases(pw); /* revaliases() may have defined this */ if(uad == (char *)NULL) { uad = append_domain(pw->pw_name); } rt = &rcpt_list; header_parse(stdin); #if 1 /* With FromLineOverride=YES set, try to recover sane MAIL FROM address */ uad = append_domain(uad); #endif from = from_format(uad, override_from); /* Now to the delivery of the message */ (void)signal(SIGALRM, (void(*)())handler); /* Catch SIGALRM */ (void)alarm((unsigned) MAXWAIT); /* Set initial timer */ if(setjmp(TimeoutJmpBuf) != 0) { /* Then the timer has gone off and we bail out */ die("Connection lost in middle of processing"); } if((sock = smtp_open(mailhost, port)) == -1) { die("Cannot open %s:%d", mailhost, port); } else if (use_starttls == False) /* no initial response after STARTTLS */ { if(smtp_okay(sock, buf) == False) die("Invalid response SMTP server"); } /* If user supplied username and password, then try ELHO */ if(auth_user) { outbytes += smtp_write(sock, "EHLO %s", hostname); } else { outbytes += smtp_write(sock, "HELO %s", hostname); } (void)alarm((unsigned) MEDWAIT); if(smtp_okay(sock, buf) == False) { die("%s (%s)", buf, hostname); } /* Try to log in if username was supplied */ if(auth_user) { #ifdef MD5AUTH if(auth_pass == (char *)NULL) { auth_pass = strdup(""); } if(auth_method && strcasecmp(auth_method, "cram-md5") == 0) { outbytes += smtp_write(sock, "AUTH CRAM-MD5"); (void)alarm((unsigned) MEDWAIT); if(smtp_read(sock, buf) != 3) { die("Server rejected AUTH CRAM-MD5 (%s)", buf); } strncpy(challenge, strchr(buf,' ') + 1, sizeof(challenge)); memset(buf, 0, bufsize); crammd5(challenge, auth_user, auth_pass, buf); } else { #endif memset(buf, 0, bufsize); to64frombits(buf, auth_user, strlen(auth_user)); if (use_oldauth) { outbytes += smtp_write(sock, "AUTH LOGIN %s", buf); } else { outbytes += smtp_write(sock, "AUTH LOGIN"); (void)alarm((unsigned) MEDWAIT); if(smtp_read(sock, buf) != 3) { die("Server didn't like our AUTH LOGIN (%s)", buf); } /* we assume server asked us for Username */ memset(buf, 0, bufsize); to64frombits(buf, auth_user, strlen(auth_user)); outbytes += smtp_write(sock, buf); } (void)alarm((unsigned) MEDWAIT); if(smtp_read(sock, buf) != 3) { die("Server didn't accept AUTH LOGIN (%s)", buf); } memset(buf, 0, bufsize); to64frombits(buf, auth_pass, strlen(auth_pass)); #ifdef MD5AUTH } #endif /* We do NOT want the password output to STDERR * even base64 encoded.*/ minus_v_save = minus_v; minus_v = False; outbytes += smtp_write(sock, "%s", buf); minus_v = minus_v_save; (void)alarm((unsigned) MEDWAIT); if(smtp_okay(sock, buf) == False) { die("Authorization failed (%s)", buf); } } /* Send "MAIL FROM:" line */ outbytes += smtp_write(sock, "MAIL FROM:<%s>", uad); (void)alarm((unsigned) MEDWAIT); if(smtp_okay(sock, buf) == 0) { die("%s", buf); } /* Send all the To: adresses */ /* Either we're using the -t option, or we're using the arguments */ if(minus_t) { if(rcpt_list.next == (rcpt_t *)NULL) { die("No recipients specified although -t option used"); } rt = &rcpt_list; while(rt->next) { p = rcpt_remap(rt->string); outbytes += smtp_write(sock, "RCPT TO:<%s>", p); (void)alarm((unsigned)MEDWAIT); if(smtp_okay(sock, buf) == 0) { die("RCPT TO:<%s> (%s)", p, buf); } rt = rt->next; } } else { for(i = 1; (argv[i] != NULL); i++) { p = strtok(argv[i], ","); while(p) { /* RFC822 Address -> "foo@bar" */ q = rcpt_remap(addr_parse(p)); outbytes += smtp_write(sock, "RCPT TO:<%s>", q); (void)alarm((unsigned) MEDWAIT); if(smtp_okay(sock, buf) == 0) { die("RCPT TO:<%s> (%s)", q, buf); } p = strtok(NULL, ","); } } } /* Send DATA */ outbytes += smtp_write(sock, "DATA"); (void)alarm((unsigned) MEDWAIT); if(smtp_read(sock, buf) != 3) { /* Oops, we were expecting "354 send your data" */ die("%s", buf); } outbytes += smtp_write(sock, "Received: by %s (sSMTP sendmail emulation); %s", hostname, arpadate); if(have_from == False) { outbytes += smtp_write(sock, "From: %s", from); } if(have_date == False) { outbytes += smtp_write(sock, "Date: %s", arpadate); } #ifdef HASTO_OPTION if(have_to == False) { outbytes += smtp_write(sock, "To: postmaster"); } #endif ht = &headers; while(ht->next) { outbytes += smtp_write(sock, "%s", ht->string); ht = ht->next; } (void)alarm((unsigned) MEDWAIT); /* End of headers, start body */ outbytes += smtp_write(sock, ""); /*prevent blocking on pipes, we really shouldnt be using stdio functions like fgets in the first place */ fcntl(STDIN_FILENO,F_SETFL,O_NONBLOCK); while(!feof(stdin)) { if (!fgets(buf, bufsize, stdin)) { /* if nothing was received, then no transmission * over smtp should be done */ sleep(1); /* don't hang forever when reading from stdin */ if (++timeout >= MEDWAIT) { log_event(LOG_ERR, "killed: timeout on stdin while reading body -- message saved to dead.letter."); die("Timeout on stdin while reading body"); } continue; } /* Trim off \n, double leading .'s */ leadingdot = standardise(buf, &linestart); if (linestart || feof(stdin)) { linestart = True; outbytes += smtp_write(sock, "%s", leadingdot ? b : buf); } else { if (log_level > 0) { log_event(LOG_INFO, "Sent a very long line in chunks"); } if (leadingdot) { outbytes += fd_puts(sock, b, sizeof(b)); } else { outbytes += fd_puts(sock, buf, bufsize); } } (void)alarm((unsigned) MEDWAIT); } if(!linestart) { smtp_write(sock, ""); } /* End of body */ outbytes += smtp_write(sock, "."); (void)alarm((unsigned) MAXWAIT); if(smtp_okay(sock, buf) == 0) { die("%s", buf); } /* Close connection */ (void)signal(SIGALRM, SIG_IGN); outbytes += smtp_write(sock, "QUIT"); (void)smtp_okay(sock, buf); (void)close(sock); log_event(LOG_INFO, "Sent mail for %s (%s) uid=%d username=%s outbytes=%d", from_strip(uad), buf, uid, pw->pw_name, outbytes); return(0); }
int mutt_smtp_send (const ADDRESS* from, const ADDRESS* to, const ADDRESS* cc, const ADDRESS* bcc, const char *msgfile, int eightbit) { CONNECTION *conn; ACCOUNT account; const char* envfrom; char buf[1024]; int ret = -1; /* it might be better to synthesize an envelope from from user and host * but this condition is most likely arrived at accidentally */ if (EnvFrom) envfrom = EnvFrom->mailbox; else if (from) envfrom = from->mailbox; else { mutt_error (_("No from address given")); return -1; } if (smtp_fill_account (&account) < 0) return ret; if (!(conn = mutt_conn_find (NULL, &account))) return -1; Esmtp = eightbit; do { /* send our greeting */ if (( ret = smtp_open (conn))) break; FREE (&AuthMechs); /* send the sender's address */ ret = snprintf (buf, sizeof (buf), "MAIL FROM:<%s>", envfrom); if (eightbit && mutt_bit_isset (Capabilities, EIGHTBITMIME)) { safe_strncat (buf, sizeof (buf), " BODY=8BITMIME", 15); ret += 14; } if (DsnReturn && mutt_bit_isset (Capabilities, DSN)) ret += snprintf (buf + ret, sizeof (buf) - ret, " RET=%s", DsnReturn); safe_strncat (buf, sizeof (buf), "\r\n", 3); if (mutt_socket_write (conn, buf) == -1) { ret = smtp_err_write; break; } if ((ret = smtp_get_resp (conn))) break; /* send the recipient list */ if ((ret = smtp_rcpt_to (conn, to)) || (ret = smtp_rcpt_to (conn, cc)) || (ret = smtp_rcpt_to (conn, bcc))) break; /* send the message data */ if ((ret = smtp_data (conn, msgfile))) break; mutt_socket_write (conn, "QUIT\r\n"); ret = 0; } while (0); if (conn) mutt_socket_close (conn); if (ret == smtp_err_read) mutt_error (_("SMTP session failed: read error")); else if (ret == smtp_err_write) mutt_error (_("SMTP session failed: write error")); else if (ret == smtp_err_code) mutt_error (_("Invalid server response")); return ret; }