Try ERPNext Buy Support Partners Foundation

Newsletter with employees name

I want to send a mass email to all employees with their names how is that possible? Newsletter has no option for doctype fields

Hope this will help.

it works with email template but not newsletter

Hi @Khadija,

we had a similar requirement on newsletters, where they should be personalised (customer name, …). Have have solved this by extending the newsletter function with a custom button “Send with dynamic content” and added some server side code which replaces predefined strings in the newsletter body. You can find the source here:

The custom script looks like this:

frappe.ui.form.on("Newsletter", {
  refresh: function(frm) {
	if ((!frm.doc.__islocal) && (frm.doc.email_sent == 0)) {
		frm.add_custom_button(__("Dynamisch senden"), function() {
			send_dynamic(frm);
		});
		frm.add_custom_button(__("Vorlage einlesen"), function() {
			load_template(frm);
		});
	}
  }
});

function send_dynamic(frm) {
	frappe.call({
		method: 'erpnextswiss.erpnextswiss.dynamic_newsletter.enqueue_send_dynamic_newsletter',
		args: {
		newsletter: frm.doc.name
		},
		callback: function(r) {
			frappe.msgprint(  
				__("The newsletter has been added to the sending queue. It will be sent in the next few minutes."),
				__("Newsletter sending") );
		}
	});
}

function load_template(frm) {
  frappe.prompt([
	  {'fieldname': 'template', 'fieldtype': 'Link', 'label': 'Vorlage', 'options': 'Terms and Conditions', 'reqd': 1}  
	],
	function(values){
	  frappe.call({
		"method": "frappe.client.get",
		"args": {
			"doctype": "Terms and Conditions",
			"name": values.template
		},
		"callback": function(response) {
			var terms = response.message;
			if (terms) {
				cur_frm.set_value('message', terms.terms);
			} 
		}
	});
	},
	'Vorlage auswählen',
	'OK'
  )
}

Hope this helps. (Maybe this also helps How to add custom fields to Newsletter using jinja?)

2 Likes

Thank you, it fulfilled the purpose

1 Like

This seems amazing and so handy Lasalesi. Except I can’t get it to work for me :expressionless:

Versions I am using:
ERPNext: v12.9.3 (version-12)
Frappe Framework: v12.6.1 (version-12)

What I did and what happened so far:

  1. Created and installed a custom app.
  2. Added the dynamic_newsletter.py file in the custom app.
  3. Added the client-side script for the Newsletter doctype.
  4. Ran bench migrate && bench restart for compiling the Python.
  5. Set up a test template in the Terms & Conditions.
  6. Created a new newsletter, loaded the template (works :white_check_mark:)
  7. Pressing “send with dynamic content” also successfully returned the message saying "“The newsletter has been added to the sending queue. It will be sent in the next few minutes.”
  8. After a couple of moments, refreshing the newsletter also updates the newsletter status to “sent”.
  9. Absolutely no related errors in the Error Log but no newsletter emails also :slightly_frowning_face:

Hope someone can help. TIA

Best,
Shubham

were there mails in email queue?

1 Like

Have you checked if there are any errors in the background processes (Settings > Background Jobs > show failed)?

1 Like

No. I noticed that it is not queuing up the mails at all. Maybe I made some mistake while modifying the code suitably for my use case.

Could it also probably be due to version difference? I am on v12 and maybe this was written for an older version?

I just checked and I can’t see any errors in the background processes as of now. Will try and send the dynamic newsletters again. That is when I will again check if any background jobs are failing.

Thanks a lot for the reply, btw.

Version should not be an issue, we are also using v12.

1 Like