|
|
|
@ -4,7 +4,7 @@
|
|
|
|
#include <sstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iomanip>
|
|
|
|
#include<stack>
|
|
|
|
#include<stack>
|
|
|
|
//#include <curl/curl.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace exam_system {
|
|
|
|
namespace exam_system {
|
|
|
|
|
|
|
|
|
|
|
|
@ -33,15 +33,15 @@ BackendImpl::BackendImpl() {
|
|
|
|
email_config_.password = "your_authorization_code"; // 注意:这是授权码,不是登录密码
|
|
|
|
email_config_.password = "your_authorization_code"; // 注意:这是授权码,不是登录密码
|
|
|
|
email_config_.use_curl = true;
|
|
|
|
email_config_.use_curl = true;
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化libcurl
|
|
|
|
初始化libcurl
|
|
|
|
// curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
|
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
|
|
|
|
|
|
|
|
|
|
Logger::Log(Logger::Level::INFO, "BackendImpl初始化完成");
|
|
|
|
Logger::Log(Logger::Level::INFO, "BackendImpl初始化完成");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BackendImpl::~BackendImpl() {
|
|
|
|
BackendImpl::~BackendImpl() {
|
|
|
|
// // curl_global_cleanup();
|
|
|
|
curl_global_cleanup();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BackendImpl::SendVerificationCode(const std::string& email) {
|
|
|
|
bool BackendImpl::SendVerificationCode(const std::string& email) {
|
|
|
|
if (email.empty() || email.find('@') == std::string::npos) {
|
|
|
|
if (email.empty() || email.find('@') == std::string::npos) {
|
|
|
|
@ -339,82 +339,82 @@ bool BackendImpl::SendEmail(const std::string& recipient,
|
|
|
|
bool BackendImpl::SendEmailViaCurl(const std::string& recipient,
|
|
|
|
bool BackendImpl::SendEmailViaCurl(const std::string& recipient,
|
|
|
|
const std::string& subject,
|
|
|
|
const std::string& subject,
|
|
|
|
const std::string& body) {
|
|
|
|
const std::string& body) {
|
|
|
|
// CURL* curl;
|
|
|
|
CURL* curl;
|
|
|
|
// CURLcode res = CURLE_OK;
|
|
|
|
CURLcode res = CURLE_OK;
|
|
|
|
// struct curl_slist* recipients = nullptr;
|
|
|
|
struct curl_slist* recipients = nullptr;
|
|
|
|
// std::string response_string;
|
|
|
|
std::string response_string;
|
|
|
|
|
|
|
|
|
|
|
|
// curl = curl_easy_init();
|
|
|
|
curl = curl_easy_init();
|
|
|
|
// if (!curl) {
|
|
|
|
if (!curl) {
|
|
|
|
// Logger::Log(Logger::Level::ERROR, "libcurl初始化失败");
|
|
|
|
Logger::Log(Logger::Level::ERROR, "libcurl初始化失败");
|
|
|
|
// return false;
|
|
|
|
return false;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// // 设置SMTP服务器
|
|
|
|
// 设置SMTP服务器
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_URL, ("smtp://" + email_config_.smtp_server + ":" + std::to_string(email_config_.smtp_port)).c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, ("smtp://" + email_config_.smtp_server + ":" + std::to_string(email_config_.smtp_port)).c_str());
|
|
|
|
|
|
|
|
|
|
|
|
// // 设置用户名和密码
|
|
|
|
// 设置用户名和密码
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_USERNAME, email_config_.username.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERNAME, email_config_.username.c_str());
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_PASSWORD, email_config_.password.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PASSWORD, email_config_.password.c_str());
|
|
|
|
|
|
|
|
|
|
|
|
// // 设置发件人和收件人
|
|
|
|
// 设置发件人和收件人
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_MAIL_FROM, email_config_.username.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, email_config_.username.c_str());
|
|
|
|
// recipients = curl_slist_append(recipients, recipient.c_str());
|
|
|
|
recipients = curl_slist_append(recipients, recipient.c_str());
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
|
|
|
|
|
|
|
|
|
|
|
|
// // 设置邮件内容
|
|
|
|
// 设置邮件内容
|
|
|
|
// std::string email_data =
|
|
|
|
std::string email_data =
|
|
|
|
// "To: " + recipient + "\r\n"
|
|
|
|
"To: " + recipient + "\r\n"
|
|
|
|
// "From: " + email_config_.username + "\r\n"
|
|
|
|
"From: " + email_config_.username + "\r\n"
|
|
|
|
// "Subject: " + subject + "\r\n"
|
|
|
|
"Subject: " + subject + "\r\n"
|
|
|
|
// "\r\n" + body + "\r\n";
|
|
|
|
"\r\n" + body + "\r\n";
|
|
|
|
|
|
|
|
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_READFUNCTION, [](char* buffer, size_t size, size_t nitems, void* instream) -> size_t {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, [](char* buffer, size_t size, size_t nitems, void* instream) -> size_t {
|
|
|
|
// std::string* email_data = static_cast<std::string*>(instream);
|
|
|
|
std::string* email_data = static_cast<std::string*>(instream);
|
|
|
|
// size_t buffer_size = size * nitems;
|
|
|
|
size_t buffer_size = size * nitems;
|
|
|
|
|
|
|
|
|
|
|
|
// if (email_data->empty()) {
|
|
|
|
if (email_data->empty()) {
|
|
|
|
// return 0;
|
|
|
|
return 0;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// size_t copy_size = std::min(buffer_size, email_data->size());
|
|
|
|
size_t copy_size = std::min(buffer_size, email_data->size());
|
|
|
|
// memcpy(buffer, email_data->c_str(), copy_size);
|
|
|
|
memcpy(buffer, email_data->c_str(), copy_size);
|
|
|
|
// email_data->erase(0, copy_size);
|
|
|
|
email_data->erase(0, copy_size);
|
|
|
|
|
|
|
|
|
|
|
|
// return copy_size;
|
|
|
|
return copy_size;
|
|
|
|
// });
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_READDATA, &email_data);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READDATA, &email_data);
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
|
|
|
|
|
|
|
|
|
|
|
|
// // 启用TLS
|
|
|
|
// 启用TLS
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
|
|
|
|
|
|
|
|
|
|
|
|
// // 设置服务器证书验证(生产环境应该设为1)
|
|
|
|
// 设置服务器证书验证(生产环境应该设为1)
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
|
|
|
|
|
|
|
|
|
|
|
// // 设置响应回调
|
|
|
|
// 设置响应回调
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
|
|
|
|
|
|
|
|
|
|
|
|
// // 设置超时
|
|
|
|
// 设置超时
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
|
|
|
|
|
|
|
|
|
|
|
|
// // 发送邮件
|
|
|
|
// 发送邮件
|
|
|
|
// res = curl_easy_perform(curl);
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
|
|
|
|
|
|
|
// // 清理
|
|
|
|
// 清理
|
|
|
|
// curl_slist_free_all(recipients);
|
|
|
|
curl_slist_free_all(recipients);
|
|
|
|
// curl_easy_cleanup(curl);
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
|
|
|
|
// if (res != CURLE_OK) {
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
// Logger::Log(Logger::Level::ERROR,
|
|
|
|
Logger::Log(Logger::Level::ERROR,
|
|
|
|
// "邮件发送失败: " + std::string(curl_easy_strerror(res)));
|
|
|
|
"邮件发送失败: " + std::string(curl_easy_strerror(res)));
|
|
|
|
// return false;
|
|
|
|
return false;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Logger::Log(Logger::Level::INFO, "邮件发送成功: " + recipient);
|
|
|
|
Logger::Log(Logger::Level::INFO, "邮件发送成功: " + recipient);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|