Send mail from Feed Addon

Hi all¸
I made a custom Feed-AddOn for our client needs. There are a bunch of fields mapped and de process_feed function is creating a XML file for the clients software.

This XML file has to be e-mailed together with the file uploads in the form. Previously I used a notification hook for this, but I’d rather to this from within the addon.

How should I send an e-mail with attachments from the process_feed function?

Have you checked the Sending Notifications on Custom Events when using the Add-On Framework article in the documentation?

The gform_notification filter can be used to attach the file. You would add that filter in your add-on’s init method.

2 Likes

Thanks, I guess I missed the custom events section.
When I initiate the send_notifications for my custom event, I add the xml file path to the $form.
Inside my notification filter I first check if this is from my custom event, because the filter is not related to a specific form.

Is this the way to do it, or can I hook the filter directly to my custom event?

Can you share the code you’re using please?

Sure. I skipped the parts that are not relevant.

class MyGFormsAddOn extends GFFeedAddOn {

	public function init() {
		parent::init();
		add_filter( 'gform_notification', array($this,'send_notification'), 10, 3 );
	}

	public function supported_notification_events( $form ) {
		if ( ! $this->has_feed( $form['id'] ) ) {
			return false;
		}

		return [
			'send_xml' => esc_html__( 'Send XML', 'mygformsaddon' ),
		];
	}

	public function process_feed( $feed, $entry, $form ) {
		$field_map = $this->get_field_map_fields( $feed, 'mappedFields' );
		$merge_vars = array();
		foreach ( $field_map as $name => $field_id ) {
			$merge_vars[ $name ] = $this->get_field_value( $form, $entry, $field_id );
		}

		$filename = 'filename.xml';

		if($this->create_xml($merge_vars, $filename)){
			$form['xml_file'] = $filename;
			GFAPI::send_notifications( $form, $entry, 'send_xml' );
		}
		return;
	}

	public function send_notification( $notification, $form, $entry ) {
		if($notification['event']!='send_xml'){
			return $notification;
		}
		//Add our xml file
		$notification['attachments'][] = $form['xml_file'];
		return $notification;
	}

	private function create_xml($fields, $filename){
		//xml code is created here
		return $dom->save($filename);
	}

}

Thank you for the code. Is this working for you now and you’re just wondering if it’s the best way, or are you looking for something else?

you’re just wondering if it’s the best way

That :slight_smile: It’s working indeed, just wanted to know if it’s best practice and if there are other standard functions.

Thanks for the support guys!

I would say it looks OK. So long as it’s working, I think the approach you took is fine. Let us know if you run into any issues. Thank you.