Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/erpnext/selling/doctype/lead/lead.txt b/erpnext/selling/doctype/lead/lead.txt
index a467f3a..056fc16 100644
--- a/erpnext/selling/doctype/lead/lead.txt
+++ b/erpnext/selling/doctype/lead/lead.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2012-06-05 20:03:20',
 		'docstatus': 0,
-		'modified': '2012-08-02 18:01:53',
+		'modified': '2012-08-03 10:49:22',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -686,5 +686,14 @@
 		'fieldtype': u'Check',
 		'label': u'Unsubscribed',
 		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'blog_subscriber',
+		'fieldtype': u'Check',
+		'label': u'Blog Subscriber',
+		'permlevel': 0
 	}
 ]
\ No newline at end of file
diff --git a/erpnext/startup/__init__.py b/erpnext/startup/__init__.py
index 3549898..cd443a0 100644
--- a/erpnext/startup/__init__.py
+++ b/erpnext/startup/__init__.py
@@ -15,9 +15,13 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # add startup propertes
-
-add_in_head = """
-<style>
-
-</style>
-"""
\ No newline at end of file
+mail_footer = """<div style="padding: 7px; text-align: right; color: #888"><small>Sent via 
+	<a style="color: #888" href="https://erpnext.com">ERPNext</a></div>"""
+	
+def get_monthly_bulk_mail_limit():
+	import webnotes
+	# if global settings, then 500 or unlimited
+	if webnotes.conn.get_value('Email Settings', None, 'outgoing_mail_server'):
+		return 999999
+	else:
+		return 500
\ No newline at end of file
diff --git a/erpnext/startup/schedule_handlers.py b/erpnext/startup/schedule_handlers.py
index 021cb1f..2df7a57 100644
--- a/erpnext/startup/schedule_handlers.py
+++ b/erpnext/startup/schedule_handlers.py
@@ -42,7 +42,7 @@
 	run_fn(send)
 
 	# send bulk emails
-	from webnotes.utils.email_lib.bulk import cleanup
+	from webnotes.utils.email_lib.bulk import clear_outbox
 	run_fn(clear_outbox)
 
 def execute_weekly():
diff --git a/erpnext/website/blog.py b/erpnext/website/blog.py
index 5692f51..ea1992b 100644
--- a/erpnext/website/blog.py
+++ b/erpnext/website/blog.py
@@ -106,6 +106,25 @@
 
 	return comment_html
 
+@webnotes.whitelist(allow_guest=True)
+def add_subscriber():
+	"""add blog subscriber to lead"""
+	full_name = webnotes.form_dict.get('your_name')
+	email = webnotes.form_dict.get('your_email_address')
+	name = webnotes.conn.sql("""select name from tabLead where email_id=%s""", email)
+	
+	from webnotes.model.doc import Document
+	if name:
+		lead = Document('Lead', name[0][0])
+	else:
+		lead = Document('Lead')
+		
+	lead.unsubscribed = 0
+	lead.blog_subscriber = 1
+	lead.lead_name = full_name
+	lead.email_id = email
+	lead.save()
+		
 def get_blog_content(blog_page_name):
 	import website.web_cache
 	content = website.web_cache.get_html(blog_page_name)
diff --git a/erpnext/website/doctype/blog/blog.js b/erpnext/website/doctype/blog/blog.js
new file mode 100644
index 0000000..b0c1ec2
--- /dev/null
+++ b/erpnext/website/doctype/blog/blog.js
@@ -0,0 +1,25 @@
+// ERPNext - web based ERP (http://erpnext.com)
+// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
+// 
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+// 
+// 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, see <http://www.gnu.org/licenses/>.
+
+cur_frm.cscript.refresh = function(doc) {
+	if(!doc.__islocal && doc.published && !doc.email_sent) {
+		cur_frm.add_custom_button('Email Subscribers', function() {
+			$c_obj(make_doclist(doc.doctype, doc.name), 'send_emails', '', function(r) {
+				cur_frm.refresh();
+			});
+		})
+	}
+}
\ No newline at end of file
diff --git a/erpnext/website/doctype/blog/blog.py b/erpnext/website/doctype/blog/blog.py
index e361ece..786941c 100644
--- a/erpnext/website/doctype/blog/blog.py
+++ b/erpnext/website/doctype/blog/blog.py
@@ -29,6 +29,31 @@
 		super(DocType, self).__init__('Blog')
 		self.doc, self.doclist = d, dl
 
+	def send_emails(self):
+		"""send emails to subscribers"""
+		if self.doc.email_sent:
+			webnotes.msgprint("""Blog Subscribers already updated""", raise_exception=1)
+		
+		from webnotes.utils.email_lib.bulk import send
+		from markdown2 import markdown
+		import webnotes.utils
+		
+		# get leads that are subscribed to the blog
+		recipients = [e[0] for e in webnotes.conn.sql("""select distinct email_id from tabLead where
+			ifnull(blog_subscriber,0)=1""")]
+
+		# make heading as link
+		content = '<h2><a href="%s/%s.html">%s</a></h2>\n\n%s' % (webnotes.utils.get_request_site_address(),
+			self.doc.page_name, self.doc.title, markdown(self.doc.content))
+
+		# send the blog
+		send(recipients = recipients, doctype='Lead', email_field='email_id',
+			first_name_field = 'lead_name', last_name_field="", subject=self.doc.title,
+			message = markdown(content))
+		
+		webnotes.conn.set(self.doc, 'email_sent', 1)
+		webnotes.msgprint("""Scheduled to send to %s subscribers""" % len(recipients))
+
 	def on_update(self):
 		super(DocType, self).on_update()
 		if not webnotes.utils.cint(self.doc.published):
diff --git a/erpnext/website/doctype/blog/blog.txt b/erpnext/website/doctype/blog/blog.txt
index 6ed7143..35d31c6 100644
--- a/erpnext/website/doctype/blog/blog.txt
+++ b/erpnext/website/doctype/blog/blog.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-07-13 13:02:27',
+		'creation': '2012-07-27 19:32:53',
 		'docstatus': 0,
-		'modified': '2012-07-27 14:15:24',
+		'modified': '2012-08-03 12:18:36',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -116,6 +116,16 @@
 	# DocField
 	{
 		'doctype': u'DocField',
+		'fieldname': u'email_sent',
+		'fieldtype': u'Check',
+		'hidden': 1,
+		'label': u'Email Sent',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
 		'fieldname': u'file_list',
 		'fieldtype': u'Text',
 		'hidden': 1,
diff --git a/erpnext/website/templates/html/blog_page.html b/erpnext/website/templates/html/blog_page.html
index 3b8348d..12a1c7a 100644
--- a/erpnext/website/templates/html/blog_page.html
+++ b/erpnext/website/templates/html/blog_page.html
@@ -2,6 +2,7 @@
 
 {% block javascript %}
 	{% include "js/blog_page.js" %}
+	{% include "js/blog_subscribe.js" %}
 {% endblock %}
 
 {% block css %}
@@ -41,11 +42,9 @@
 			<div class="layout-side-section">
 				<p><a href="blog.html">All Blogs</a></p>
 				<br />
-				<h4>Subscribe</h4>
-				<p>
-					<img src="images/feed.png" style="margin-right: 4px; margin-bottom: -4px">
-					<a href="rss.xml" target="_blank">RSS Feed</a>
-				</p>
+				{% block blog_subscribe %}
+					{% include "html/blog_subscribe.html" %}
+				{% endblock %}
 				<br />
 				<h4>Recent Posts</h4>
 				<div class="recent-posts" style="min-height: 100px;"></div>
diff --git a/erpnext/website/templates/html/blog_subscribe.html b/erpnext/website/templates/html/blog_subscribe.html
new file mode 100644
index 0000000..7a4fd49
--- /dev/null
+++ b/erpnext/website/templates/html/blog_subscribe.html
@@ -0,0 +1,9 @@
+<h4>Subscribe</h4>
+<br>
+<p>
+<button class="btn btn-warning btn-blog-subscribe">Get Updates via Email</button>
+</p>
+<p>
+<img src="images/feed.png" style="margin-right: 4px; margin-bottom: -4px">
+<a href="rss.xml" target="_blank">RSS Feed</a>
+</p>
\ No newline at end of file
diff --git a/erpnext/website/templates/js/blog_page.js b/erpnext/website/templates/js/blog_page.js
index 56dcf21..02d6dd5 100644
--- a/erpnext/website/templates/js/blog_page.js
+++ b/erpnext/website/templates/js/blog_page.js
@@ -59,8 +59,9 @@
 		hide_refresh: true,
 		render_row: function(parent, data) {
 			if(data.content && data.content.length>=100) data.content += '...';
-			parent.innerHTML = repl('<a href="%(page_name)s.html">%(title)s</a>\
-				<div class="comment">%(content)s</div><br>', data);
+			parent.innerHTML = repl('<div style="font-size: 80%">\
+				<a href="%(page_name)s.html">%(title)s</a>\
+				<div class="comment">%(content)s</div><br></div>', data);
 			
 			// adjust page height depending on sidebar height
 			erpnext.blog.adjust_page_height(wrapper);
diff --git a/erpnext/website/templates/js/blog_subscribe.js b/erpnext/website/templates/js/blog_subscribe.js
new file mode 100644
index 0000000..b3e10a7
--- /dev/null
+++ b/erpnext/website/templates/js/blog_subscribe.js
@@ -0,0 +1,33 @@
+wn.provide('erpnext.blog');
+
+(function() {
+	$('body').on('click', '.btn-blog-subscribe', function() {
+		var d = new wn.ui.Dialog({
+			title: "Get Blog Updates via Email",
+			fields: [
+				{label: "Your Name", fieldtype:"Data", reqd:1},
+				{label: "Your Email Address", fieldtype:"Data", reqd:1
+					,description: "You can unsubscribe anytime."},
+				{label: "Subscribe", fieldtype:"Button"}
+			]
+		});
+		$(d.fields_dict.subscribe.input).click(function() {
+			var args = d.get_values();
+			if(!args) return;
+			wn.call({
+				method: 'website.blog.add_subscriber',
+				args: args,
+				callback: function(r) {
+					if(r.exc) {
+						msgprint('Opps there seems to be some error, Please check back after some time.');
+					} else {
+						msgprint('Thanks for subscribing!');
+					}
+					d.hide();
+				},
+				btn: this
+			})
+		})
+		d.show()
+	})	
+})()
diff --git a/erpnext/website/templates/pages/blog.html b/erpnext/website/templates/pages/blog.html
index 40c90c2..17fd6e7 100644
--- a/erpnext/website/templates/pages/blog.html
+++ b/erpnext/website/templates/pages/blog.html
@@ -2,6 +2,7 @@
 
 {% block javascript %}
 	{% include "js/blog.js" %}
+	{% include "js/blog_subscribe.js" %}
 {% endblock %}
 
 {% block css %}
@@ -23,17 +24,9 @@
 			</div>
 			
 			<div class="layout-side-section">
-				<!-- for later
-				<h4>Get Updates</h4>
-				<p>
-				<input name="blog-subscribe">
-				<button class="btn" id="blog-subscribe">Subscribe</button>
-				</p>-->
-				<h4>Subscribe</h4>
-				<p>
-				<img src="images/feed.png" style="margin-right: 4px; margin-bottom: -4px">
-				<a href="rss.xml" target="_blank">RSS Feed</a>
-				</p>
+				{% block blog_subscribe %}
+					{% include "html/blog_subscribe.html" %}
+				{% endblock %}
 			</div>
 			<div style="clear: both"></div>
 		</div>
diff --git a/public/js/all-app.js b/public/js/all-app.js
index f7fc897..df69de6 100644
--- a/public/js/all-app.js
+++ b/public/js/all-app.js
@@ -870,7 +870,8 @@
  *	lib/js/wn/ui/dialog.js
  */
 wn.widgets.FieldGroup=function(){this.first_button=false;this.make_fields=function(body,fl){if(!window.make_field){wn.require('css/fields.css');wn.require('js/fields.js');}
-$y(this.body,{padding:'11px'});this.fields_dict={};for(var i=0;i<fl.length;i++){var df=fl[i];var div=$a(body,'div','',{margin:'6px 0px'})
+$y(this.body,{padding:'11px'});this.fields_dict={};for(var i=0;i<fl.length;i++){var df=fl[i];if(!df.fieldname&&df.label){df.fieldname=df.label.replace(/ /g,'_').toLowerCase();}
+var div=$a(body,'div','',{margin:'6px 0px'})
 f=make_field(df,null,div,null);f.not_in_form=1;this.fields_dict[df.fieldname]=f
 f.refresh();if(df.fieldtype=='Button'&&!this.first_button){$(f.input).addClass('btn-info');this.first_button=true;}}}
 this.get_values=function(){var ret={};var errors=[];for(var key in this.fields_dict){var f=this.fields_dict[key];var v=f.get_value?f.get_value():null;if(f.df.reqd&&!v)
diff --git a/public/js/all-web.js b/public/js/all-web.js
index a921819..5a4a4a1 100644
--- a/public/js/all-web.js
+++ b/public/js/all-web.js
@@ -529,7 +529,8 @@
  *	lib/js/wn/ui/dialog.js
  */
 wn.widgets.FieldGroup=function(){this.first_button=false;this.make_fields=function(body,fl){if(!window.make_field){wn.require('css/fields.css');wn.require('js/fields.js');}
-$y(this.body,{padding:'11px'});this.fields_dict={};for(var i=0;i<fl.length;i++){var df=fl[i];var div=$a(body,'div','',{margin:'6px 0px'})
+$y(this.body,{padding:'11px'});this.fields_dict={};for(var i=0;i<fl.length;i++){var df=fl[i];if(!df.fieldname&&df.label){df.fieldname=df.label.replace(/ /g,'_').toLowerCase();}
+var div=$a(body,'div','',{margin:'6px 0px'})
 f=make_field(df,null,div,null);f.not_in_form=1;this.fields_dict[df.fieldname]=f
 f.refresh();if(df.fieldtype=='Button'&&!this.first_button){$(f.input).addClass('btn-info');this.first_button=true;}}}
 this.get_values=function(){var ret={};var errors=[];for(var key in this.fields_dict){var f=this.fields_dict[key];var v=f.get_value?f.get_value():null;if(f.df.reqd&&!v)