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 = "mailto:";
51 mailURL += message.m_to[0] + "?&subject=" + message.m_subject + "&body=";
52 wxString msgBody = message.m_body;
53 msgBody.Replace(" ", "%20");
54 msgBody.Replace("\n", "%0A");
55 mailURL += msgBody;
56
57 wxLaunchDefaultBrowser(mailURL);
58 return true;
59#if 0
60 // wxASSERT (message.m_to.GetCount() > 0) ;
61 wxASSERT (!message.m_to.IsEmpty()) ;
62 wxString profile(profileName);
63 if (profile.IsEmpty())
64 profile = wxGetUserName();
65
66 wxMapiSession session;
67
68 if (!session.MapiInstalled())
69 return FALSE;
70 if (!session.Logon(profile))
71 return FALSE;
72
73 return session.Send(message);
74#endif
75}
76#elif defined(__UNIX__)
77bool wxEmail::Send(wxMailMessage& message, int sendMethod,
78 const wxString& profileName, const wxString& sendMail2,
79 const wxString& sendMail1, const wxString& sendMail0) {
80 wxASSERT_MSG(!message.m_to.IsEmpty(), "no recipients to send mail to");
81
82 wxString from = message.m_from;
83 if (from.empty()) {
84 from = wxGetEmailAddress();
85 }
86
87 wxString msg, sendmail;
88
89 if (sendMethod == 0) { // with xdg-email via local mail system (MUA)
90#ifdef __WXMAC__
91 wxString addr;
92 for (size_t rcpt = 0; rcpt < message.m_to.GetCount(); rcpt++) {
93 if (rcpt > 0) addr << ",";
94 addr << message.m_to[rcpt];
95 }
96 wxString msg = wxString::Format(
97 "sh -c \"open 'mailto:%s?subject=%s&body=%s'\"", addr.c_str(),
98 message.m_subject.c_str(), message.m_body.c_str());
99 long ret = wxExecute(msg.c_str());
100 return ret != 0; // 0 means the execution failed
101#else
102 if (wxFileExists(sendMail0))
103 sendmail << sendMail0;
104 else if (wxFileExists(sendMail1))
105 sendmail << sendMail1;
106 else {
107 wxLogMessage("MAIL Error: xdg-email is not installed on this computer!");
108 return false;
109 }
110
111 msg << "sh -c \" " << sendmail << " --utf8 --subject '"
112 << message.m_subject << "' "
113 << "--body '" << message.m_body << "'";
114 for (size_t rcpt = 0; rcpt < message.m_to.GetCount(); rcpt++)
115 msg << " '" << message.m_to[rcpt] << "'"; // add recipients
116 msg << "\"";
117
118 wxSystem(msg.c_str());
119 return true;
120
121 } else { // directly with sendmail
122 msg << "To: ";
123 for (size_t rcpt = 0; rcpt < message.m_to.GetCount(); rcpt++) {
124 if (rcpt) msg << ", ";
125 msg << message.m_to[rcpt];
126 }
127 msg << "\nFrom: " << from << "\nSubject: " << message.m_subject;
128 msg << "\n\n" << message.m_body;
129
130 wxString filename;
131 filename.Printf("/tmp/msg-%ld-%ld-%ld.txt", (long)getpid(),
132 wxGetLocalTime(), (long)rand());
133
134 wxFileOutputStream stream(filename);
135 if (stream.Ok())
136 stream.Write(msg.ToUTF8(), msg.Length());
137 else
138 return FALSE;
139
140 // TODO search for a suitable sendmail if sendMail is empty
141 sendmail << sendMail2;
142
143 wxString cmd;
144 cmd << sendmail << " < " << filename;
145
146 // TODO: check return code
147 wxSystem(cmd.c_str());
148
149 wxRemoveFile(filename);
150
151 return TRUE;
152#endif
153 }
154 return FALSE;
155}
156#else
157wxLogMessage("Send eMail not yet implemented for this platform");
158return false;
159// #error Send not yet implemented for this platform.
160#endif
Email Request System for GRIB Data.