0

我正在为 smtp 使用以下代码:

当它调用sendEmail时,输出将显示在终端中。如何将其发送到 dev/null 或不输出?在后台运行没有帮助。

部分输出:

* About to connect() to smtp.gmail.com port 587 (#0)
*   Trying 173.194.79.109... * connected
< 220 mx.google.com ESMTP nv6sm10641402pbc.42
> EHLO ubuntu
< 250-mx.google.com at your service, [222.164.82.97]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-STARTTLS
< 250 ENHANCEDSTATUSCODES
> STARTTLS
< 220 2.0.0 Ready to start TLS
* successfully set certificate verify locations:
*   CAfile: none

代码:

#include <stdio.h>
#include <string>
#include <cstring>
#include <curl/curl.h>
using namespace std;
/* This is a simple example showing how to send mail using libcurl's SMTP
 * capabilities. It builds on the simplesmtp.c example, adding some
 * authentication and transport security.
 */

#define FROM    "<asktheprogramer2@gmail.com>"

#define CC      "<asktheprogramer2@gmail.com>"

string ToAddress,strSubject, strMessage;

struct upload_status {
  int lines_read;
};

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
const char *payload_text[]={ToAddress.c_str(),"From: " FROM "\n","Cc: " CC "\n",strSubject.c_str(),"\n", /* empty line to divide headers from body, see RFC5322 */
  strMessage.c_str(),NULL};

  struct upload_status *upload_ctx = (struct upload_status *)userp;
  const char *data;

  if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
    return 0;
  }

  data = payload_text[upload_ctx->lines_read];

  if (data) {
    size_t len = strlen(data);
    memcpy(ptr, data, len);
    upload_ctx->lines_read ++;
    return len;
  }
  return 0;
}

void sendMail(string strRecipent, string strAubject, string strBessage){

    ToAddress="To: ";
    ToAddress+=strRecipent;
    ToAddress+="\n";


    strSubject="Subject: ";
    strSubject+=strAubject;
    strSubject+="\n";
    strMessage=strBessage;

CURL *curl;
  CURLcode res;
  struct curl_slist *recipients = NULL;
  struct upload_status upload_ctx;

  upload_ctx.lines_read = 0;

  curl = curl_easy_init();
  if (curl) {
    /* This is the URL for your mailserver. Note the use of port 587 here,
     * instead of the normal SMTP port (25). Port 587 is commonly used for
     * secure mail submission (see RFC4403), but you should use whatever
     * matches your server configuration. */
    curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:500");

    /* In this example, we'll start with a plain text connection, and upgrade
     * to Transport Layer Security (TLS) using the STARTTLS command. Be careful
     * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
     * will continue anyway - see the security discussion in the libcurl
     * tutorial for more details. */
    curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);

    /* If your server doesn't have a valid certificate, then you can disable
     * part of the Transport Layer Security protection by setting the
     * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
     *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
     *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
     * That is, in general, a bad idea. It is still better than sending your
     * authentication details in plain text though.
     * Instead, you should get the issuer certificate (or the host certificate
     * if the certificate is self-signed) and add it to the set of certificates
     * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
     * docs/SSLCERTS for more information.
     */
    //curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");

    /* A common reason for requiring transport security is to protect
     * authentication details (user names and passwords) from being "snooped"
     * on the network. Here is how the user name and password are provided: */
    curl_easy_setopt(curl, CURLOPT_USERNAME, "sim.dssprojects@gmail.com");
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "SIMpassword");

    /* value for envelope reverse-path */
    curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
    /* Add two recipients, in this particular case they correspond to the
     * To: and Cc: addressees in the header, but they could be any kind of
     * recipient. */
    recipients = curl_slist_append(recipients, strRecipent.c_str());
    recipients = curl_slist_append(recipients, CC);
    curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);

    /* In this case, we're using a callback function to specify the data. You
     * could just use the CURLOPT_READDATA option to specify a FILE pointer to
     * read from.
     */
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
    curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);

    /* Since the traffic will be encrypted, it is very useful to turn on debug
     * information within libcurl to see what is happening during the transfer.
     */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    /* send the message (including headers) */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* free the list of recipients and clean up */
    curl_slist_free_all(recipients);
    curl_easy_cleanup(curl);
  }
}
int main(void)
{

int pid = fork();

 if (pid == 0)
 {
    for(int i=0;i<2;i++)
    {
     sleep(10);
     sendMail("<asktheprogramer@gmail.com>","ABE Reminder","You have an appt in 15 mins");
    }
  return 0;
 }
 else
 {
   sleep(30);
 }

return 0;
}
4

3 回答 3

1

您可以在运行程序时重定向程序的输出。像这样(假设 *nix 环境):

./<yourprogram> > /dev/null

为标准输出。

./<yourprogram> &> /dev/null

用于标准和错误输出。

于 2012-08-20T08:39:41.600 回答
1

一种可能是重定向标准输出和标准错误。我不确定是否有通过 C++ 流实现此功能的等效机制,但您可以使用freopen()

freopen("stdout.txt", "w", stdout);
freopen("stderr.txt", "w", stderr);
于 2012-08-20T08:34:20.803 回答
0

从代码中删除或注释掉这一行:

  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
于 2013-12-14T05:29:50.883 回答