<%@page import="Base64,java.util.*,java.io.*,javax.mail.*,javax.mail.internet.*;"%> <%@include file="MailListConfigs_basic.jsp"%> <% //#File: MailList_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: //# 0 - Print subscribe form. This is done by default. //# 1 - Mail confirmation subscribe email to the user requesting to sign up. //# 2 - Subscribe to the list if the user clicks on the link within their confirmation email. //# //#REQUIREMENTS: //# 1) JSP Java API >= 1.3.1 //# 2) You must specify an SMTP server this script will have access to. //# //#INSTALLATION: //# 1) Set the SCRIPT_URL and ADMIN_SCRIPT_URL values located in the "MailListConfigs_basic.jsp" //# file. //# 2) Make sure the file "MailListConfigs_basic.jsp" has appropriate read permissions. //# 3) Complete the "installation" section of the file "MailListAdmin_basic.jsp". //# 4) Install and compile the java class file "Base64.java". //# //#RUNNING: //# 1) The link to print the "Subscribe" form to your users is: //# http://www.yourdomain.com/scripts/MailList_advanced.jsp? //# //#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 program resides 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 //# //################################ //#NOTHING BELOW THIS LINE SHOULD NEED TO BE CONFIGURED //################################ int a = 0; try { if (request.getParameter("a") == null) { a = 0; } else { a = Integer.parseInt(request.getParameter("a")); } } catch (NumberFormatException e) { a = 0; } String e = (request.getParameter("e") == null ? "" : request.getParameter("e")); String l = (request.getParameter("l") == null ? "" : request.getParameter("l")); 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 subject = ""; String msg_body = ""; String line = ""; BufferedWriter fw = null; File file = null; BufferedReader br = null; 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))); Properties props = new Properties(); Session s = Session.getDefaultInstance(props,null); props.put("mail.smtp.host",MAIL_HOST); MimeMessage message = null; Address addr = null; boolean found = false; String output = ""; String Title_String = "Mailing List "; //#0 - Print subscribe form if (a>2 || a<0) { //#1 - Mail confirmation subscribe email a = 0; //#2 - Subscribe to list } if (l.equals("")) { AnError = true; ErrorMessage = "

An error occurred. You are not accessing this script with the correct URL Query String. See the documentation for more details.

\n"; } else if ((a==1 || a==2) && (e.equals(""))) { AnError = true; ErrorMessage = "

You need to provide your email address.

\n"; } else if ((a==1) && ! IsValidEmailAddress(e)) { AnError = true; ErrorMessage = "

You need to provide a valid email address.

\n"; } else if ((a==2) && ! IsValidEmailAddress((String)Base64.decodeToObject(e))) { AnError = true; ErrorMessage = "

You need to provide a valid email address.

\n"; } if (!AnError && !l.equals("")) { 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); } else { if (a == 1) { //#Mail the confirmation subscribe or unsubscribe email msg_body += "" + subject + "
\n"; msg_body += "You are receiving this email because your email address was submitted to "; msg_body += "subscribe to: "; msg_body += "\n\"" + Title + "\".\n

To confirm, click on the link below. "; msg_body += "if you cannot, copy and paste the URL into your browser. Thank you!

\n\n"; msg_body += "" + SCRIPT_URL + "?l=" + l + "&a=2&e=" + Base64.encodeObject(e); msg_body += "

\n\n\n"; subject = "Confirmation Email: " + Title; message = new MimeMessage(s); message.setContent(msg_body,"text/html"); message.setSubject(subject); addr = new InternetAddress(Admin_Email); message.setFrom(addr); addr = new InternetAddress(e); message.addRecipient(Message.RecipientType.TO,addr); try { Transport.send(message); out.print ("

Thank you! A confirmation email has been sent to \"" + e + "\".

\n"); out.print ("You need to click on the URL (or copy and paste it into your browser) to confirm your selection.

\n"); } catch (MessagingException me) { out.print ("

An error occurred and a confirmation email could not be sent to \"" + (String)Base64.decodeToObject(e) + "\".

\n"); out.print ("If the problem persists, please contact " + Admin_Email + ". Thank you!

\n"); } } else if (a == 2) { //# Subscribe to the list boolean error = false; e = new String((String)Base64.decodeToObject(e)); try { file = new File(Mail_List_Data_File); try { if (file.length() > 0) { br = new BufferedReader(new FileReader(file)); while (!found && (line = br.readLine()) != null) { if (!line.equals("")) { if (line.substring(0,line.indexOf("|")).equals(e)) { found = true; } } } } else { found = false; } } catch (IOException ioe) { 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"); error = true; //###So we don't go into the appending section } } catch (FileNotFoundException fnfe) { found = false; } if (!error) { if ( !found ) { try { fw = new BufferedWriter(new FileWriter(Mail_List_Data_File,true)); fw.write(e + "|"); fw.write(dateString); fw.newLine(); fw.close(); if (Email_Admin_With_List_Update) { EmailAdmin(dateString, a, e, Admin_Email, Title, MAIL_HOST, out); } out.print ("

Thank you! You are now subscribed to: " + Title + "

\n"); if (Mail_Subscribe_Thank_You_Letter) { msg_body = "Thank you for subscribing to " + Title + "!\n

\n"; msg_body += "If you wish to unsubscribe, copy and paste the following URL into your browser:\n

\n"; msg_body += SCRIPT_URL + "?l=" + l + "&a=5&e=" + Base64.encodeObject(e); subject = "Thank you for subscribing to: " + Title; message = new MimeMessage(s); message.setSubject(subject); addr = new InternetAddress(Admin_Email); message.setFrom(addr); addr = new InternetAddress(e); message.addRecipient(Message.RecipientType.TO,addr); message.setContent(msg_body,"text/html"); try { Transport.send(message); } catch (MessagingException me) { out.print ("

An error occurred and a confirmation email could not be sent to \"" + e + "\".

\n"); out.print ("If the problem persists, please contact " + Admin_Email + ". Thank you!

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

An error occurred, and your confirmation could not be saved. Please try again. \n"); out.print ("If you continue to experience problems, please email " + Admin_Email + ". Thank you!

\n"); } } else { //#already in the list out.print("

You have already subscribed to \"" + Title + "\".

\n"); out.print("Click here if you wish to unsubscribe.

\n"); } } } else { //#Print the subscribe form by default %>

<%=Title%> : Subscribe

Enter your email address below. A confirmation email will be sent to your email address.



<% } } %>
<%! //################################ //#boolean IsValidEmailAddress //# Checks to make sure a user submitted an email address //#in a valid format. //################################ boolean IsValidEmailAddress (String theAddress) { boolean valid = false; // there must be >= 1 character before @, so we // start looking at character position 1 // (i.e. second character) int i = 1; int aLength = theAddress.length(); // look for @ while ((i < aLength) && (theAddress.charAt(i) != '@')) { i++; } if ((i >= aLength) || (theAddress.charAt(i) != '@')) { valid = false; } else { i += 2; // look for . } while ((i < aLength) && (theAddress.charAt(i) != '.')) { i++; } // there must be at least one character after the . if ((i >= aLength - 1) || (theAddress.charAt(i) != '.')) { valid = false; } else { valid = true; } return (valid); } //### IsValidEmailAddress //################################ //#function EmailAdmin //# Sends an email to the address specified by $ADMIN_EMAIL //#everytime someone confirms to subscribe or unsubscribe //#from the mailing list. This function is only called if //#EMAIL_ADMIN_WITH_LIST_UPDATE is set to 1 (yes). //################################ void EmailAdmin (String lt, int a, String e, String Admin_Email, String Title, String MAIL_HOST, JspWriter out) throws IOException { try { String msg_body = ""; Properties props = new Properties(); Session s = Session.getDefaultInstance(props,null); props.put("mail.smtp.host",MAIL_HOST); MimeMessage message; Address addr = new InternetAddress(Admin_Email); msg_body = "\"" + e + "\""; msg_body += " subscribed to \"" + Title + "\" on: "; msg_body += lt + "\n"; message = new MimeMessage(s); message.setSubject("Mail List Update for: " + Title); message.setFrom(addr); message.addRecipient(Message.RecipientType.TO,addr); message.setContent(msg_body,"text/html"); Transport.send(message); } catch (javax.mail.MessagingException me) { out.print ("

An error occurred. Please notify " + Admin_Email + ". Thank you!

\n" + me); } } //################################ %>