<%@page import="Base64,java.util.*,java.io.*,javax.mail.*,javax.mail.internet.*,javax.servlet.http.*;"%> <%@include file="MailListConfigs_basic.jsp"%> <% //#File: MailListAdmin_basic.jsp - Version 3.00 //# //# This program was written to support basic mailing-list functionality //#without being dependent on an actual email alias for the mailing list. This //#increases the portability while also significantly reducing the possibilitiy //#of spammers spamming members. //# //# This particular file will do one of the following depending on the //#parameters passed to it: //# a is the value for "a" in the query string, which can be any of the following: //# 0 - Print the Configurations for the current list //# 1 - Print the email form to email a message to members of the current list //# 2 - Mail the message to current list //# 8 - Print the members of the current list //# 9 - View all the currently configured Mailing Lists //# //#REQUIREMENTS: //# 1) JSP Java API >= 1.3.1 //# 2) You must specify an SMTP server this script has access to. //# //#INSTALLATION: //# 1) If you changed the name of MailListConfigs_basic.jsp, you need to change //# the value in the final String MAIL_LIST_CONFIG_FILE below. //# 2) If you changed the name of MailList_basic.jsp, you need to change //# the value in the final String MAIL_LIST_ADVANCED_FILE below. //# 3) Set the values located in the "MailListConfigs_basic.jsp" //# file. ALL VALUES SHOULD BE ENCLOSED IN QUOTES. //# 4) Make sure the file "MailListConfigs_basic.jsp" has appropriate read permissions. //# 5) Install and compile the java class file "Base64.java". //# //#RUNNING: //# Call this file through a URL similar to the following: //# http://www.yourdomain.com/scripts/MailListAdmin_basic.jsp?a=9 //# //# All administrative functionality is accessible from the buttons //#on the left hand side. //# //#ADDITIONAL FEATURES AVAILABLE IN THE ADVANCED VERSION: //# 1) Print the Add form to add an email to the current list //# 2) Adding an email to the current list directly, bypassing any //# confirmation emails //# 3) Removing an email from the current list directly, bypassing any //# confirmation emails //# 4) Removing several emails from the current list directly, bypassing //# any confirmation emails //# 5) Displaying the Remove the form to remove an email from the current list //# 6) Web interface to update current list settings //# 7) Web interface to Create a new list //# 8) Web interface to Delete a list //# 9) Web interface to Update URLs //# 10) Support for MULTIPLE lists //# 11) Exporting mail list data to Excel, Word, and a text editor //# 12) Sorting member data by email address or date joined //# //#LICENSE: //# You are free to further develop/modify the source code to fit your //#needs, but you MAY NOT resell and/or redistribute the source code in any //#way, manner, fashion, or form without my hand-written consent! //# //# This license grants the purchaser of this archive the permission to //#have an unlimited number of users use this script at any time. //#Likewise, the purchaser may have any number of copies of this script as long //#as the media that this programs is stored on or run from is owned by the purchaser. //#If not, more licenses MUST be purchased. //#Examples: //# 1) If you have your own webserver hosting your various customers, you only need once license //#to run the code on your webserver to provide the email functionality to your clients hosted on your server. //# 2) If you are designing websites for 3 individual clients, each of which wants the email functionality, //#and each of which will be hosting their websites and emailer on their own server, //#then you need to purchase 3 licenses. //# //#If you modify the source code in anyway for your own purposes, the //#original header-comments must remain intact. //# //#REVISION HISTORY: //# 3.00 07/31/04 Original Version //# //#CONTACT INFORMATION: //# http://www.davelozinski.com/cgi-bin/email_lozinski.pl //# final String MAIL_LIST_CONFIG_FILE = application.getRealPath("./MailListConfigs_basic.jsp"); final String MAIL_LIST_ADVANCED_FILE = application.getRealPath("./MailList_basic.jsp"); //################################ //#NOTHING BELOW THIS LINE SHOULD NEED TO BE CONFIGURED //################################ //#Done to distinguish between a Windows OS and other OS. Windows requires //#a \r\n for mail and to write to files. Other OS's only require the \n. final String NEWLINE = (System.getProperty("os.name").toUpperCase().indexOf("WINDOW") >= 0 ? "\r\n" : "\n"); int a = 0; try { if (request.getParameter("a") == null) { a = 0; } else { a = Integer.parseInt(request.getParameter("a")); } } catch (NumberFormatException nfe1) { a = 0; } String e = (request.getParameter("e") == null ? "" : request.getParameter("e")); String l = (request.getParameter("l") == null ? "" : request.getParameter("l")); String from = (request.getParameter("from") == null ? "" : request.getParameter("from")); String message = (request.getParameter("message") == null ? "" : request.getParameter("message")); String subject = (request.getParameter("subject") == null ? "" : request.getParameter("subject")); boolean AnError = false; String ErrorMessage = ""; String Title = ""; String Admin_Email = ""; boolean Email_Admin_With_List_Update = false; boolean Mail_Subscribe_Thank_You_Letter = false; boolean Send_HTML_Format = false; String Mail_List_Data_File = ""; String msg_body = ""; GregorianCalendar cal = new GregorianCalendar(); cal.setTime(new Date()); String dateString = cal.get(Calendar.YEAR) + "-" + ((cal.get(Calendar.MONTH) + 1) < 10 ? ("0" + String.valueOf(cal.get(Calendar.MONTH) + 1)) : String.valueOf(cal.get(Calendar.MONTH) + 1)) + "-" + (cal.get(Calendar.DATE) < 10 ? "0" + String.valueOf(cal.get(Calendar.DATE)) : String.valueOf(cal.get(Calendar.DATE))); //Used for reverse-sorting by value instead of key ArrayList entries = null; Properties props = new Properties(); Session s = Session.getDefaultInstance(props,null); props.put("mail.smtp.host",MAIL_HOST); MimeMessage mimeMessage = null; Address addr = null; String line = ""; PrintWriter fw = null; File file = null; BufferedReader br = null; boolean error = false; boolean found = false; String email_address = ""; String date = ""; String output = ""; String Title_String = "Mailing List "; Title = C_TITLE; Admin_Email = C_ADMIN_EMAIL; Email_Admin_With_List_Update = C_EMAIL_ADMIN_WITH_LIST_UPDATE.equals("1") ? true : false; Mail_Subscribe_Thank_You_Letter = C_MAIL_SUBSCRIBE_THANK_YOU_LETTER.equals("1") ? true : false; Send_HTML_Format = C_SEND_HTML_FORMAT.equals("1") ? true : false; Mail_List_Data_File = C_MAIL_LIST_DATA_FILE; Title_String += "for: \"" + Title + "\"\n"; %> <%=Title_String%> <% if (AnError) { out.print("

" + ErrorMessage + "

\n"); } else { //###No error %>

Mail List Administrative Interface <% if (!l.equals("")) { out.print ("for: \"" + Title + "\"\n"); } %>


<% if (!l.equals("")) { //#Print the left hand column of buttons. %>



<% } //#Always print the following buttons %>

<% if (a == 0) { //#Print the configurations for the current list. %>

Current Configurations for: <%=Title%>

Mail List Abbreviation:N/A
URL for Members to add themselves to this list:<%=SCRIPT_URL%>?l=Test_List
URL for Members to remove themselves to this list:Available in the advanced version
Mail List Title:
The full title for this list.
Email a "thank you" message to each new subscriber?:
Selecting "yes" will have the program email the new subscriber a "thank you" message for joining "<%=Title%>".
Administrator's Email:
Email Admin with each list update?:
Selecting "yes" will have the program email the administrator everytime someone confirms their addition or removal from this list.
Send HTML formatted messages by default?:
Datafile for this mail list:
Full operating system path, including the file name.
NO BACKSLASHES "\"!
Examples:
F:/Inetpub/wwwroot/scripts/maillist/listdata.txt
/var/home/public_html/somedir/datafile.txt
 
<% } else if (a == 1) { //#Print the Email form to mail a message to the members. try { file = new File(Mail_List_Data_File); if (file.length() > 0) { %>

Email members of: <%=Title%>

From:

Reply To:

Subject:

Message:


Send as: <% out.print("Text  "); out.print("HTML  "); %> [Preview]


<% } else { //###Empty or non-existent file out.print ("

There are no members currently subscribed to the mail list \"" + Title + "\".

\n"); } } catch (FileNotFoundException fnfe) { out.print ("

There are no members currently subscribed to the mail list \"" + Title + "\".

\n"); } } else if (a == 2) { //#Mail the message to the members on the list. if (subject.equals("")) { out.print ("

You need to provide a subject for your email.

\n"); } else if (from.equals("")) { out.print ("

You need to specify a \"from\" address for your email.

\n"); } else { try { file = new File(Mail_List_Data_File); ArrayList successes = new ArrayList(); ArrayList failures = new ArrayList(); int successes_count = 0; int failures_count = 0; try { if (file.length() > 0) { br = new BufferedReader(new FileReader(file)); while ((line = br.readLine()) != null) { if (!line.equals("")) { email_address = line.substring(0,line.indexOf("|")); date = line.substring(line.indexOf("|"),line.length()); msg_body = ""; if (!Send_HTML_Format) { msg_body += "" + Title + "\n"; } msg_body += message + "\n"; if (!Send_HTML_Format) { msg_body += "

"; } msg_body += "\n\nYou are receiving this email because you subscribed to: " + Title + "\n\n"; if (!Send_HTML_Format) { msg_body += "

"; } msg_body += "To unsubscribe, click on the link below. if you cannot click on the link, copy and paste it into your browser window.\n\n"; if (!Send_HTML_Format) { msg_body += "

"; } msg_body += SCRIPT_URL + "?l=" + l + "&a=5&e=" + Base64.encodeObject(email_address); if (!Send_HTML_Format) { msg_body += "

"; } msg_body += "\n\n"; mimeMessage = new MimeMessage(s); mimeMessage.setSubject(subject); addr = new InternetAddress(from); mimeMessage.setFrom(addr); addr = new InternetAddress(email_address); mimeMessage.addRecipient(Message.RecipientType.TO,addr); if (!Send_HTML_Format) { mimeMessage.setContent(msg_body,"text/html"); } else { mimeMessage.setContent(msg_body,"text/plain"); } try { Transport.send(mimeMessage); successes.add(email_address); successes_count += 1; } catch (MessagingException me) { failures.add(email_address); failures_count += 1; } } //!line.equals("") } //while br.close(); br = null; } //if file.length>0 if (failures.size() > 0 && successes.size() > 0) { out.print ("Click to view members who could not be emailed.
\n"); out.print ("Click to view members successfully emailed
\n"); out.print ("


\n"); } if (failures.size() > 0) { out.print (" \n"); out.print ("

The following " + failures.size() + " members were not emailed:

\n"); for (failures_count=0; failures_count < failures.size(); failures_count++) { out.print (failures.get(failures_count) + "
\n"); } } if (successes.size() > 0) { out.print (" \n"); out.print ("

Successfully sent an email to these " + successes.size() + " member(s):

\n"); for (successes_count=0; successes_count < successes.size(); successes_count++) { out.print (successes.get(successes_count) + "
\n"); } } else { out.print ("

No members were successfully emailed.

\n"); } } catch (IOException ioe) { out.print ("

Error! The file \"" + Mail_List_Data_File + "\" containing the members of: " + C_TITLE + " could not be read!

\n"); } } catch (FileNotFoundException fnfe) { out.print ("

Error! There are no members currently subscribed to the mail list \"" + Title + "\"!

\n"); } } //subject.equals("") } else if (a == 8) { //#Print the current list of members. try { HashMap members = new HashMap(); file = new File(Mail_List_Data_File); try { if (file.length() > 0) { %> <% br = new BufferedReader(new FileReader(file)); while ((line = br.readLine()) != null) { if (! line.equals("")) { members.put( line.substring(0,line.indexOf("|")) , line.substring(line.indexOf("|") + 1,line.length()) ); } } entries = new ArrayList(members.entrySet()); for (Iterator i = entries.iterator(); i.hasNext(); ) { Map.Entry mapentry = (Map.Entry) i.next(); out.print ("\t\n"); out.print ("\t\n"); out.print ("\t\n"); out.print ("\t\n"); out.print ("\t\n"); } out.print ("\t\n"); out.print ("\t

<%=Title%> Members:

[export]

Display:
Remove?Email AddressDate Joined
" + mapentry.getKey() + "" + mapentry.getValue() + "
  
\n\t\n"); members = null; } else { out.print ("

The mail list \"" + Title + "\" currently has no members.

\n"); } // if file.length() > 0 file = null; members = null; } catch (IOException ioe2) { out.print ("

An error occurred. The user data file could not be opened for reading. \n"); out.print ("If you continue to experience problems, please email " + Admin_Email + ". Thank you!

\n"); } } catch (FileNotFoundException fnfe) { out.print ("

Error. Could not open the datafile \"" + Mail_List_Data_File + "\" for reading.

\n"); out.print ("

If the mail list \"" + Title + "\" currently has no members, this error can be ignored.

\n"); } } else if (a == 9) { //#View all current lists %> <% out.print ("\n"); out.print ("\n"); out.print ("\n"); out.print ("\n"); try { file = new File(C_MAIL_LIST_DATA_FILE); if (file.length() > 0) { out.print ("\n"); } else { out.print ("\n"); } file = null; } catch (FileNotFoundException fnfe) { out.print ("\n"); } out.print ("\n"); out.print ("

All Mail Lists. Click on a list for specifics.

List AbbreviationList Title 
Basic Test List" + Title + "[Update][Members]No MembersNo Members
\n"); } %>
<% } //###!AnError %>