-2

I was used to use Matlab, and for very long simulation I created a function which sent me an email whenever Matlab finished. It was a very easy Matlab function, you just had to add your email, password and the SMTP (I think).

Now, because of university stuff, I have to use C++ (I'm not very familiar with it, as you have probably guessed) but I can't find an equivalent way for sending an email to myself. I compile my .cpp in Terminal, using g++.

Can you please help me? I don't know if I miss some libraries or what.

Ahmet Kakıcı
  • 6,294
  • 4
  • 37
  • 49
Pan
  • 3
  • 2
  • Have you really tried anything? If yes, what? Do you get an error? – Megasa3 Jan 26 '18 at 11:18
  • 1
    I guess you need to use an external system call or use a library for that, on linux there are many ways to send an email, you don't need to use pure C++ for that (if you want to keep it simple). So you only need to figure out how to send an email from the terminal with some linux tool (e.g. `sendmail`) and then how to execute a system call from c++. – xander Jan 26 '18 at 11:19

1 Answers1

2

If you want to do this in C++ it would be the best to go for some library like

  1. libquickmail
  2. 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.

wdudzik
  • 1,264
  • 15
  • 24
  • Oh yes!! I think this will be fine! Thank you so much! – Pan Jan 26 '18 at 15:30
  • So you can accept this answer and upvote if it was useful :) – wdudzik Jan 26 '18 at 21:06
  • Yes, I was trying to use libquickmail, using my pc (Mac). I've installed both libquickmail and libcurl but I always get this error: Undefined symbols for architecture x86_64. Can the problem be in my g++ from terminal? There are many similar problem, each one with a different solution. On Monday I'm gonna try with linux and see if something changes. – Pan Jan 26 '18 at 23:31