OpenCPN Partial API docs
Loading...
Searching...
No Matches
email.cpp
Go to the documentation of this file.
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
11
15// For compilers that support precompilation, includes "wx/wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19#pragma hdrstop
20#endif
21
22#ifndef WX_PRECOMP
23#include "wx/wx.h"
24#endif
25
26#include "wx/string.h"
27#include "email.h"
28
29#ifdef __WXMSW__
30#include "smapi.h"
31#endif
32
33#ifdef __UNIX__
34#include "wx/filefn.h"
35#include "wx/timer.h"
36#include "wx/wfstream.h"
37#include "stdlib.h"
38#include "unistd.h"
39#endif
40
41// Send a message.
42// The 'from' field is not needed by MAPI, and on Unix, is needed only by
43// certain MTA ( for example ssmtp) if not enter by user , will be supplied by
44// the system
45
46#ifdef __WXMSW__
47bool wxEmail::Send(wxMailMessage& message, int sendMethod,
48 const wxString& profileName, const wxString& sendMail2,
49 const wxString& sendMail1, const wxString& sendMail0) {
50 wxString mailURL = _T("mailto:");
51 mailURL +=
52 message.m_to[0] + _T("?&subject=") + message.m_subject + _T("&body=");
53 wxString msgBody = message.m_body;
54 msgBody.Replace(" ", "%20");
55 msgBody.Replace("\n", "%0A");
56 mailURL += msgBody;
57
58 wxLaunchDefaultBrowser(mailURL);
59 return true;
60#if 0
61 // wxASSERT (message.m_to.GetCount() > 0) ;
62 wxASSERT (!message.m_to.IsEmpty()) ;
63 wxString profile(profileName);
64 if (profile.IsEmpty())
65 profile = wxGetUserName();
66
67 wxMapiSession session;
68
69 if (!session.MapiInstalled())
70 return FALSE;
71 if (!session.Logon(profile))
72 return FALSE;
73
74 return session.Send(message);
75#endif
76}
77#elif defined(__UNIX__)
78bool wxEmail::Send(wxMailMessage& message, int sendMethod,
79 const wxString& profileName, const wxString& sendMail2,
80 const wxString& sendMail1, const wxString& sendMail0) {
81 wxASSERT_MSG(!message.m_to.IsEmpty(), _T("no recipients to send mail to"));
82
83 wxString from = message.m_from;
84 if (from.empty()) {
85 from = wxGetEmailAddress();
86 }
87
88 wxString msg, sendmail;
89
90 if (sendMethod == 0) { // with xdg-email via local mail system (MUA)
91#ifdef __WXMAC__
92 wxString addr;
93 for (size_t rcpt = 0; rcpt < message.m_to.GetCount(); rcpt++) {
94 if (rcpt > 0) addr << ",";
95 addr << message.m_to[rcpt];
96 }
97 wxString msg = wxString::Format(
98 "sh -c \"open 'mailto:%s?subject=%s&body=%s'\"", addr.c_str(),
99 message.m_subject.c_str(), message.m_body.c_str());
100 long ret = wxExecute(msg.c_str());
101 return ret != 0; // 0 means the execution failed
102#else
103 if (wxFileExists(sendMail0))
104 sendmail << sendMail0;
105 else if (wxFileExists(sendMail1))
106 sendmail << sendMail1;
107 else {
108 wxLogMessage(
109 _T("MAIL Error: xdg-email is not installed on this computer!"));
110 return false;
111 }
112
113 msg << "sh -c \" " << sendmail << " --utf8 --subject '"
114 << message.m_subject << "' "
115 << "--body '" << message.m_body << "'";
116 for (size_t rcpt = 0; rcpt < message.m_to.GetCount(); rcpt++)
117 msg << " '" << message.m_to[rcpt] << "'"; // add recipients
118 msg << "\"";
119
120 wxSystem(msg.c_str());
121 return true;
122
123 } else { // directly with sendmail
124 msg << wxT("To: ");
125 for (size_t rcpt = 0; rcpt < message.m_to.GetCount(); rcpt++) {
126 if (rcpt) msg << wxT(", ");
127 msg << message.m_to[rcpt];
128 }
129 msg << wxT("\nFrom: ") << from << wxT("\nSubject: ") << message.m_subject;
130 msg << wxT("\n\n") << message.m_body;
131
132 wxString filename;
133 filename.Printf(wxT("/tmp/msg-%ld-%ld-%ld.txt"), (long)getpid(),
134 wxGetLocalTime(), (long)rand());
135
136 wxFileOutputStream stream(filename);
137 if (stream.Ok())
138 stream.Write(msg.ToUTF8(), msg.Length());
139 else
140 return FALSE;
141
142 // TODO search for a suitable sendmail if sendMail is empty
143 sendmail << sendMail2;
144
145 wxString cmd;
146 cmd << sendmail << wxT(" < ") << filename;
147
148 // TODO: check return code
149 wxSystem(cmd.c_str());
150
151 wxRemoveFile(filename);
152
153 return TRUE;
154#endif
155 }
156 return FALSE;
157}
158#else
159wxLogMessage(_T("Send eMail not yet implemented for this platform"));
160return false;
161// #error Send not yet implemented for this platform.
162#endif
Email Request System for GRIB Data.