]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/net/email.cpp
don't use wxStaticCast to wxBookCtrlBase -- it doesn't work
[wxWidgets.git] / contrib / src / net / email.cpp
CommitLineData
decb3a6a
JS
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
decb3a6a
JS
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
23#include "wx/string.h"
24#include "wx/net/email.h"
25
26#ifdef __WXMSW__
27#include "wx/net/smapi.h"
28#endif
29
a23c0047
JS
30#ifdef __UNIX__
31#include "wx/filefn.h"
32#include "wx/timer.h"
33#include "wx/wfstream.h"
34#include "stdlib.h"
b57b228b 35#include "unistd.h"
a23c0047
JS
36#endif
37
decb3a6a 38// Send a message.
be5a51fb 39// Specify profile, or leave it to wxWidgets to find the current user name
decb3a6a
JS
40
41#ifdef __WXMSW__
a23c0047 42bool wxEmail::Send(wxMailMessage& message, const wxString& profileName, const wxString& WXUNUSED(sendMail))
decb3a6a
JS
43{
44 wxASSERT (message.m_to.GetCount() > 0) ;
45
46 wxString profile(profileName);
47 if (profile.IsEmpty())
48 profile = wxGetUserName();
49
50 wxMapiSession session;
51
52 if (!session.MapiInstalled())
53 return FALSE;
54 if (!session.Logon(profile))
55 return FALSE;
56
57 return session.Send(message);
58}
a23c0047 59#elif defined(__UNIX__)
8e2ff10c
VZ
60bool
61wxEmail::Send(wxMailMessage& message,
62 const wxString& profileName,
63 const wxString& sendMail)
a23c0047 64{
8e2ff10c
VZ
65 wxASSERT_MSG( !message.m_to.IsEmpty(), _T("no recipients to send mail to") ) ;
66
a23c0047
JS
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;
8e2ff10c 71 if ( from.empty() )
a23c0047
JS
72 {
73 from = wxGetEmailAddress();
74 }
75
a23c0047
JS
76 wxString msg;
77 msg << wxT("To: ");
78
8e2ff10c
VZ
79 const size_t rcptCount = message.m_to.GetCount();
80 for (size_t rcpt = 0; rcpt < rcptCount; rcpt++)
a23c0047 81 {
8e2ff10c 82 if ( rcpt )
a23c0047 83 msg << wxT(", ");
8e2ff10c 84 msg << message.m_to[rcpt];
a23c0047
JS
85 }
86
87 msg << wxT("\nFrom: ") << from << wxT("\nSubject: ") << message.m_subject;
88 msg << wxT("\n\n") << message.m_body;
89
90 wxString filename;
91 filename.Printf(wxT("/tmp/msg-%ld-%ld-%ld.txt"), (long) getpid(), wxGetLocalTime(),
92 (long) rand());
93
94 {
95 wxFileOutputStream stream(filename);
96 if (stream.Ok())
97 {
98 stream.Write(msg, msg.Length());
99 }
100 else
101 {
102 return FALSE ;
103 }
104 }
105
106 // TODO search for a suitable sendmail if sendMail is empty
107 wxString sendmail(sendMail);
108
109 wxString cmd;
110 cmd << sendmail << wxT(" < ") << filename;
111
112 // TODO: check return code
113 wxSystem(cmd.c_str());
114
115 wxRemoveFile(filename);
116
117 return TRUE;
118}
decb3a6a
JS
119#else
120#error Send not yet implemented for this platform.
121#endif
122