C 言語にて libcurl を使って メールを送信する
の続きです。
curlsmtp
curlsmtp は、電子メールを送信するためのC++ライブラリです。
SMTP通信には、libcurl を使用します。
https://github.com/honeyligo/curlsmtp
curlsmtpのインストール
(1) プロジェクトファイルをダウンロードする。
git clone https://github.com/honeyligo/curlsmtp
(2) 下記の3つのファイルをアプリ作成のディレクトリに配置する。
- curlsmtp.cpp
- curlsmtp.h
- ustd_string.h
curlsmtp のプログラム例
同封されている下記のサンプルを参考にした。 https://github.com/honeyligo/curlsmtp/blob/master/main.cpp
#include "curlsmtp.h" std::string server = "smtp.gmail.com", std::string port = "587"; std::stringfrom = "hoge@example.com"; std::string password = "password"; std::vector<std::string> to = { "hoge@example.com" }; std::vector<std::string> secret; std::vector<std::string> cc; std::vector<std::string> attach; std::string subject = "test mail"; std::string message = "this is test mail"; CurlSmtp* mail = new CurlSmtp( from, password, to, secret, cc, attach, subject, message, server port ); mail->send_mail();
username と mail_from を分離する
curlsmtp は ユーザ名(username) と 送信者メールアドレス(mail_from) を区別せず一緒になっている。 Gmailでは、 ユーザ名はメールアドレスなので、問題ない。 potfix サーバーでは、ユーザ名と 送信者メールアドレスは別なので、問題あり。
両者を別々に設定できるように改変する。 メンバー変数 user を追加し、 下記のように変更する。
// curl_easy_setopt(curl_, CURLOPT_USERNAME, from_.c_str()); curl_easy_setopt(curl_, CURLOPT_USERNAME, user_.c_str());
改変したコードは、githubに公開した。 https://github.com/ohwada/MAC_cpp_Samples/tree/master/curlsmtp