Created
April 20, 2026 10:30
-
-
Save sunmeat/a4a24e8e42859fe4c909a75db2b58f51 to your computer and use it in GitHub Desktop.
SmtpClient C++ example MacOS version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // # встановлюємо Homebrew (якщо ще немає) | |
| // /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | |
| // # встановлюємо libcurl (включає заголовки та бібліотеку) | |
| // brew install curl | |
| #include <iostream> | |
| #include <fstream> | |
| #include <sstream> | |
| #include <ctime> | |
| #include <curl/curl.h> | |
| using namespace std; | |
| // функція для отримання поточної дати та часу у форматі "дд.мм.рррр гг:хх" | |
| string getFormattedDateTime() { | |
| time_t now = time(nullptr); | |
| tm timeInfo {}; | |
| // на macOS / Linux використовується thread-safe localtime_r | |
| localtime_r(&now, &timeInfo); | |
| char buf[80] = {}; | |
| strftime(buf, sizeof(buf), "%d.%m.%Y %H:%M", &timeInfo); | |
| return string(buf); | |
| } | |
| // читання файлу (зображення) у бінарному вигляді | |
| string readImageBase64(const string& imagePath) { | |
| ifstream file(imagePath, ios::binary); | |
| if (!file.is_open()) { | |
| cerr << "Не вдалося відкрити файл: " << imagePath << endl; | |
| return ""; | |
| } | |
| ostringstream oss; | |
| oss << file.rdbuf(); | |
| return oss.str(); | |
| } | |
| // !!! https://support.google.com/accounts/answer/185833?dark=1&sjid=3195842429112725011-EU | |
| int main() { | |
| const string senderEmail = "your_email@gmail.com"; | |
| const string senderPassword = "**** **** **** ****"; // ваш пароль додатка Google | |
| const string receiverEmail = "sunmeatrich@gmail.com"; | |
| const string imagePath = "/Users/твій_логін/1/cat.jpg"; // ← змініть шлях!!! | |
| // HTML-тіло листа | |
| string htmlBody = R"( | |
| <html> | |
| <head> | |
| <style> | |
| body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif; background-color: #f5f5f5; padding: 20px; } | |
| .container { max-width: 600px; margin: 0 auto; background-color: #fff; border-radius: 15px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); padding: 20px; } | |
| h2 { color: #1a73e8; } | |
| .highlight { background-color: #e8f0fe; padding: 15px; border-left: 4px solid #1a73e8; border-radius: 5px; } | |
| .image-container { text-align: center; margin: 20px 0; } | |
| img { max-width: 100%; height: auto; border-radius: 10px; border: 2px solid #ddd; } | |
| .footer { font-size: 12px; color: #888; text-align: center; margin-top: 20px; } | |
| </style> | |
| </head> | |
| <body> | |
| <div class='container'> | |
| <h2>Привіт!</h2> | |
| <p>Це тестовий лист з програми на C++!</p> | |
| <div class='highlight'> | |
| <p>Подивись, яке гарне оформлення!</p> | |
| </div> | |
| <div class='image-container'> | |
| <p>А ось і котик для тебе:</p> | |
| <img src="cid:catImage" alt="Котик" /> | |
| </div> | |
| <div class='footer'> | |
| Відправлено: DATE_HERE | |
| </div> | |
| </div> | |
| </body> | |
| </html> | |
| )"; | |
| // заміна дати | |
| size_t pos = htmlBody.find("DATE_HERE"); | |
| if (pos != string::npos) | |
| htmlBody.replace(pos, 9, getFormattedDateTime()); | |
| curl_global_init(CURL_GLOBAL_DEFAULT); | |
| CURL* curl = curl_easy_init(); | |
| if (curl) { | |
| curl_slist* recipients = nullptr; | |
| curl_mime* mime = curl_mime_init(curl); | |
| curl_mimepart* part; | |
| // SMTP налаштування для Gmail | |
| curl_easy_setopt(curl, CURLOPT_USERNAME, senderEmail.c_str()); | |
| curl_easy_setopt(curl, CURLOPT_PASSWORD, senderPassword.c_str()); | |
| curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587"); | |
| curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); | |
| curl_easy_setopt(curl, CURLOPT_MAIL_FROM, ("<" + senderEmail + ">").c_str()); | |
| recipients = curl_slist_append(recipients, ("<" + receiverEmail + ">").c_str()); | |
| curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); | |
| // HTML частина | |
| part = curl_mime_addpart(mime); | |
| curl_mime_data(part, htmlBody.c_str(), CURL_ZERO_TERMINATED); | |
| curl_mime_type(part, "text/html"); | |
| // вкладення зображення | |
| part = curl_mime_addpart(mime); | |
| curl_mime_filedata(part, imagePath.c_str()); | |
| curl_mime_type(part, "image/jpeg"); | |
| curl_mime_encoder(part, "base64"); | |
| curl_mime_filename(part, "cat.jpg"); | |
| curl_mime_headers(part, curl_slist_append(nullptr, "Content-ID: <catImage>"), 1); | |
| curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); | |
| // відправка | |
| CURLcode res = curl_easy_perform(curl); | |
| if (res != CURLE_OK) | |
| cerr << "Помилка при відправці листа: " << curl_easy_strerror(res) << "\n"; | |
| else | |
| cout << "Лист успішно відправлено!\n"; | |
| // очищення | |
| curl_mime_free(mime); | |
| curl_slist_free_all(recipients); | |
| curl_easy_cleanup(curl); | |
| } | |
| curl_global_cleanup(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment