Merge branch 'email-campaign' of https://github.com/ruchamahabal/erpnext into email-campaign
diff --git a/erpnext/config/crm.py b/erpnext/config/crm.py
index e49fc60..70784f3 100644
--- a/erpnext/config/crm.py
+++ b/erpnext/config/crm.py
@@ -143,6 +143,11 @@
 				},
 				{
 					"type": "doctype",
+					"name": "Email Campaign",
+					"description": _("Sends Mails to lead or contact based on a Campaign schedule"),
+				},
+				{
+					"type": "doctype",
 					"name": "SMS Center",
 					"description":_("Send mass SMS to your contacts"),
 				},
diff --git a/erpnext/crm/doctype/email_campaign/email_campaign.py b/erpnext/crm/doctype/email_campaign/email_campaign.py
index 25d54af..8821fd5 100644
--- a/erpnext/crm/doctype/email_campaign/email_campaign.py
+++ b/erpnext/crm/doctype/email_campaign/email_campaign.py
@@ -15,6 +15,7 @@
 		#checking if email is set for lead. Not checking for contact as email is a mandatory field for contact.
 		if self.email_campaign_for == "Lead":
 			self.validate_lead()
+		self.validate_email_campaign_already_exists()
 		self.update_status()
 
 	def validate_dates(self):
@@ -40,6 +41,10 @@
 		if not lead_email_id:
 			frappe.throw(_("Please set an email id for lead communication"))
 
+	def validate_email_campaign_already_exists(self):
+		if frappe.db.get_value("Email Campaign", {"campaign_name": self.campaign_name, "recipient": self.recipient, "status": "In Progress"}):
+			frappe.throw(_("The Campaign '{0}' already exists for the {1} '{2}'").format(self.campaign_name, self.email_campaign_for, self.recipient))
+
 	def update_status(self):
 		start_date = getdate(self.start_date)
 		end_date = getdate(self.end_date)
@@ -52,7 +57,7 @@
 			self.status = "Completed"
 
 #called through hooks to send campaign mails to leads
-def send_email_to_leads():
+def send_email_to_leads_or_contacts():
 	email_campaigns = frappe.get_all("Email Campaign", filters = { 'status': ('not in', ['Unsubscribed', 'Completed', 'Scheduled']) })
 	for camp in email_campaigns:
 		email_campaign = frappe.get_doc("Email Campaign", camp.name)
@@ -81,6 +86,7 @@
 		send_email = True,
 		email_template = email_template.name
 	)
+	return comm
 
 #called from hooks on doc_event Email Unsubscribe
 def unsubscribe_recipient(unsubscribe, method):
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 48d133f..47d1a68 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -275,7 +275,7 @@
 		"erpnext.projects.doctype.project.project.send_project_status_email_to_users",
 		"erpnext.quality_management.doctype.quality_review.quality_review.review",
 		"erpnext.support.doctype.service_level_agreement.service_level_agreement.check_agreement_status",
-		"erpnext.crm.doctype.email_campaign.email_campaign.send_email_to_leads",
+		"erpnext.crm.doctype.email_campaign.email_campaign.send_email_to_leads_or_contacts",
 		"erpnext.crm.doctype.email_campaign.email_campaign.set_email_campaign_status"
 	],
 	"daily_long": [