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// Copyright: (c) Julian Smart
8// Licence: wxWindows licence
10
17// For compilers that support precompilation, includes "wx/wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25#include "wx/wx.h"
26#endif
27
28#include "wx/string.h"
29#include "email.h"
30
31#ifdef __WXMSW__
32#include "smapi.h"
33#endif
34
35#ifdef __UNIX__
36#include "wx/filefn.h"
37#include "wx/timer.h"
38#include "wx/wfstream.h"
39#include "stdlib.h"
40#include "unistd.h"
41#endif
42
43// Send a message.
44// The 'from' field is not needed by MAPI, and on Unix, is needed only by
45// certain MTA ( for example ssmtp) if not enter by user , will be supplied by
46// the system
47
48#ifdef __WXMSW__
49bool wxEmail::Send(wxMailMessage& message, int sendMethod,
50 const wxString& profileName, const wxString& sendMail2,
51 const wxString& sendMail1, const wxString& sendMail0) {
52 wxString mailURL = "mailto:";
53 mailURL += message.m_to[0] + "?&subject=" + message.m_subject + "&body=";
54 wxString msgBody = message.m_body;
55 msgBody.Replace(" ", "%20");
56 msgBody.Replace("\n", "%0A");
57 mailURL += msgBody;
58
59 wxLaunchDefaultBrowser(mailURL);
60 return true;
61#if 0
62 // wxASSERT (message.m_to.GetCount() > 0) ;
63 wxASSERT (!message.m_to.IsEmpty()) ;
64 wxString profile(profileName);
65 if (profile.IsEmpty())
66 profile = wxGetUserName();
67
68 wxMapiSession session;
69
70 if (!session.MapiInstalled())
71 return FALSE;
72 if (!session.Logon(profile))
73 return FALSE;
74
75 return session.Send(message);
76#endif
77}
78#elif defined(__UNIX__)
79bool wxEmail::Send(wxMailMessage& message, int sendMethod,
80 const wxString& profileName, const wxString& sendMail2,
81 const wxString& sendMail1, const wxString& sendMail0) {
82 wxASSERT_MSG(!message.m_to.IsEmpty(), "no recipients to send mail to");
83
84 wxString from = message.m_from;
85 if (from.empty()) {
86 from = wxGetEmailAddress();
87 }
88
89 wxString msg, sendmail;
90
91 if (sendMethod == 0) { // with xdg-email via local mail system (MUA)
92#ifdef __WXMAC__
93 wxString addr;
94 for (size_t rcpt = 0; rcpt < message.m_to.GetCount(); rcpt++) {
95 if (rcpt > 0) addr << ",";
96 addr << message.m_to[rcpt];
97 }
98 wxString msg = wxString::Format(
99 "sh -c \"open 'mailto:%s?subject=%s&body=%s'\"", addr.c_str(),
100 message.m_subject.c_str(), message.m_body.c_str());
101 long ret = wxExecute(msg.c_str());
102 return ret != 0; // 0 means the execution failed
103#else
104 if (wxFileExists(sendMail0))
105 sendmail << sendMail0;
106 else if (wxFileExists(sendMail1))
107 sendmail << sendMail1;
108 else {
109 wxLogMessage("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 << "To: ";
125 for (size_t rcpt = 0; rcpt < message.m_to.GetCount(); rcpt++) {
126 if (rcpt) msg << ", ";
127 msg << message.m_to[rcpt];
128 }
129 msg << "\nFrom: " << from << "\nSubject: " << message.m_subject;
130 msg << "\n\n" << message.m_body;
131
132 wxString filename;
133 filename.Printf("/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 << " < " << 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("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.