gpt4 book ai didi

c++ - C/C++ 程序无法在 Raspberry Pi 上正常运行

转载 作者:行者123 更新时间:2023-11-30 02:44:29 26 4
gpt4 key购买 nike


我编写了程序,通过我的 gmail 地址发送 5 封电子邮件,中间有 1 分钟的暂停时间。该程序适用于我的笔记本电脑,但不适用于 Raspberry Pi。在 Raspberry Pi 上它只发送一封电子邮件。

主要.cpp:

#include <string>
#include <thread>
#include <chrono>

#include "email.c"

void threadFunction()
{
for (int i = 0; i < 5; i++)
{
std::string message = /*"Hello my name is Dmitry\r\nTest1\r\nTest2\r\n" + */std::to_string(i) + "\r\n";
int status = sent_email("*********", "*********", "Test message", message.c_str(), "smtps://smtp.gmail.com", "*********", "*********");
std::this_thread::sleep_for(std::chrono::minutes(1));
status++;
}
}

int main()
{
/*std::thread thr(threadFunction);
thr.join();*/
threadFunction();
return 0;
}

电子邮件.c:

#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct upload_information {
const char *data;
size_t data_length;
size_t sent;
};

size_t read_callback(char *buffer, size_t size, size_t nitems, void *instream);

int sent_email(const char *FROM, const char *TO, const char *SUBJECT, const char *message, const char *server, const char *login, const char *password)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;

size_t length = strlen(message) + strlen(FROM) + strlen(TO) + strlen(SUBJECT) + 32; //32 is FROM: <>/n/r, TO: <>/n/r length and SUBJECT + new line(after header - SMTP standart) + /0

char *data;
data = (char*)malloc(sizeof(char) * length);

strcat(data, "FROM: <");
strcat(data, FROM);
strcat(data, ">\r\n");
strcat(data, "TO: <");
strcat(data, TO);
strcat(data, ">\r\n");
strcat(data, "SUBJECT: ");
strcat(data, SUBJECT);
strcat(data, "\r\n\r\n");
strcat(data, message);

struct upload_information upload_info;
upload_info.data = data;
upload_info.data_length = length;
upload_info.sent = 0;

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_USERNAME, login);
curl_easy_setopt(curl, CURLOPT_PASSWORD, password);

curl_easy_setopt(curl, CURLOPT_URL, server);

curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
recipients = curl_slist_append(recipients, TO);
//recipients = curl_slist_append(recipients, CC);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_info);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

res = curl_easy_perform(curl);

if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}

free(data);
return (int)res;
}

size_t read_callback(char *buffer, size_t size, size_t nitems, void *instream)
{
struct upload_information *upload_info = (struct upload_information *)instream;

if ((size == 0) || (nitems == 0) || (size * nitems < 1))
return 0;

if (upload_info->sent < upload_info->data_length)
{
size_t length = size * nitems;

if (length > upload_info->data_length - upload_info->sent)
length = upload_info->data_length - upload_info->sent;

memcpy(buffer, upload_info->data + upload_info->sent, length);

upload_info->sent += length;

return length;
}

return 0;
}

树莓派上的错误信息:
*** 检测到 glibc *** ./Mailer:双重释放或损坏 (!prev):0x01d0da38 ***

附言树莓派上的程序发送了 1 条正确的消息和第二条消息的一部分。
P.S.S 帖子已更新

最佳答案

您为消息分配的缓冲区不足 1 个字节。您添加的所有常量字符串的总长度为 29,但您还需要为 '\0' 字符添加一个字节,strcat 将终止字符串。

如果运行以下代码,它会显示所有字符串文字的连接长度为 30:

#include <stdio.h>

int
main() {
printf("%d\n", (int) sizeof("FROM: <" ">\r\n"
"TO: <" ">\r\n"
"SUBJECT: " "\r\n"));
}

消息还有另外两个问题。一是您没有按要求在 header 和消息之间放置一个空行。另一个是您没有使用 \r\n 终止消息的最后一行。这可能会混淆 curl 库,因为它需要自己添加行尾终止符。否则,邮件将无法通过 SMTP 正确发送。

关于c++ - C/C++ 程序无法在 Raspberry Pi 上正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25263989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com