]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/net/email.cpp
77a6aabcd00963748a7cb152287d2ff7dd454126
[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 wxWidgets 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
65 wxEmail::Send(wxMailMessage& message,
66 const wxString& profileName,
67 const wxString& sendMail)
68 {
69 wxASSERT_MSG( !message.m_to.IsEmpty(), _T("no recipients to send mail to") ) ;
70
71
72 // The 'from' field is optionally supplied by the app; it's not needed
73 // by MAPI, and on Unix, will be guessed if not supplied.
74 wxString from = message.m_from;
75 if ( from.empty() )
76 {
77 from = wxGetEmailAddress();
78 }
79
80 wxString msg;
81 msg << wxT("To: ");
82
83 const size_t rcptCount = message.m_to.GetCount();
84 for (size_t rcpt = 0; rcpt < rcptCount; rcpt++)
85 {
86 if ( rcpt )
87 msg << wxT(", ");
88 msg << message.m_to[rcpt];
89 }
90
91 msg << wxT("\nFrom: ") << from << wxT("\nSubject: ") << message.m_subject;
92 msg << wxT("\n\n") << message.m_body;
93
94 wxString filename;
95 filename.Printf(wxT("/tmp/msg-%ld-%ld-%ld.txt"), (long) getpid(), wxGetLocalTime(),
96 (long) rand());
97
98 {
99 wxFileOutputStream stream(filename);
100 if (stream.Ok())
101 {
102 stream.Write(msg, msg.Length());
103 }
104 else
105 {
106 return FALSE ;
107 }
108 }
109
110 // TODO search for a suitable sendmail if sendMail is empty
111 wxString sendmail(sendMail);
112
113 wxString cmd;
114 cmd << sendmail << wxT(" < ") << filename;
115
116 // TODO: check return code
117 wxSystem(cmd.c_str());
118
119 wxRemoveFile(filename);
120
121 return TRUE;
122 }
123 #else
124 #error Send not yet implemented for this platform.
125 #endif
126