changeset 1589:0c46edec25dd

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 13 Mar 2021 21:02:38 -0700
parents 0b904d30721f
children bce893009f90
files conv.txt src/luan/modules/mail/Mail.luan src/luan/modules/mail/MailCon.java src/luan/modules/mail/MailSender.java website/src/examples/upload-and-email.html.luan
diffstat 5 files changed, 141 insertions(+), 121 deletions(-) [+]
line wrap: on
line diff
--- a/conv.txt	Fri Mar 12 23:47:17 2021 -0700
+++ b/conv.txt	Sat Mar 13 21:02:38 2021 -0700
@@ -1,3 +1,5 @@
+Mail.Sender
+
 Thread.global_callable
 
 Luan.try
--- a/src/luan/modules/mail/Mail.luan	Fri Mar 12 23:47:17 2021 -0700
+++ b/src/luan/modules/mail/Mail.luan	Sat Mar 13 21:02:38 2021 -0700
@@ -3,13 +3,13 @@
 local error = Luan.error
 local type = Luan.type or error()
 local System = require "java:java.lang.System"
---local SmtpCon = require "java:luan.modules.mail.SmtpCon"
-local SmtpCon = require "java:luan.modules.mail.MailCon"
+local SmtpCon = require "java:luan.modules.mail.SmtpCon"
+local MailSender = require "java:luan.modules.mail.MailSender"
 
 
 local Mail = {}
 
---System.setProperty( "mail.mime.charset", "UTF-8" )
+System.setProperty( "mail.mime.charset", "UTF-8" )
 
 function Mail.Sender(params)
 	type(params)=="table" or error()
@@ -17,4 +17,10 @@
 	return { send = smtpCon.send }
 end
 
+function Mail.sender(params)
+	type(params)=="table" or error()
+	local mailSender = MailSender.new(params)
+	return { send = mailSender.send }
+end
+
 return Mail
--- a/src/luan/modules/mail/MailCon.java	Fri Mar 12 23:47:17 2021 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,112 +0,0 @@
-package luan.modules.mail;
-
-import java.io.IOException;
-import java.net.Socket;
-import java.util.Map;
-import java.util.LinkedHashMap;
-import java.util.List;
-import goodjava.mail.Message;
-import goodjava.mail.Smtp;
-import goodjava.mail.MailException;
-import luan.Luan;
-import luan.LuanTable;
-import luan.LuanException;
-
-
-public final class MailCon {
-	private final String host;
-	private final int port;
-	private final String username;
-	private final String password;
-
-	public MailCon(LuanTable paramsTbl) throws LuanException {
-		Map<Object,Object> params = paramsTbl.asMap();
-
-		String host = getString(params,"host");
-		if( host==null )
-			throw new LuanException( "parameter 'host' is required" );
-		this.host = host;
-
-		Object port = params.remove("port");
-		if( port==null )
-			throw new LuanException( "parameter 'port' is required" );
-		if( !(port instanceof Number) )
-			throw new LuanException( "parameter 'port' must be an integer" );
-		Integer i = Luan.asInteger(port);
-		if( i == null )
-			throw new LuanException( "parameter 'port' must be an integer" );
-		this.port = i;
-
-		this.username = getString(params,"username");
-		this.password = getString(params,"password");
-		if( this.username!=null && this.password==null )
-			throw new LuanException( "password required with username" );
-		if( this.username==null && this.password!=null )
-			throw new LuanException( "username required with password" );
-
-		if( !params.isEmpty() )
-			throw new LuanException( "unrecognized parameters: "+params );
-	}
-
-	private static String getString(Map<Object,Object> params,String key) throws LuanException {
-		Object val = params.remove(key);
-		if( val!=null && !(val instanceof String) )
-			throw new LuanException( "parameter '"+key+"' must be a string" );
-		return (String)val;
-	}
-
-	private static Message message(LuanTable mailTbl) throws LuanException {
-		Map<Object,Object> mailParams = mailTbl.asMap();
-		Object body = mailParams.remove("body");
-		if( body == null )
-			throw new LuanException( "parameter 'body' is required" );
-		Map<String,String> headers = new LinkedHashMap<String,String>();
-		boolean hasContentType = false;
-		for( Map.Entry<Object,Object> entry : mailParams.entrySet() ) {
-			Object key = entry.getKey();
-			Object value = entry.getValue();
-			if( !(key instanceof String) )
-				throw new LuanException( "keys must be strings" );
-			if( !(value instanceof String) )
-				throw new LuanException( "value for '"+key+"' must be string" );
-			String name = (String)key;
-			headers.put(name,(String)value);
-			if( name.equalsIgnoreCase("content-type") )
-				hasContentType = true;
-		}
-		if( body instanceof String ) {
-			if( !hasContentType )
-				headers.put("Content-Type","text/plain; charset=utf-8");
-			return new Message(headers,(String)body);
-		}
-		if( body instanceof byte[] ) {
-			return new Message(headers,(byte[])body);
-		}
-		if( body instanceof LuanTable ) {
-			LuanTable tbl = (LuanTable)body;
-			if( !tbl.isList() )
-				throw new LuanException( "body table must be a list" );
-			List list = tbl.asList();
-			Message[] msgs = new Message[list.size()];
-			for( int i=0; i<msgs.length; i++ ) {
-				Object obj = list.get(i);
-				if( !(obj instanceof LuanTable) )
-					throw new LuanException( "body table must be a list of part tables" );
-				msgs[i] = message((LuanTable)obj);
-			}
-			return new Message(headers,msgs);
-		}
-		throw new LuanException("body must be a string, binary, or list of part tables");
-	}
-
-	public void send(LuanTable mailTbl) throws LuanException, IOException, MailException {
-		Message msg = message(mailTbl);
-		Socket socket = new Socket(host,port);
-		Smtp smtp = new Smtp(socket);
-		if( username != null )
-			smtp.authenticate(username,password);
-		smtp.send(msg);
-		smtp.close();
-	}
-
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/mail/MailSender.java	Sat Mar 13 21:02:38 2021 -0700
@@ -0,0 +1,116 @@
+package luan.modules.mail;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.util.Map;
+import java.util.LinkedHashMap;
+import java.util.List;
+import goodjava.mail.Message;
+import goodjava.mail.Smtp;
+import goodjava.mail.MailException;
+import luan.Luan;
+import luan.LuanTable;
+import luan.LuanException;
+
+
+public final class MailSender {
+	private final String host;
+	private final int port;
+	private final String username;
+	private final String password;
+
+	public MailSender(LuanTable paramsTbl) throws LuanException {
+		Map<Object,Object> params = paramsTbl.asMap();
+
+		String host = getString(params,"host");
+		if( host==null )
+			throw new LuanException( "parameter 'host' is required" );
+		this.host = host;
+
+		Object port = params.remove("port");
+		if( port==null )
+			throw new LuanException( "parameter 'port' is required" );
+		if( !(port instanceof Number) )
+			throw new LuanException( "parameter 'port' must be an integer" );
+		Integer i = Luan.asInteger(port);
+		if( i == null )
+			throw new LuanException( "parameter 'port' must be an integer" );
+		this.port = i;
+
+		this.username = getString(params,"username");
+		this.password = getString(params,"password");
+		if( this.username!=null && this.password==null )
+			throw new LuanException( "password required with username" );
+		if( this.username==null && this.password!=null )
+			throw new LuanException( "username required with password" );
+
+		if( !params.isEmpty() )
+			throw new LuanException( "unrecognized parameters: "+params );
+	}
+
+	private static String getString(Map<Object,Object> params,String key) throws LuanException {
+		Object val = params.remove(key);
+		if( val!=null && !(val instanceof String) )
+			throw new LuanException( "parameter '"+key+"' must be a string" );
+		return (String)val;
+	}
+
+	private static Message message(LuanTable mailTbl) throws LuanException {
+		Map<Object,Object> mailParams = mailTbl.asMap();
+		Object body = mailParams.remove("body");
+		if( body == null )
+			throw new LuanException( "parameter 'body' is required" );
+		Map<String,String> headers = new LinkedHashMap<String,String>();
+		boolean hasContentType = false;
+		for( Map.Entry<Object,Object> entry : mailParams.entrySet() ) {
+			Object key = entry.getKey();
+			Object value = entry.getValue();
+			if( !(key instanceof String) )
+				throw new LuanException( "keys must be strings" );
+			if( !(value instanceof String) )
+				throw new LuanException( "value for '"+key+"' must be string" );
+			String name = (String)key;
+			headers.put(name,(String)value);
+			if( name.equalsIgnoreCase("content-type") )
+				hasContentType = true;
+		}
+		if( body instanceof String ) {
+			if( !hasContentType )
+				headers.put("Content-Type","text/plain; charset=utf-8");
+			return new Message(headers,(String)body);
+		}
+		if( body instanceof byte[] ) {
+			if( !hasContentType )
+				throw new LuanException("Content-Type required for binary body");
+			return new Message(headers,(byte[])body);
+		}
+		if( body instanceof LuanTable ) {
+			if( !hasContentType )
+				headers.put("Content-Type","multipart/mixed");
+			LuanTable tbl = (LuanTable)body;
+			if( !tbl.isList() )
+				throw new LuanException( "body table must be a list" );
+			List list = tbl.asList();
+			Message[] msgs = new Message[list.size()];
+			for( int i=0; i<msgs.length; i++ ) {
+				Object obj = list.get(i);
+				if( !(obj instanceof LuanTable) )
+					throw new LuanException( "body table must be a list of part tables" );
+				msgs[i] = message((LuanTable)obj);
+			}
+			return new Message(headers,msgs);
+		}
+		throw new LuanException("body must be a string, binary, or list of part tables");
+	}
+
+	public void send(LuanTable mailTbl) throws LuanException, IOException, MailException {
+		Message msg = message(mailTbl);
+		Socket socket = new Socket(host,port);
+		Smtp smtp = new Smtp(socket);
+		if( username != null )
+			smtp.authenticate(username,password);
+		smtp.send(msg);
+		smtp.close();
+	}
+
+}
--- a/website/src/examples/upload-and-email.html.luan	Fri Mar 12 23:47:17 2021 -0700
+++ b/website/src/examples/upload-and-email.html.luan	Sat Mar 13 21:02:38 2021 -0700
@@ -3,7 +3,7 @@
 local Mail = require "luan:mail/Mail.luan"
 
 
-local send = Mail.Sender{
+local send = Mail.sender{
 	host = "smtpcorp.com"
 	username = "smtp@luan.software"
 	password = "luanhost"
@@ -46,11 +46,19 @@
 	else
 		local file = Http.request.parameters.file
 		send{
-			from = "smtp@luan.software"
-			to = email
-			subject = "Upload and Email"
-			body = "file should be attached"
-			attachments = {file}
+			From = "smtp@luan.software"
+			To = email
+			Subject = "Upload and Email"
+			body = {
+				{
+					body = "file should be attached"
+				}
+				{
+					["Content-Type"] = file.content_type
+					["Content-Disposition"] = [[attachment; filename="]]..file.filename..[["]]
+					body = file.content
+				}
+			}
 		}
 		sent()
 	end