• Resolved bhenselmann

    (@bhenselmann)


    Hello,
    I would like to process the emails fetched by Postie with a script. I use the filter “postie_post_before” for this.
    If “Process Email” is started manually in Postie, the script works as intended.
    However, when the emails are fetched automatically, my script is apparently not processed.
    What could be the reason for this?
    Thanks for the help.
    Bernhard

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Wayne Allen

    (@wayneallen-1)

    Without seeing your code it is hard to tell.

    Thread Starter bhenselmann

    (@bhenselmann)

    Thanks for answering!
    Here is my code:

    <?php
    /*
    Module Name: ReMailer 
    Description: Ueber Postie-Plugin empfangene Antwort-EMails werden weitergemailt an urspruenglichen Absender. [Backend]
    */
    
    /* Sicherheitsabfrage */
    if ( !class_exists('Toolbox') ) {
    	die();
    }
    
    /* Ab hier kann's los gehen */
    // Siehe Postie-Filter unter https://postieplugin.com/extending/
    // postie_post_pre  This filter is called before postie_post_before and before Postie has made any changes.
    // postie_post_before  This filter is called just before Postie saves the post. Replaces postie_post.
    // postie_post_after  This action is called just after Postie saves the post.
    // postie_filter_email 	This filter is called after the "from" email address has been extracted from the email so that you have a chance to change who is the author.
    // postie_filter_email2 This filter is called after postie_filter_email and includes the "To" and "Reply-To" fields.
    // postie_email_notify_recipients  This filter is called before a success notification is sent and can be used to modify the recipient list.
    // postie_email_notify_subject 	This filter is called before a success notification is sent and can be used to modify the subject line.
    // postie_email_notify_body This filter is called before a success notification is sent and can be used to modify the message body.
    
    /* Absender und Empfaenger der empfangenen Postie-Mail in custom fields speichern */
    
    function bh_before_save ($post, $headers){
    print('<h3>Start Postie</h3>');
    
    print('<u>EMail-Eingang:</u><br />');print_r($post); print('<br />'); print('<br />');
    
    print('<u>$headers:</u><br />');print_r($headers); print('<br />'); print('<br />');
    
    	$absender = $headers['from']['personal'] . ' <' . $headers['from']['mailbox'] . '@' . $headers['from']['host'] . '>' ; 
    	$empfaenger = $headers['to'][0]['personal'] . ' <' . $headers['to'][0]['mailbox'] . '@' . $headers['to'][0]['host'] . '>';
    	$empfaenger_zielgruppe = substr($headers['to'][0]['mailbox'], 0, strpos($headers['to'][0]['mailbox'],'.'));
    	
    	$empfaenger_name = substr($headers['to'][0]['personal'], 0, strpos($headers['to'][0]['personal'],' via Aktiv gegen Kinderarbeit'));
    	
    print('<u>Empf?nger-Zielgruppe:</u><br />');print_r($empfaenger_zielgruppe); print('<br />'); print('<br />');
    	$empfaenger_timestamp = substr($headers['to'][0]['mailbox'], strpos($headers['to'][0]['mailbox'],'.')+1);
    	
    print('<u>Empf?nger-Zeitstempel:</u><br />');print_r($empfaenger_timestamp); print('<br />'); print('<br />');
    
    	$betreff = $headers['subject'];
    	$suche = Array('"', 'wpcf7s// ');
    	$ersetze = Array("'", "");
    	$betreff = str_replace($suche, $ersetze, $betreff);
    	
    	add_post_meta($post['ID'], 'wpcf7s_posted-empfaenger-zielgruppe', $empfaenger_zielgruppe);
    	add_post_meta($post['ID'], 'wpcf7s_posted-empfaenger-name', $empfaenger_name);
    	add_post_meta($post['ID'], 'sender', $absender);
    	add_post_meta($post['ID'], 'recipient', $empfaenger);
    	add_post_meta($post['ID'], 'subject', $betreff);
    	add_post_meta($post['ID'], 'wpcf7s_posted-empfaenger-timestamp', $empfaenger_timestamp);
    	add_post_meta($post['ID'], 'wpcf7s_posted-text', $post['post_content']);
    	
    		
    	/* ursprünglichen Absender = neuer Empf?nger identifizieren */
    	$args = array(
    		'posts_per_page'   => 1,
    		'meta_key'         => 'wpcf7s_posted-sender-timestamp', 
    		'meta_value'       => $empfaenger_timestamp,
    		'post_type'        => 'wpcf7s',
    		'post_status'      => 'publish,private'
    	);
    	$posts_array = get_posts( $args );	
    	
    print('<u>Empf?nger-Nachricht:</u><br />');print_r($posts_array); print('<br />'); print('<br />');
    	$empfaenger_postID = $posts_array[0]->ID;
    	
    print('<u>Empf?nger-Post-ID:</u><br />');print_r($empfaenger_postID); print('<br />'); print('<br />');
    	$empfaenger_email = get_post_meta($empfaenger_postID, 'wpcf7s_posted-sender-email', true);
    	$empfaenger_name = get_post_meta($empfaenger_postID, 'wpcf7s_posted-sender-name', true);
    	$an = $empfaenger_name . ' <' . $empfaenger_email . '>';
    	
    print('<u>Empf?nger-Email-Adresse:</u><br />');print_r($an); print('<br />');print($an); print('<br />'); print('<br />');
    
    	/* Betreff der weiterzuleitenden EMail definieren */
    	$betreff = '' . $betreff;
    print('<u>Betreff für Weiterleitung:</u><br />');print_r($betreff); print('<br />'); print('<br />');
    	
    	/* Weiterzuleitende Nachricht definieren */
    	
    	$wichtiger_hinweis = get_post(420844);
    	
    	
    	$hinweis_anfang = $wichtiger_hinweis->post_content . "
    
    ------------- Anfang der Nachricht -------------
    	
    	";
    	$hinweis_ende = "
    
    ------------- Ende der Nachricht -------------
    	
    	";
    	$post_content = strip_tags($post['post_content']);
    	
    	
    	
    	$nachricht = $hinweis_anfang . $post_content . $hinweis_ende;
    	
    print('<u>EMail-Ausgang (Nachricht zur Weiterleitung):</u><br />');print_r($nachricht); print('<br />'); print('<br />');
    	
    	/* Antworter als neuen Sender mit anonymisierter Mail_Adresse einstellen */
    	$sender_name = get_post_meta($empfaenger_postID, 'wpcf7s_posted-empfaenger-name', true);
    	$sender_timestamp = microtime(true);
    	add_post_meta($post['ID'], 'wpcf7s_posted-sender-timestamp', $sender_timestamp);
    	$sender_email = $headers['from']['mailbox'] . '@' . $headers['from']['host'];
    	add_post_meta($post['ID'], 'wpcf7s_posted-sender-email', $sender_email);
    	switch ($empfaenger_zielgruppe){
    		case "kunde": $sender_zielgruppe = "firma"; break;
    		case "firma": $sender_zielgruppe = "kunde"; break;
    	}
    	$sender_email_anonym = $sender_zielgruppe . '.' . $sender_timestamp . '@aktiv-gegen-kinderarbeit.de';
    	add_post_meta($post['ID'], 'wpcf7s_posted-sender-email-anonym', $sender_email_anonym); 
    	$kopf[] = 'From: ' . $sender_name . ' via Aktiv gegen Kinderarbeit <' . $sender_email_anonym . '>';
    	
    print('<u>EMail-Header der Weiterleitung:</u><br />');print('<br />'); 
    
    print('Sender-Name: '); print_r($sender_name);print('<br />'); 
    
    print('Sender-Email anonym: '); print_r($sender_email_anonym);print('<br />'); 
    
    print('Kopf: '); print_r($kopf); print('<br />'); print('<br />');
    
    	
    	wp_mail($an, $betreff, $nachricht, $kopf);
    
    print('<h3>Ende Postie</h3><hr />');
    	return $post;
    
    }
    add_filter('postie_post_before', 'bh_before_save', 10, 2); 
    
    Thread Starter bhenselmann

    (@bhenselmann)

    Hello Wayne Allen,
    Thank you for trying to help.
    I have now found the error myself. It was the security query of the plugin through which I call the function. The queried class is of course only present when the mails are retrieved manually …

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘custom email process not working with automatic checking for emails’ is closed to new replies.