Putting mailer in Windows using Code::Blocks

Hello,

I am trying to put in a mailer to send me emails in C++. I am using Code::Blocks. Every guide online has thrown me errors. I tried this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<fstream>
#include<conio.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <winable.h>
#include <cmath>
#include<string>
using namespace std;

int main(){
FILE *email= popen( "/usr/lib/sendmail", "wb" );

fprintf( email, "To: xxxxxxx@xxxxxxx.com\r\n" );
fprintf( email, "From: xxxxxx@xxxxxx.com\r\n" );
fprintf( email, "\r\n" );
fprintf( email, "Hello!\r\n" );

pclose( email );
system("pause");
return 0;
}


Error:

1
2
3
error: 'popen' was not declared in this scope|

error: 'pclose' was not declared in this scope|


What am I doing wrong?

P.S. this is an example, I did use more complex ones.
well the error means that you need to define popen and pclose do you want me to edit the code for you to stop the error?
There is no "/usr/lib/sendmail" program on windows. The code posted is Unix specific.

Regarding popen and pclose, these functions exists on windows, but are called _popen() and _pclose():
http://msdn.microsoft.com/en-us/library/96ayss4b%28v=vs.100%29.aspx

As for sending emails, the most easy solutions (and portable) is to use libcurl:
http://curl.haxx.se/libcurl/
Of course, you can use sockets directly if you insist on reimplement SMTP protocol :)

Of course, you can use sockets directly if you insist on reimplement SMTP protocol :)

Smiley face indeed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#define CRLF "\r\n"

bool SendMail(std::string FromAddr, std::string Subject, std::string File)
{
   int iProtocolPort = 0;

   char SMTPServerName[] = "mail.service.com";///Mail Server Name
   char ToAddr[] 		 = "accountname@mail.com";///Mail To Address
   char Buffer[4096];
   char Line[255];
   char MsgLine[255];

   SOCKET      Server;
   WSADATA     wsaData;
   LPHOSTENT   lpHostEntry;
   LPSERVENT   lpServEntry;
   SOCKADDR_IN SockAddr;

   std::ifstream MsgFile(File.c_str());

   if(WSAStartup(MAKEWORD(1, 1), &wsaData))
   {
      std::cout << "FAILED TO START WSA\n";

      return false;
   }

   lpHostEntry = gethostbyname(SMTPServerName);
   if(!lpHostEntry)
   {
      std::cout << "Could Not Reach Host: " << SMTPServerName << " " << WSAGetLastError() << std::endl;
      return false;
   }

   Server = socket(PF_INET, SOCK_STREAM, 0);
   if(!Server == INVALID_SOCKET)
   {
      std::cout << "Cannot Open Mail Server Socket: " << WSAGetLastError();
      return false;
   }

   lpServEntry = getservbyname("mail", 0);
   if(!lpServEntry)
   {
      iProtocolPort = htons(IPPORT_SMTP);
   }
   else
   {
      iProtocolPort = lpServEntry->s_port;
   }

   SockAddr.sin_family = AF_INET;
   SockAddr.sin_port   = iProtocolPort;
   SockAddr.sin_addr   = *((LPIN_ADDR)*lpHostEntry->h_addr_list);

   if(connect(Server, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
   {
       std::cout << "Error Connecting To Server Socket: " << WSAGetLastError();
       return false;
   }

   try
   {

        recv(Server, Buffer, sizeof(Buffer), 0);///Response From Server Should Be 220 'OK'
            if(Buffer[0] != '2')
            {
                sprintf(MsgLine, "SERVER NOT READY:%s", Buffer);
                throw Buffer;
            }

        sprintf(MsgLine, "HELO %s%s", SMTPServerName, CRLF);
        send(Server, MsgLine, strlen(MsgLine), 0);
        recv(Server, Buffer, sizeof(Buffer), 0);
            if(Buffer[0] != '2')
            {
                sprintf(MsgLine, "%s:%s", MsgLine, Buffer);
                throw MsgLine;
            }


        sprintf(MsgLine, "MAIL FROM: %s%s", FromAddr.c_str(), CRLF);
        send(Server, MsgLine, strlen(MsgLine), 0);
        recv(Server, Buffer, sizeof(Buffer), 0);
            if(Buffer[0] != '2')
            {
                sprintf(MsgLine, "%s:%s", MsgLine, Buffer);
                throw MsgLine;
            }

        sprintf(MsgLine, "RCPT TO: %s%s", ToAddr, CRLF);
        send(Server, MsgLine, strlen(MsgLine), 0);
        recv(Server, Buffer, sizeof(Buffer), 0);
            if(Buffer[0] != '2')
            {
                sprintf(MsgLine, "%s:%s", MsgLine, Buffer);
                throw MsgLine;
            }

        sprintf(MsgLine, "VRFY %s%s", ToAddr, CRLF);
        send(Server, MsgLine, strlen(MsgLine), 0);
        recv(Server, Buffer, sizeof(Buffer), 0);
            if(Buffer[0] != '2')
            {
                sprintf(MsgLine, "%s:%s", MsgLine, Buffer);
                throw MsgLine;
            }

        sprintf(MsgLine, "DATA%s", CRLF);
        send(Server, MsgLine, strlen(MsgLine), 0);
        recv(Server, Buffer, sizeof(Buffer), 0);///Mail Server Responds With Code: 354 Telling The Client To End 'DATA' Section With A '.'

        sprintf(MsgLine, "Subject: %s%s", Subject.c_str(), CRLF);
        send(Server, MsgLine, strlen(MsgLine), 0); ///The Mail Server Does Not Respond After DATA Is Sent Until It Sees '.' So No 'recv()'


        MsgFile.getline(Line, sizeof(Line));

        do
        {
            sprintf(MsgLine, "%s%s", Line, CRLF);
            send(Server, MsgLine, strlen(MsgLine), 0);
            MsgFile.getline(Line, sizeof(Line));

        }while(MsgFile.good());

        sprintf(MsgLine, "%s.%s", CRLF, CRLF);
        send(Server, MsgLine, strlen(MsgLine), 0);
        recv(Server, Buffer, sizeof(Buffer), 0);
            if(Buffer[0] != '2')
            {
                throw Buffer;
            }

        sprintf(MsgLine, "QUIT%s", CRLF);
        send(Server, MsgLine, strlen(MsgLine), 0);
        recv(Server, Buffer, sizeof(Buffer), 0);
            if(Buffer[0] != '2')
            {
                sprintf(MsgLine, "%s:%s", MsgLine, Buffer);
                throw MsgLine;
            }

        std::cout << "Message Sent\n";
   }
   catch(char* Error)
   {
       std::cout << "\nEXCEPTION CAUGHT!\n";
       std::cout << "Server Reply: " << Error << std::endl;

       sprintf(MsgLine, "RSET%s", CRLF);
       send(Server, MsgLine, strlen(MsgLine), 0);
       recv(Server, Buffer, sizeof(Buffer), 0);

       sprintf(MsgLine, "QUIT%s", CRLF);
       send(Server, MsgLine, strlen(MsgLine), 0);
   }

   shutdown(Server, SD_BOTH);
   closesocket(Server);
   WSACleanup();

   return true;
}


This will work if the mail server you are connecting to does not require authentication, I'm still working on that part on and off. This will be my next shot at an article when I get that part done. This time I won't be a complete bone head and mix pass by address up with actual references.

EDIT: You probably want to switch the "FromAddr" and "ToAddr" strings around. I'm not sure why I have it like it is here, I'll have to remember to fix that.

EDIT 2: I forgot to include the definition for 'CRLF'.
Last edited on
Error with that code:
1
2
3
Line 16|error: a function-definition is not allowed here before '{' token|

Line 177|error: expected '}' at end of input|
Are you kidding me? That post wasn't meant to be an answer to your question, it doesn't even have a valid mail server name. I was simply pointing out that SMTP isn't that difficult.

If you really don't know what that error means then this code is probably a bit above your head for now. I'm not trying to insult you here, I'm just being realistic about what you should be attempting.
Yeah, I see what you are saying. I know what that error means, I played around but couldn't fix it. I have followed the tutorials here, but sprintf, send, closesocket, etc. are all new to me. Any ideas for better research? Also, my creativity can only go so far in a CMD prompt, what GUI creator do you recommend?
That error means that you copy and pasted this function into another function, my guess would be "main()". You should revisit the tutorial section on functions when you have some time, that seems to be where your break down is.

... what GUI creator do you recommend?

I don't, I think that 99% of the time writing and maintaining a GUI is a waste of time and effort. But if you absolutely must have one I hear good things about Qt.
Okay, I will stick to CMD prompts :). I will read on functions. BTW, here is a little code I created, Let me know if there is any improvements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include <iostream>
#include <cmath>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main ()
{
string A;
string B;
string C;
float D;
string F;
float G;
float H;
float I;
float J;
float K;
float L;
float M;
B = "Goodbye! ";
C = "\n";
    cout << C;
    cout << " ********************  Welcome to the Ebay Fee Calculator. ******************** " << C << C;
    cout << "   There will be questions in this application. Enter Y for yes and N for no."<< C << C;
    cout << "                          Would you like to continue?" << C << C;
    cout << "                                      ";
    cin >> A;

if (A == "N")
{   cout << C << "                                   " << B << C << C;
    cout << "                         ";
    system("pause");
    exit(0);
}
else if (A == "Y")
{   cout << C;
    cout << "                            Enter your sale price: " << C << C;
     cout << "                                    ";
    cin >> D;
    cout << C;
    cout << "                        Did the buyer pay with PayPal? " << C << C;
    cout << "                                      ";
    cin >> F;
    cout << C;
if (F == "Y")
{
    G = D * 0.87;
    H = D - G;
    cout << C;
    cout << "                         Your Final Profit is: " << G << C << C;
    cout << "                           Total Paid in Fees: "  << H;
    cout << C << C;
    cout << "                       ";
    system("pause");
    exit(0);
}
if (F == "N")
{
    cout << "            Enter fee for payment processor. Format should be 0.00 : ";
    cout << C << C;
    cout << "                                    ";
    cin >> H;
    J = D * H;
    K = D * 0.90;
    L = H + 0.10;
    M = D * L;
    cout << C << "                         Your Final Profit is: " << K << C << C;
    cout << "                           Total Paid in Fees: "  << M << C << C;
    if (H > 0.03)
{
 cout << "                 You should consider PayPal, fees are 0.03" << C << C;
 }
    cout << "                       ";
    system("pause");
    exit(0);

}
else
{
    cout << "                                 ";
    cout << "Invalid entry ";
    cout << C;
    cout << "                         ";
    system("pause");
    exit(0);
}
}
else
{
    cout << "                                 ";
    cout << "Invalid entry ";
    cout << C;
    cout << "                         ";
    system("pause");
    exit(0);
}
}
Last edited on
Topic archived. No new replies allowed.