]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/net/email.cpp
Committing in .
[wxWidgets.git] / contrib / src / net / email.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: email.h
3 // Purpose: wxEmail: portable email client class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2001-08-21
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "email.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/string.h"
28 #include "wx/net/email.h"
29
30 #ifdef __WXMSW__
31 #include "wx/net/smapi.h"
32 #endif
33
34 #ifdef __UNIX__
35 #include "wx/filefn.h"
36 #include "wx/timer.h"
37 #include "wx/wfstream.h"
38 #include "stdlib.h"
39 #include "unistd.h"
40 #endif
41
42 // Send a message.
43 // Specify profile, or leave it to wxWindows to find the current user name
44
45 #ifdef __WXMSW__
46 bool wxEmail::Send(wxMailMessage& message, const wxString& profileName, const wxString& WXUNUSED(sendMail))
47 {
48 wxASSERT (message.m_to.GetCount() > 0) ;
49
50 wxString profile(profileName);
51 if (profile.IsEmpty())
52 profile = wxGetUserName();
53
54 wxMapiSession session;
55
56 if (!session.MapiInstalled())
57 return FALSE;
58 if (!session.Logon(profile))
59 return FALSE;
60
61 return session.Send(message);
62 }
63 #elif defined(__UNIX__)
64 bool wxEmail::Send(wxMailMessage& message, const wxString& profileName, const wxString& sendMail)
65 {
66 wxASSERT (message.m_to.GetCount() > 0) ;
67
68 // The 'from' field is optionally supplied by the app; it's not needed
69 // by MAPI, and on Unix, will be guessed if not supplied.
70 wxString from = message.m_from;
71 if (from.IsEmpty())
72 {
73 from = wxGetEmailAddress();
74 }
75
76 wxASSERT (!from.IsEmpty());
77
78 wxString msg;
79 msg << wxT("To: ");
80
81 size_t i;
82 for (i = 0; i < message.m_to.GetCount(); i++)
83 {
84 msg << message.m_to[i];
85 if (i < message.m_to.GetCount())
86 msg << wxT(", ");
87 }
88
89 msg << wxT("\nFrom: ") << from << wxT("\nSubject: ") << message.m_subject;
90 msg << wxT("\n\n") << message.m_body;
91
92 wxString filename;
93 filename.Printf(wxT("/tmp/msg-%ld-%ld-%ld.txt"), (long) getpid(), wxGetLocalTime(),
94 (long) rand());
95
96 {
97 wxFileOutputStream stream(filename);
98 if (stream.Ok())
99 {
100 stream.Write(msg, msg.Length());
101 }
102 else
103 {
104 return FALSE ;
105 }
106 }
107
108 // TODO search for a suitable sendmail if sendMail is empty
109 wxString sendmail(sendMail);
110
111 wxString cmd;
112 cmd << sendmail << wxT(" < ") << filename;
113
114 // TODO: check return code
115 wxSystem(cmd.c_str());
116
117 wxRemoveFile(filename);
118
119 return TRUE;
120 }
121 #else
122 #error Send not yet implemented for this platform.
123 #endif
124