2020-11-18 Fred Gleason <fredg@paravelsystems.com>

* Added an 'RDSendMail()' function in 'lib/rdsendmail.[cpp|h]'.

Signed-off-by: Fred Gleason <fredg@paravelsystems.com>
This commit is contained in:
Fred Gleason 2020-11-19 08:52:51 -05:00
parent de303c1b17
commit 365a302c96
17 changed files with 579 additions and 0 deletions

1
.gitignore vendored
View File

@ -131,6 +131,7 @@ tests/readcd_test
tests/reserve_carts_test
tests/sas_switch_torture
tests/sas_torture
tests/sendmail_test
tests/stringcode_test
tests/test_hash
tests/test_pam

View File

@ -20601,3 +20601,5 @@
Operations Guide.
2020-11-16 Fred Gleason <fredg@paravelsystems.com>
* Incremented the package version to 3.4.1int6.
2020-11-18 Fred Gleason <fredg@paravelsystems.com>
* Added an 'RDSendMail()' function in 'lib/rdsendmail.[cpp|h]'.

View File

@ -208,6 +208,7 @@ dist_librd_la_SOURCES = dbversion.h\
rdschedcodes_dialog.cpp rdschedcodes_dialog.h\
rdschedruleslist.cpp rdschedruleslist.h\
rdsegmeter.cpp rdsegmeter.h\
rdsendmail.cpp rdsendmail.h\
rdsettings.cpp rdsettings.h\
rdsimpleplayer.cpp rdsimpleplayer.h\
rdslider.cpp rdslider.h\

View File

@ -148,6 +148,7 @@ SOURCES += rdrssschemas.cpp
SOURCES += rdrsscategorybox.cpp
SOURCES += rdschedcode.cpp
SOURCES += rdsegmeter.cpp
SOURCES += rdsendmail.cpp
SOURCES += rdsettings.cpp
SOURCES += rdsimpleplayer.cpp
SOURCES += rdslider.cpp
@ -292,6 +293,7 @@ HEADERS += rdrssschemas.h
HEADERS += rdrsscategorybox.h
HEADERS += rdschedcode.h
HEADERS += rdsegmeter.h
HEADERS += rdsendmail.h
HEADERS += rdsettings.h
HEADERS += rdsimpleplayer.h
HEADERS += rdslider.h

View File

@ -849,6 +849,34 @@
<source>[none]</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>is invalid</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>You must supply a &quot;from&quot; address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>unable to start sendmail</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail crashed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail returned non-zero exit code</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>ok</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RDAddCart</name>

View File

@ -845,6 +845,34 @@
<source>[none]</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>is invalid</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>You must supply a &quot;from&quot; address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>unable to start sendmail</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail crashed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail returned non-zero exit code</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>ok</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RDAddCart</name>

View File

@ -845,6 +845,34 @@
<source>[none]</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>is invalid</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>You must supply a &quot;from&quot; address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>unable to start sendmail</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail crashed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail returned non-zero exit code</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>ok</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RDAddCart</name>

View File

@ -815,6 +815,34 @@
<source>[none]</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>is invalid</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>You must supply a &quot;from&quot; address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>unable to start sendmail</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail crashed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail returned non-zero exit code</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>ok</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RDAddCart</name>

View File

@ -845,6 +845,34 @@
<source>[none]</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>is invalid</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>You must supply a &quot;from&quot; address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>unable to start sendmail</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail crashed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail returned non-zero exit code</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>ok</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RDAddCart</name>

View File

@ -845,6 +845,34 @@
<source>[none]</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>is invalid</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>You must supply a &quot;from&quot; address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>unable to start sendmail</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail crashed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail returned non-zero exit code</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>ok</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RDAddCart</name>

View File

@ -845,6 +845,34 @@
<source>[none]</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>is invalid</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>You must supply a &quot;from&quot; address</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>unable to start sendmail</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail crashed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>sendmail returned non-zero exit code</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>ok</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RDAddCart</name>

203
lib/rdsendmail.cpp Normal file
View File

@ -0,0 +1,203 @@
// rdsendmail.cpp
//
// Send an e-mail message using sendmail(1)
//
// (C) Copyright 2020 Fred Gleason <fredg@paravelsystems.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <QObject>
#include "rdsendmail.h"
#include "rduser.h"
#include <QProcess>
bool __RDSendMail_IsAscii(const QString &str)
{
for(int i=0;i<str.length();i++) {
QChar ch=str.at(i);
if((ch.cell()>127)||(ch.row()>0)) {
return false;
}
}
return true;
}
QByteArray __RDSendMail_EncodeBody(QString *charset,QString *encoding,
const QString &str)
{
if(__RDSendMail_IsAscii(str)) {
*charset="";
*encoding="";
return str.toAscii();
}
*charset=";charset=utf8";
*encoding="Content-Transfer-Encoding: base64\r\n";
QByteArray ret;
QByteArray raw=str.toUtf8();
for(int i=0;i<raw.length();i+=48) {
ret+=raw.mid(i,48).toBase64()+"\r\n";
}
return ret;
}
QByteArray __RDSendMail_EncodeHeader(const QString &str)
{
if(__RDSendMail_IsAscii(str)) {
return str.toAscii();
}
return QByteArray("=?utf-8?B?")+str.toUtf8().toBase64()+"?=";
}
//
// This implements a basic email sending capability using the system's
// sendmail(1) interface.
//
bool RDSendMail(QString *err_msg,const QString &subject,const QString &body,
const QString &from_addr,const QStringList &to_addrs,
const QStringList &cc_addrs,const QStringList &bcc_addrs)
{
QStringList args;
QProcess *proc=NULL;
QString msg="";
*err_msg="";
//
// Validate Addresses
//
if(from_addr.isEmpty()) {
*err_msg+=QObject::tr("You must supply a \"from\" address")+"\n";
}
else {
if(!RDUser::emailIsValid(from_addr)) {
*err_msg+=QObject::tr("address")+" \""+from_addr+"\" "+
QObject::tr("is invalid")+"\n";
}
}
for(int i=0;i<to_addrs.size();i++) {
if(!RDUser::emailIsValid(to_addrs.at(i))) {
*err_msg+=QObject::tr("address")+" \""+to_addrs.at(i)+"\" "+
QObject::tr("is invalid")+"\n";
}
}
for(int i=0;i<cc_addrs.size();i++) {
if(!RDUser::emailIsValid(cc_addrs.at(i))) {
*err_msg+=QObject::tr("address")+" \""+cc_addrs.at(i)+"\" "+
QObject::tr("is invalid")+"\n";
}
}
for(int i=0;i<bcc_addrs.size();i++) {
if(!RDUser::emailIsValid(bcc_addrs.at(i))) {
*err_msg+=QObject::tr("address")+" \""+bcc_addrs.at(i)+"\" "+
QObject::tr("is invalid")+"\n";
}
}
if(!err_msg->isEmpty()) {
return false;
}
//
// Compose Message
//
QString charset;
QString encoding;
QByteArray raw=__RDSendMail_EncodeBody(&charset,&encoding,body);
msg+="From: "+from_addr+"\r\n";
// msg+="Content-Type: text/plain;charset=utf-8\r\n";
// msg+="Content-Transfer-Encoding: base64\r\n";
msg+="Content-Type: text/plain"+charset+"\r\n";
msg+=encoding;
if(to_addrs.size()>0) {
msg+="To: ";
for(int i=0;i<to_addrs.size();i++) {
msg+=to_addrs.at(i)+", ";
}
msg=msg.left(msg.length()-2);
msg+="\r\n";
}
if(cc_addrs.size()>0) {
msg+="Cc: ";
for(int i=0;i<cc_addrs.size();i++) {
msg+=cc_addrs.at(i)+", ";
}
msg=msg.left(msg.length()-2);
msg+="\r\n";
}
if(bcc_addrs.size()>0) {
msg+="Bcc: ";
for(int i=0;i<bcc_addrs.size();i++) {
msg+=bcc_addrs.at(i)+", ";
}
msg=msg.left(msg.length()-2);
msg+="\r\n";
}
msg+="Subject: "+__RDSendMail_EncodeHeader(subject)+"\r\n";
msg+="\r\n";
msg+=raw;
//
// Send message
//
args.clear();
args.push_back("-bm");
args.push_back("-t");
proc=new QProcess();
proc->start("sendmail",args);
if(!proc->waitForStarted()) {
*err_msg=QObject::tr("unable to start sendmail")+"\n";
delete proc;
return false;
}
proc->write(msg.toUtf8());
proc->closeWriteChannel();
proc->waitForFinished();
if(proc->exitStatus()!=QProcess::NormalExit) {
*err_msg=QObject::tr("sendmail crashed")+"\r\n";
delete proc;
return false;
}
if(proc->exitCode()!=0) {
*err_msg=QObject::tr("sendmail returned non-zero exit code")+
QString().sprintf(": %d [",proc->exitCode())+
QString::fromUtf8(proc->readAllStandardError())+"]\n";
delete proc;
return false;
}
delete proc;
*err_msg=QObject::tr("ok");
return true;
}
bool RDSendMail(QString *err_msg,const QString &subject,const QString &body,
const QString &from_addr,const QString &to_addrs,
const QString &cc_addrs,const QString &bcc_addrs)
{
return RDSendMail(err_msg,subject,body,from_addr,
to_addrs.split(",",QString::SkipEmptyParts),
cc_addrs.split(",",QString::SkipEmptyParts),
bcc_addrs.split(",",QString::SkipEmptyParts));
}

37
lib/rdsendmail.h Normal file
View File

@ -0,0 +1,37 @@
// rdsendmail.h
//
// Send an e-mail message using sendmail(1)
//
// (C) Copyright 2020 Fred Gleason <fredg@paravelsystems.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef RDSENDMAIL_H
#define RDSENDMAIL_H
#include <QString>
#include <QStringList>
bool RDSendMail(QString *err_msg,const QString &subject,const QString &body,
const QString &from_addr,const QStringList &to_addrs,
const QStringList &cc_addrs=QStringList(),
const QStringList &bcc_addrs=QStringList());
bool RDSendMail(QString *err_msg,const QString &subject,const QString &body,
const QString &from_addr,const QString &to_addrs,
const QString &cc_addrs=QString(),
const QString &bcc_addrs=QString());
#endif // RDSENDMAIL

View File

@ -22,6 +22,7 @@
#define RDUSER_H
#include <QDateTime>
#include <QHostAddress>
class RDUser
{

View File

@ -46,6 +46,7 @@ noinst_PROGRAMS = audio_convert_test\
rdxml_parse_test\
readcd_test\
reserve_carts_test\
sendmail_test\
stringcode_test\
test_hash\
test_pam\
@ -117,6 +118,9 @@ readcd_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ @MUSICBRAINZ_LIBS@ -lQt3
dist_reserve_carts_test_SOURCES = reserve_carts_test.cpp reserve_carts_test.h
reserve_carts_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ @MUSICBRAINZ_LIBS@ -lQt3Support
dist_sendmail_test_SOURCES = sendmail_test.cpp sendmail_test.h
sendmail_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ @MUSICBRAINZ_LIBS@ -lQt3Support
dist_stringcode_test_SOURCES = stringcode_test.cpp stringcode_test.h
stringcode_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ @MUSICBRAINZ_LIBS@ -lQt3Support

97
tests/sendmail_test.cpp Normal file
View File

@ -0,0 +1,97 @@
// sendmail_test.cpp
//
// Test the Rivendell string encoder routines.
//
// (C) Copyright 2013,2016 Fred Gleason <fredg@paravelsystems.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <stdlib.h>
#include <stdio.h>
#include <qapplication.h>
#include <qvariant.h>
#include <rdapplication.h>
#include <rdcmd_switch.h>
#include <rdsendmail.h>
#include <rdweb.h>
#include "sendmail_test.h"
MainObject::MainObject(QObject *parent)
:QObject(parent)
{
QString err_msg;
QString from_addr;
QString to_addrs;
QString cc_addrs;
QString bcc_addrs;
QString subject;
QString body;
//
// Read Command Options
//
RDCmdSwitch *cmd=
new RDCmdSwitch(qApp->argc(),qApp->argv(),"sendmail_test",
SENDMAIL_TEST_USAGE);
for(unsigned i=0;i<cmd->keys();i++) {
if(cmd->key(i)=="--from-addr") {
from_addr=cmd->value(i);
cmd->setProcessed(i,true);
}
if(cmd->key(i)=="--to-addrs") {
to_addrs=cmd->value(i);
cmd->setProcessed(i,true);
}
if(cmd->key(i)=="--cc-addrs") {
cc_addrs=cmd->value(i);
cmd->setProcessed(i,true);
}
if(cmd->key(i)=="--bcc-addrs") {
bcc_addrs=cmd->value(i);
cmd->setProcessed(i,true);
}
if(cmd->key(i)=="--subject") {
subject=cmd->value(i);
cmd->setProcessed(i,true);
}
if(cmd->key(i)=="--body") {
body=cmd->value(i);
cmd->setProcessed(i,true);
}
if(!cmd->processed(i)) {
fprintf(stderr,"sendmail_test: unknown option \"%s\"\n",
(const char *)cmd->key(i));
exit(RDApplication::ExitInvalidOption);
}
}
if(!RDSendMail(&err_msg,subject,body,from_addr,to_addrs,cc_addrs,bcc_addrs)) {
fprintf(stderr,"%s\n",err_msg.toUtf8().constData());
exit(256);
}
exit(RDApplication::ExitOk);
}
int main(int argc,char *argv[])
{
QApplication a(argc,argv,false);
new MainObject();
return a.exec();
}

35
tests/sendmail_test.h Normal file
View File

@ -0,0 +1,35 @@
// sendmail_test.h
//
// Test the Rivendell email sending routines.
//
// (C) Copyright 2020 Fred Gleason <fredg@paravelsystems.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef SENDMAIL_TEST_H
#define SENDMAIL_TEST_H
#include <qobject.h>
#define SENDMAIL_TEST_USAGE "[options]\n\nTest the Rivendell email sending routines\n\nOptions are:\n--from-address=<addr>\n Originating email address\n\n--to-addresses=<addrs>\n To addresses (comma seperated)\n\n--cc-addresses=<addrs>\n CC addresses (comma seperated)\n\n--bcc-addresses=<addrs>\n BCC addresses (comma seperated)\n\n--subject=<str>\n Message subject\n\n--body=<str>\n Message body\n\n"
class MainObject : public QObject
{
public:
MainObject(QObject *parent=0);
};
#endif // SENDMAIL_TEST_H