Created
September 1, 2019 00:10
-
-
Save sanyarnd/229b7a8aef416ee4566b6823b678bdbb to your computer and use it in GitHub Desktop.
Sample CGI SMTP application
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
| #include <iostream> | |
| #include <vector> | |
| #include <string> | |
| #include <sstream> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <cgicc/CgiDefs.h> | |
| #include <cgicc/Cgicc.h> | |
| #include <cgicc/HTTPHTMLHeader.h> | |
| #include <cgicc/HTMLClasses.h> | |
| #define HAVE_STDINT_H 1 | |
| #include <mimetic/mimetic.h> | |
| #include "Poco/Net/MailMessage.h" | |
| #include "Poco/Net/MailRecipient.h" | |
| #include "Poco/Net/SMTPClientSession.h" | |
| #include "Poco/Net/NetException.h" | |
| #include "Poco/Net/SecureSMTPClientSession.h" | |
| #include "Poco/Net/InvalidCertificateHandler.h" | |
| #include "Poco/Net/AcceptCertificateHandler.h" | |
| #include "Poco/Net/SSLManager.h" | |
| #include "Poco/Net/SecureStreamSocket.h" | |
| #include "Poco/Net/MailRecipient.h" | |
| #include "Poco/Net/StringPartSource.h" | |
| using namespace std; | |
| using namespace cgicc; | |
| using namespace mimetic; | |
| using namespace Poco::Net; | |
| using namespace Poco; | |
| int main () | |
| { | |
| Cgicc cgi; | |
| cout << "Content-type:text/html\r\n\r\n"; | |
| cout << "<html>\n"; | |
| cout << "<head>\n"; | |
| cout << "<title>SMTP Simple Sender Response</title>\n"; | |
| cout << "</head>\n"; | |
| cout << "<body>\n"; | |
| form_iterator fi = cgi.getElement("name"); | |
| string sender_name = ""; | |
| if( !fi->isEmpty() && fi != (*cgi).end()) { | |
| sender_name.assign(**fi); | |
| cout << "sender_name: " << sender_name << endl; | |
| } else { | |
| cout << "No text entered for sender_name" << endl; | |
| } | |
| cout << "<br/>\n"; | |
| fi = cgi.getElement("email"); | |
| string sender_email = ""; | |
| if( !fi->isEmpty() &&fi != (*cgi).end()) { | |
| sender_email.assign(**fi); | |
| cout << "sender_email: " << sender_email << endl; | |
| } else { | |
| cout << "No text entered for sender_email" << endl; | |
| } | |
| cout << "<br/>\n"; | |
| const_file_iterator file = cgi.getFile("attachment"); | |
| string attachment, attach_name, attach_mime; | |
| bool attach = false; | |
| if(file != cgi.getFiles().end()) { | |
| attach_name = HTTPContentHeader(file->getFilename()).getData(); | |
| attach_mime = HTTPContentHeader(file->getDataType()).getData(); | |
| attachment = HTTPContentHeader(file->getData()).getData(); | |
| attach = true; | |
| } | |
| fi = cgi.getElement("description"); | |
| string description = ""; | |
| if( !fi->isEmpty() && fi != (*cgi).end()) { | |
| description.assign(**fi); | |
| cout << "Description: " << description << endl; | |
| } else { | |
| cout << "No text entered" << endl; | |
| } | |
| cout << "<br/>\n"; | |
| string host = "mx.yandex.ru"; | |
| UInt16 port = 25; | |
| string admin_email = "whatever@yandex.ru"; | |
| string from = sender_email; | |
| string subject = "Simple SMTP sender"; | |
| subject = MailMessage::encodeWord(subject, "UTF-8"); | |
| MailMessage message; | |
| message.setSender(from); | |
| message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, admin_email)); | |
| message.setSubject(subject); | |
| message.addContent(new StringPartSource(description, "text/plain")); | |
| if (attach) { | |
| message.addAttachment(attach_name, new StringPartSource(attachment, attach_mime)); | |
| } | |
| try { | |
| SMTPClientSession session(host, port); | |
| session.open(); | |
| try { | |
| session.login(); | |
| // session.login(SMTPClientSession::AUTH_LOGIN, user, password); | |
| session.sendMessage(message); | |
| session.close(); | |
| } catch (SMTPException &e) { | |
| cerr << e.displayText() << endl; | |
| session.close(); | |
| } | |
| } catch (NetException &e) { | |
| cerr << e.displayText() << endl; | |
| } | |
| cout << "<br/>\n<br/>\nMessage:<br/>\n<br/>\n"; | |
| cout << "<div><xmp>"; | |
| message.write(cout); | |
| cout << "</xmp></div>"; | |
| cout << "</body>\n"; | |
| cout << "</html>\n"; | |
| return 0; | |
| } |
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
| <html> | |
| <title>Simple SMTP sender</title> | |
| <form method="POST" enctype="multipart/form-data" action="./cgi_smtp.cgi"> | |
| Your name:<br> <input type="text" name="name" size="40"> | |
| <p> Your e-mail:<br> <input type="text" name="email" size="40"> | |
| <p> Message: <br> | |
| <textarea name="description" rows="8" cols="45"></textarea> | |
| <p> Attachment: <br> | |
| <input type="file" name="attachment" /> | |
| <p> <input type="submit" name="submit" value="submit"> | |
| </form> | |
| </html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment