How to send an email?

I know this is probably a stupid question, but I've been looking for tutorials and examples and all I get are nefarious ways to send keyloggers or something dumb like "How to email your C++ code".

All I want is to send a an email to a user based on their given email with a message based on a string variable. No strings attached. Something like this would be suitable.

1
2
3
4
5
6
7
8
9
int main() {
    string email;
    string message;
    cout << "What's your email?" << endl;
    cin >> email;
    cout << "What message do you want to send?" << endl;
    getline(cin, message);
    //send the message to the given email
}


At some point I'd like to create an email verification system before I start sending emails to any address the user gives me, but for now I'd just like to create a basic code that can send emails. Thank you.
One of the easiest ways is to just use a library.

Here's an example using libcurl.
https://curl.haxx.se/libcurl/c/smtp-mail.html
<curl/curl.h> "no such file or directory".

I'm guessing I need to install the library or something--sorry I'm a bit new to this. How would I do that?

EDIT: I installed curl-7.64.0.tar.gz and untared it, getting a curl-7.64.0 directory, but I'm still having this issue.
Last edited on
Welcome to the world of 3rd-party libaries. C++ itself doesn't have built-in functionality for email, so you need to either implement it yourself (HARD, don't waste your time learning email protocols), or use a library to help you. cURL can do that.

Yep, you'll need to build the library on your system, and then link your code to it.

See this link: https://curl.haxx.se/docs/install.html

I have not built libcurl but try reading through it. It will probably seem complicated if you've never done something like this before. If you're on Windows, I personally I find it the easiest to build these sorts of libraries using MSYS2, but you don't need it.
What system?
Have you ever installed anything before?

Try (in google or whatever): "How to install curl on <your system here>"
Last edited on
I suppose we ought to establish which OS and compiler you're using.
unix has mail on the commandline you can borrow it with system calls very easily.

windows, it is less easy:
https://www.howtogeek.com/120011/stupid-geek-tricks-how-to-send-email-from-the-command-line-in-windows-without-extra-software/
I am using Linux. I have the curl-7.64.0 directory, but it's not working...do I need to put my code in the directory too?
no, your code can be wherever. Just need to get the paths right for #includes and library objects in your compile.

on Linux, something like system "mail -s "Test Subject" user@example.com < /dev/null" can be rigged up as well, for the cheesy get it done approach. Not the best approach to everything, but its 'an' answer to the problem. Ive used this to alert myself when something had a problem.
Last edited on
So I would need to change my makefile somehow?

1
2
3
4
5
6
7
8
9
10
11
12
CXX = g++
CXXFLAGS = -c -g -std=c++11 -Wall -W -Werror -pedantic
LDFLAGS =

email: main.o
	$(CXX) $(LDFLAGS) main.o

main.o : main.cpp
	$(CXX) $(CXXFLAGS) main.cpp

clean :
	rm -f core $(PROG) *.o


And in include, it'd be something like this?

#include <curl-7.64.0/curl/curl.h>

Instead of #include <curl/curl.h>?
> I am using Linux.
Debian, Ubuntu, Redhat, Suse, Gentoo ................

Regardless, the easier way is to use your favourite package manager to locate pre-configured libraries for your system.

On Ubuntu, I did
sudo apt-get install libcurl4-openssl-dev
Topic archived. No new replies allowed.