dmdb/model/FormMail.cs

120 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Text;
namespace dezentrale.model
{
public class FormMail : XmlData
{
public string To { get; set; } = "{Nickname} <{EMail}>";
public string Subject { get; set; }
public string Body { get; set; }
public FormMail() { }
public FormMail(FormMail copyFrom)
{
if(copyFrom != null)
{
this.To = copyFrom.To;
this.Subject = copyFrom.Subject;
this.Body = copyFrom.Body;
}
}
public FormMail ReplaceReflect(object o)
{
FormMail ret = new FormMail(this);
PropertyInfo[] objProperties = o.GetType().GetProperties();
foreach (var mailProperty in ret.GetType().GetProperties())
{
if (mailProperty.PropertyType != typeof(string)) continue;
if (!mailProperty.CanRead) continue;
if (!mailProperty.CanWrite) continue;
string propVal = (string)mailProperty.GetValue(ret, null);
if (propVal == null) continue;
bool changed = false;
foreach (var objProperty in objProperties)
{
if (!objProperty.CanRead) continue;
//check if objProperty occurs in mailProperty contents, then replace
string token = $"{{{objProperty.Name}}}";
string tokenValue = objProperty.GetValue(o, null).ToString();
if (propVal.Contains(token))
{
//NOTE: This is problematic because it allows the user to generate recursive replacements by setting e.g. the Nickname to "{PgpFingerprint}"
propVal = propVal.Replace(token, tokenValue);
changed = true;
}
}
if (changed)
{
mailProperty.SetValue(ret, propVal, null);
}
}
return ret;
}
public bool Send(object replaceReflect = null)
{
FormMail src = replaceReflect == null ? this : ReplaceReflect(replaceReflect);
SmtpClient client = new SmtpClient(Program.config.Smtp.Host, Program.config.Smtp.Port);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = Program.config.Smtp.SSL;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(Program.config.Smtp.UserName, Program.config.Smtp.Password);
try
{
client.Send(Program.config.Smtp.From, src.To, src.Subject, src.Body);
} catch(Exception ex)
{
Console.WriteLine($"Error while sending Mail:\n{ex.Message}\n");
return false;
}
return true;
}
public static FormMail GenerateNewMemberWelcome()
{
FormMail ret = new FormMail();
ret.To = "{Nickname} <{EMail}>";
ret.Subject = "Willkommen in der dezentrale, {Nickname}!";
ret.Body = "Hello {Nickname}!\n"
+ "\n"
+ "Welcome to the dezentrale Hackspace.\n"
+ "Your membership number is {Number}\n";
return ret;
}
public static FormMail GenerateNewMemberVorstand()
{
FormMail ret = new FormMail();
ret.To = "{VSName} <{VSEMail}>";
ret.Subject = "Neuer Mitgliedsantrag, Nummer = {Number}, nick = {Nickname}!";
ret.Body = "Hallo Vorstandsliste,!\n"
+ "\n"
+ "Folgender Nutzer wurde in die Datenbank aufgenommen.\n"
+ "Mitgliednummer: {Number}\n"
+ "Nickname: {Nickname}\n"
+ "Realname: {FirstName} {LastName}\n"
+ "EMail: {EMail}\n"
+ "\n"
+ "Damit beginnt eine 7-Tage-Einspruchsfrist für den Vorstand.\n"
+ "\n"
+ "\n"
+ "Dies ist eine automatisch generierte E-Mail.\n";
return ret;
}
}
}