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: email.cpp 35650 2005-09-23 12:56:45Z MR $
8// Copyright:   (c) Julian Smart
9// Licence:     wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
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
30#ifdef __UNIX__
31#include "wx/filefn.h"
32#include "wx/timer.h"
33#include "wx/wfstream.h"
34#include "stdlib.h"
35#include "unistd.h"
36#endif
37
38// Send a message.
39// Specify profile, or leave it to wxWidgets to find the current user name
40
41#ifdef __WXMSW__
42bool wxEmail::Send(wxMailMessage& message, const wxString& profileName, const wxString& WXUNUSED(sendMail))
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}
59#elif defined(__UNIX__)
60bool
61wxEmail::Send(wxMailMessage& message,
62              const wxString& profileName,
63              const wxString& sendMail)
64{
65    wxASSERT_MSG( !message.m_to.IsEmpty(), _T("no recipients to send mail to") ) ;
66
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.empty() )
72    {
73        from = wxGetEmailAddress();
74    }
75
76    wxString msg;
77    msg << wxT("To: ");
78
79    const size_t rcptCount = message.m_to.GetCount();
80    for (size_t rcpt = 0; rcpt < rcptCount; rcpt++)
81    {
82        if ( rcpt )
83            msg << wxT(", ");
84        msg << message.m_to[rcpt];
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}
119#else
120#error Send not yet implemented for this platform.
121#endif
122
123