sending an email from a C/C++ program in linux
You could invoke your local MTA directly using popen()
and feed it RFC822-compliant text.
#include <stdio.h>
#include <string.h>
#include <errno.h>
int sendmail(const char *to, const char *from, const char *subject, const char *message)
{
int retval = -1;
FILE *mailpipe = popen("/usr/lib/sendmail -t", "w");
if (mailpipe != NULL) {
fprintf(mailpipe, "To: %s\n", to);
fprintf(mailpipe, "From: %s\n", from);
fprintf(mailpipe, "Subject: %s\n\n", subject);
fwrite(message, 1, strlen(message), mailpipe);
fwrite(".\n", 1, 2, mailpipe);
pclose(mailpipe);
retval = 0;
}
else {
perror("Failed to invoke sendmail");
}
return retval;
}
main(int argc, char** argv)
{
if (argc == 5) {
sendmail(argv[1], argv[2], argv[3], argv[4]);
}
}
A simple way to send mails from a C application on Unix
Here's a nice SMTP library, libESMTP
sending an email with C++ (Linux/Mac)
If you want to do this in C++ it would be the best to go for some library like
- libquickmail
- vmime
If it is ok for you to call some other program (like linux terminal program) go and check this stackoverflow answers send-mail-from-linux-terminal-in-one-line
Using the last method will leave with you with something like that (minimal example):
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[]){
int status;
status = system(R"(echo "this is the body" | mail -s "this is the subject" "to@address")");
return 0;
}
R"()" is c++ string literal so you don't have to care about escape characters (but is available since C++11).
Here see the documentation for system to check how it work.
Send emails from linux appication in c++
You could use something like the QT framework on linux to help you.
But you probably just need this: Send Mail using SMTP in C++ on Linux
Hope that helps,
~ Dan
Sending emails using C
You should take a look at some examples on smtp via telnet :)
Basically you need to input in plaintext something like this:
HELO local.domain.name
MAIL FROM: mail@domain.ext
RCPT TO: mail@otherdomain.ext
DATA
...
EDIT according to this example, your code should be:
// Not sure about this one, maybe just "\n"
#define SEPARATOR "\n\r"
int sendData( Socket *socket, const char *data) {
int iResult;
iResult = send(socket, data, (int) strlen(data), 0);
if(iResult == SOCKET_ERROR){
// Do error handling as you like
}
return iResult;
}
sendData( socket, "HELO local.doman.name" SEPARATOR);
sendData( socket, "MAIL FROM: mail@domain.ext" SEPARATOR);
sendData( socket, "RCPT TO: mail@otherdomain.ext" SEPARATOR);
sendData( socket, "DATA" SEPARATOR);
sendData( socket, "This is subject of my mail" SEPARATOR SEPARATOR);
sendData( socket, "And this is text" SEPARATOR);
sendData( socket, "." SEPARATOR); // Send mail
Mailx from within a C program?
Working at the command line, I got this to work for me (with my normal corporate email address in place of me@example.com
, of course):
mailx -s "Just a test" -t <<EOF
To: me@example.com
Subject: Just a test with a subject
Just testing mailx -t which seems to ignore -s options too
-=JL=-
EOF
The -s
option subject line was ignored, as hinted in the body text. (This is a mailx
version 12.5 6/20/10
on a machine running a derivative of Ubuntu 12.04 LTS.)
Note that the To:
line is case-sensitive and space-sensitive (at least there must be a space after the colon). This is standard notation for the headers defined by RFC-822 (or whatever its current incarnation is). When I tried with the -s
option but no Subject:
line, I got a message without a subject in my inbox.
The following should work for you:
$ cat /home/me/Email_list.txt
To: My.email@company.com
Subject: Test email
$ { cat /home/me/Email_List.txt; echo "Test Text"; } | mailx -t
$
Note that blank line! Or you could use a plain echo;
before the echo "Test text";
. The semicolons are needed in the { ... }
notation.
Send Mail using SMTP in C++ on Linux
You might be interested in libcURL.
It's a great multi-platform C library which supports a lot of different protocols, including SMTP.
The official web page contains samples and tips to get you started.
Here is one that might help you.
Bindings exists for C++ (but I never used them) if you don't want to use the C interface.
Related Topics
How to Switch Between Blas Libraries Without Recompiling Program
Intercepting/Rerouting Tcp Syn Packets to C++ Program in Linux
Q_Object Throwing 'Undefined Reference to Vtable' Error
Iterate Through Struct and Class Members
When to Use Std::Forward to Forward Arguments
Differencebetween Pre-Increment and Post-Increment in the Cycle (For/While)
What Is the Purpose of Allocating a Specific Amount of Memory for Arrays in C++
How to Make Visual Studio's Build Be Very Verbose
Making Std::Vector Allocate Aligned Memory
Using Qsocketnotifier to Select on a Char Device
Is There a Decent Wait Function in C++
Std::Fstream Doesn't Create File
Qtcore.Qobject.Connect in a Loop Only Affects the Last Instance