Пример #1
0
bool SmtpClient::post()
{
    d_ptr->alloc();      
    
    smtp_set_header(d_ptr->message, "To", NULL, content.to.c_str());
    if (content.cc.empty() == false)
        smtp_set_header(d_ptr->message, "Cc", NULL, content.cc.c_str());
    if (content.bcc.empty() == false)
        smtp_set_header(d_ptr->message, "Bcc", NULL, content.bcc.c_str());

    smtp_set_reverse_path(d_ptr->message, content.from.c_str());
    
    smtp_set_header(d_ptr->message, "Subject", NULL, content.header.c_str());
    smtp_set_header_option(d_ptr->message, "Subject", Hdr_OVERRIDE, 1);
    
    if (!smtp_start_session(d_ptr->session))
    {
        trace_msg("failed to start smtp session.");
        
# ifdef NNT_DEBUG
        char buf[128];
        
        fprintf (stderr, "SMTP server problem: %s\n",
                 smtp_strerror (smtp_errno (), buf, sizeof buf)); 
# endif
        return false;
    }
    
    d_ptr->print_status();
    
    return true;
}
Пример #2
0
void EmailNotify::notify(Withdrawal *withdrawal)
{
	User *user;
	smtp_session_t smtp_session;
	smtp_message_t smtp_message;
	QString subject, txtState, To, ToBoss;
	QDate start, end;

	if (!configOk)
		return;

	user = withdrawal->getUser();

	switch (withdrawal->getState()) {
	case WDRL_REQUEST: txtState = tr("Request"); break;
	case WDRL_APPROVED: txtState = tr("Approved"); break;
	case WDRL_REJECTED: txtState = tr("Rejected"); break;
	case WDRL_CANCEL_REQUEST: txtState = tr("Cancellation Request"); break;
	case WDRL_CANCELLED: txtState = tr("Cancelled"); break;
	}
	start = QDate::fromJulianDay(withdrawal->getStart());
	end = QDate::fromJulianDay(withdrawal->getEnd());

	subject = tr("[Vacation - ") + user->getFirstName() + " " + user->getLastName() + "] ("
			+ txtState + ") " +
			QDate::shortMonthName(start.month()) + ' ' + QString::number(start.day()) + " - " +
			QDate::shortMonthName(end.month()) + ' ' + QString::number(end.day());

	To = user->getEmail();
	To.replace('#', '.');
	ToBoss = user->getBoss()->getEmail();
	ToBoss.replace('#', '.');

	smtp_session = smtp_create_session();
	smtp_message = smtp_add_message(smtp_session);

	smtp_set_header(smtp_message, "To", NULL, (const char *)To.toUtf8());

	smtp_set_server(smtp_session, server.toUtf8());

	smtp_set_reverse_path(smtp_message, reply.toUtf8());
	smtp_set_header(smtp_message, "Subject", (const char *)subject.toUtf8());
	smtp_set_header_option(smtp_message, "Subject", Hdr_OVERRIDE, 1);
	smtp_set_message_str(smtp_message, (void*)"Vacation Update");

	smtp_add_recipient(smtp_message, To.toUtf8());
	if (ToBoss.size())
		smtp_add_recipient(smtp_message, ToBoss.toUtf8());

	smtp_start_session(smtp_session);	// send messages
	smtp_destroy_session(smtp_session);
}
Пример #3
0
bool send(const std::string& from, const std::string& to, 
	std::vector<std::string>& recipients, 
	const std::string& subject, const std::string& data)
{
	BOOSTER_LOG(debug,__FUNCTION__);
	message_ = smtp_add_message(session_);
	/* Request MDN sent to the same address as the reverse path */
	//smtp_set_header(message, "Disposition-Notification-To", NULL, NULL);

	/* Set the reverse path for the mail envelope.  (NULL is ok) */
	smtp_set_reverse_path(message_, from.c_str());

#if 0
	/* The message-id is OPTIONAL but SHOULD be present.  By default
	libESMTP supplies one.  If this is not desirable, the following
	prevents one making its way to the server.
	N.B. It is not possible to prohibit REQUIRED headers.  Furthermore,
	the submission server will probably add a Message-ID header,
	so this cannot prevent the delivered message from containing
	the message-id.  */
	smtp_set_header_option(message_, "Message-Id", Hdr_PROHIBIT, 1);
#endif

	/* RFC 2822 doesn't require recipient headers but a To: header would
	be nice to have if not present. */
	if(!to.empty())
		smtp_set_header(message_, "To", NULL, NULL);
	else
		smtp_set_header(message_, "To", NULL, to.c_str());

	/* Set the Subject: header.  For no reason, we want the supplied subject
	to override any subject line in the message headers. */
	if (!subject.empty())
	{
		smtp_set_header(message_, "Subject", subject.c_str());
		smtp_set_header_option(message_, "Subject", Hdr_OVERRIDE, 1);
	}

	std::string msg = std::string("MIME-Version: 1.0\r\n") +
             "Content-Type: text/plain;\r\n" +
             "Content-Transfer-Encoding: 8bit\r\n" +
             "Subject: " + subject + "\r\n" +
             "\r\n" + 
             "Message: " + data;

	char s[ESMTP_MAX_STR];
	strcpy(s, msg.c_str());
	smtp_set_message_str(message_, s);
	/* Add remaining program arguments as message recipients. */
	std::vector<std::string>::const_iterator it;
	for(it = recipients.begin(); it != recipients.end(); ++it )
	{
		recipient_ = smtp_add_recipient(message_, it->c_str());

		/* Recipient options set here */
		if (notify_ != Notify_NOTSET)
			smtp_dsn_set_notify(recipient_, notify_);
	}
	/* Initiate a connection to the SMTP server and transfer the message. */
	if (!smtp_start_session(session_))
	{
		char buf[128];
		BOOSTER_LOG(error,__FUNCTION__) << "SMTP server problem: " << smtp_strerror(smtp_errno(), buf, sizeof buf);
		return false;
	}
	
	/* Report on the success or otherwise of the mail transfer. */
	status_ = smtp_message_transfer_status(message_);
	BOOSTER_LOG(debug,__FUNCTION__) << status_->code << ((status_->text != NULL) ? status_->text : "\n") << std::endl;
	smtp_enumerate_recipients (message_, print_recipient_status, NULL);
	return true;
}
Пример #4
0
int main()
{
	smtp_session_t session;
	smtp_message_t message;
	struct sigaction sa;
	const smtp_status_t *status;
	char buf[128];
	FILE *fp;
	/* This program sends only one message at a time. Create an SMTP
	 * session and add a message to it. */
	if( (session = smtp_create_session ()) == NULL){
		fprintf (stderr, "smtp_create_session problem %s\n",
				smtp_strerror (smtp_errno (), buf, sizeof buf));
		return 1;
	}
	if((message = smtp_add_message (session)) == NULL){
		fprintf (stderr, "smtp_add_message problem %s\n",
				smtp_strerror (smtp_errno (), buf, sizeof buf));
		return 1;
	}
	/* NB. libESMTP sets timeouts as it progresses through the protocol.
	 * In addition the remote server might close its socket on a timeout.
	 * Consequently libESMTP may sometimes try to write to a socket with
	 * no reader. Ignore SIGPIPE, then the program doesn't get killed
	 * if/when this happens. */
	sa.sa_handler = SIG_IGN;
	sigemptyset (&sa.sa_mask);
	sa.sa_flags = 0;
	sigaction (SIGPIPE, &sa, NULL); 
	/* Set the host running the SMTP server. LibESMTP has a default port
	 * number of 587, however this is not widely deployed so the port
	 * is specified as 25 along with the default MTA host. */
	smtp_set_server (session, "smtp.qq.com:25");
	/* Set the reverse path for the mail envelope. (NULL is ok)
	 */
	smtp_set_reverse_path (message, "*****@*****.**");
	/* RFC 2822 doesn't require recipient headers but a To: header would
	 * be nice to have if not present. */
	smtp_set_header (message, "To", NULL, NULL);
	smtp_set_header (message, "Subject", " test mail");
	smtp_set_header_option (message, "Subject", Hdr_OVERRIDE, 1);
	fprintf(stderr,"%s\n","smtp_set_server.");
	if ((fp = fopen ("./test-mail.eml", "r")) == NULL) {
		fprintf (stderr, "can't open mail file: %s\n", strerror (errno));
		return (1);
	}
	smtp_set_message_fp (message, fp);
	smtp_add_recipient (message,"test@localhost");
	/* Initiate a connection to the SMTP server and transfer the
	 * message. */
	if (!smtp_start_session (session)){
		fprintf (stderr, "SMTP server problem %s\n",
				smtp_strerror (smtp_errno (), buf, sizeof buf));
	}
	else{
		/* Report on the success or otherwise of the mail transfer.
		 */
		status = smtp_message_transfer_status (message);
		printf ("%d %s", status->code,
				(status->text != NULL) ? status->text : "\n");
	}
	/* Free resources consumed by the program.
	 */
	smtp_destroy_session (session);
	if(fp != NULL){
		fclose(fp);
	}

	return 0;
}
Пример #5
0
/*
 * Back-end function to actually send the email
 */
static int send_email(char *msg)
{
    int i, err = PMIX_SUCCESS;
    char *str = NULL;
    char *errmsg = NULL;
    struct sigaction sig, oldsig;
    bool set_oldsig = false;
    smtp_session_t session = NULL;
    smtp_message_t message = NULL;
    message_status_t ms;
    pmix_plog_smtp_component_t *c = &mca_plog_smtp_component;

    if (NULL == c->to_argv) {
        c->to_argv = pmix_argv_split(c->to, ',');
        if (NULL == c->to_argv ||
            NULL == c->to_argv[0]) {
            return PMIX_ERR_OUT_OF_RESOURCE;
        }
    }

    ms.sent_flag = SENT_NONE;
    ms.prev_string = NULL;
    ms.msg = msg;

    /* Temporarily disable SIGPIPE so that if remote servers timeout
       or hang up on us, it doesn't kill this application.  We'll
       restore the original SIGPIPE handler when we're done. */
    sig.sa_handler = SIG_IGN;
    sigemptyset(&sig.sa_mask);
    sig.sa_flags = 0;
    sigaction(SIGPIPE, &sig, &oldsig);
    set_oldsig = true;

    /* Try to get a libesmtp session.  If so, assume that libesmtp is
       happy and proceeed */
    session = smtp_create_session();
    if (NULL == session) {
        err = PMIX_ERR_NOT_SUPPORTED;
        errmsg = "stmp_create_session";
        goto error;
    }

    /* Create the message */
    message = smtp_add_message(session);
    if (NULL == message) {
        err = PMIX_ERROR;
        errmsg = "stmp_add_message";
        goto error;
    }

    /* Set the SMTP server (yes, it's a weird return status!) */
    asprintf(&str, "%s:%d", c->server, c->port);
    if (0 == smtp_set_server(session, str)) {
        err = PMIX_ERROR;
        errmsg = "stmp_set_server";
        goto error;
    }
    free(str);
    str = NULL;

    /* Add the sender */
    if (0 == smtp_set_reverse_path(message, c->from_addr)) {
        err = PMIX_ERROR;
        errmsg = "stmp_set_reverse_path";
        goto error;
    }

    /* Set the subject and some headers */
    asprintf(&str, "PMIx SMTP Plog v%d.%d.%d",
             c->super.base.pmix_mca_component_major_version,
             c->super.base.pmix_mca_component_minor_version,
             c->super.base.pmix_mca_component_release_version);
    if (0 == smtp_set_header(message, "Subject", c->subject) ||
        0 == smtp_set_header_option(message, "Subject", Hdr_OVERRIDE, 1) ||
        0 == smtp_set_header(message, "To", NULL, NULL) ||
        0 == smtp_set_header(message, "From",
                             (NULL != c->from_name ?
                              c->from_name : c->from_addr),
                             c->from_addr) ||
        0 == smtp_set_header(message, "X-Mailer", str) ||
        0 == smtp_set_header_option(message, "Subject", Hdr_OVERRIDE, 1)) {
        err = PMIX_ERROR;
        errmsg = "smtp_set_header";
        goto error;
    }
    free(str);
    str = NULL;

    /* Add the recipients */
    for (i = 0; NULL != c->to_argv[i]; ++i) {
        if (NULL == smtp_add_recipient(message, c->to_argv[i])) {
            err = PMIX_ERR_OUT_OF_RESOURCE;
            errmsg = "stmp_add_recipient";
            goto error;
        }
    }

    /* Set the callback to get the message */
    if (0 == smtp_set_messagecb(message, message_cb, &ms)) {
        err = PMIX_ERROR;
        errmsg = "smtp_set_messagecb";
        goto error;
    }

    /* Send it! */
    if (0 == smtp_start_session(session)) {
        err = PMIX_ERROR;
        errmsg = "smtp_start_session";
        goto error;
    }

    /* Fall through */

 error:
    if (NULL != str) {
        free(str);
    }
    if (NULL != session) {
        smtp_destroy_session(session);
    }
    /* Restore the SIGPIPE handler */
    if (set_oldsig) {
        sigaction(SIGPIPE, &oldsig, NULL);
    }
    if (PMIX_SUCCESS != err) {
        int e;
        char em[256];

        e = smtp_errno();
        smtp_strerror(e, em, sizeof(em));
        pmix_show_help("help-pmix-plog-smtp.txt",
                       "send_email failed",
                       true, "libesmtp library call failed",
                       errmsg, em, e, msg);
    }
    return err;
}