• Resolved Jeffrey Cannon

    (@jacannon2)


    I’m trying to pipe an email to a script that will send me an email back and create a new post on a blog within my multisite network.

    I can get the email to pipe to the script and it sends me an email back, but when it gets to the WordPress part it doesn’t do anything.

    There are plugins to post by email, but that’s not what I’m looking for.

    Right now I just want to send an email to a script and have that script post the raw message to a blog.

    #!/usr/bin/php -q
    
    <?php
    // read from stdin
    $fd = fopen("php://stdin", "r");
    $email = "";
    while (!feof($fd))
    {
    	$email .= fread($fd, 1024);
    }
    fclose($fd);
    
    mail('[email protected]','From my email pipe!', $email);
    
    require_once('/path/to/wordpress/wp-load.php');
    
    switch_to_blog(69);
    
    // Create post object
    $my_post = array(
      'post_title'    => 'Test',
      'post_content'  => $email,
      'post_status'   => 'publish',
      'post_author'   => 52
    );
    
    // Insert the post into the database
    wp_insert_post( $my_post );
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Did you ever resolve this issue? I am looking for a script that will automatically post specific emails to my website and to assign categories for them. In addition, have you experimented with any scripts that will post a link or message automatically from a Facebook group to a website?

    Same here. Please respond asap

    Thread Starter Jeffrey Cannon

    (@jacannon2)

    I did resolve this issue.

    #!/usr/bin/php -q
    <?php
    /**/
    //setup global $_SERVER variables to keep WP from trying to redirect
    $_SERVER = array(
      "HTTP_HOST" => "domain.com",
      "SERVER_NAME" => "domain.com",
      "REQUEST_URI" => "/email_pipe/reader.php", // location of this file
      "REQUEST_METHOD" => "GET"
    );
    
    //require the WP bootstrap
    require_once('/home/user/public_html/wp-load.php');
    
    switch_to_blog(70);
    
    //include email parser
    require_once('/home/user/public_html/email_pipe/include/rfc822_addresses.php');
    require_once('/home/user/public_html/email_pipe/include/mime_parser.php');
    
    // read from stdin
    $fd = fopen("php://stdin", "r");
    $email = "";
    while (!feof($fd))
    {
    	$email .= fread($fd, 1024);
    }
    fclose($fd);
    
    //create the email parser class
    $mime=new mime_parser_class;
    $mime->ignore_syntax_errors = 1;
    $parameters=array(
    	'Data'=>$email,
    );
    
    $mime->Decode($parameters, $decoded);
    
    //---------------------- GET EMAIL HEADER INFO -----------------------//
    
    //get the name and email of the sender
    $fromName = $decoded[0]['ExtractedAddresses']['from:'][0]['name'];
    $fromEmail = $decoded[0]['ExtractedAddresses']['from:'][0]['address'];
    
    //get the name and email of the recipient
    $toEmail = $decoded[0]['ExtractedAddresses']['to:'][0]['address'];
    $toName = $decoded[0]['ExtractedAddresses']['to:'][0]['name'];
    
    //get the subject
    $subject = $decoded[0]['Headers']['subject:'];
    
    if (!$subject) {$subject = "(no subject)";}
    
    $removeChars = array('<','>');
    
    //get the message id
    $messageID = str_replace($removeChars,'',$decoded[0]['Headers']['message-id:']);
    
    //get the reply id
    $replyToID = str_replace($removeChars,'',$decoded[0]['Headers']['in-reply-to:']);
    
    //---------------------- FIND THE BODY -----------------------//
    
    //get the message body
    if(substr($decoded[0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Body'])){
    
    	$body = $decoded[0]['Body'];
    
    } elseif(substr($decoded[0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Body'])) {
    
    	$body = $decoded[0]['Parts'][0]['Body'];
    
    } elseif(substr($decoded[0]['Parts'][0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Parts'][0]['Body'])) {
    
    	$body = $decoded[0]['Parts'][0]['Parts'][0]['Body'];
    
    }
    
    //create email message
    $email = "
    
    Message ID: $messageID
    
    Reply ID: $replyToID
    
    Subject: $subject
    
    To: $toName $toEmail
    
    From: $fromName $fromEmail
    
    Body: $body
    
    ";
    
    $email_post = "
    To: $toName $toEmail
    
    From: $fromName $fromEmail
    
    Subject: $subject
    
    $body
    ";
    
    // Create post object
    $my_post = array(
      'post_title'    => $subject,
      'post_content'  => $email_post,
      'post_status'   => 'publish',
      'post_author'   => 52,
      'post_type' => 'ticket'
    );
    
    // Insert the post into the database
    $new_post = wp_insert_post( $my_post );
    
    // Update status to: New
    $wpdb->insert(
    	'blog_70_term_relationships',
    	array(
    		'object_id' => $new_post,
    		'term_taxonomy_id' => 5
    	),
    	array(
    		'%d',
    		'%d'
    	)
    );
    
    // Update priority to: High
    $wpdb->insert(
    	'blog_70_term_relationships',
    	array(
    		'object_id' => $new_post,
    		'term_taxonomy_id' => 3
    	),
    	array(
    		'%d',
    		'%d'
    	)
    );
    
    // Update type to: Start
    $wpdb->insert(
    	'blog_70_term_relationships',
    	array(
    		'object_id' => $new_post,
    		'term_taxonomy_id' => 17
    	),
    	array(
    		'%d',
    		'%d'
    	)
    );
    
    // Send email
    //mail('[email protected]',$subject, $email);
    
    ?>

    Things I discovered through trial and error…

    The EOL (end of line) Conversion for the file had to be Unix and not Windows format. I think it also had to be created in UTF-8 and not ANSI.

    I had to specify the server variables at the beginning or the switch_to_blog() function wouldn’t work at all.

    After the new post was created I had to use MySQL queries to update the post’s taxonomy because it wouldn’t work using the WordPress functions. I think this was because they didn’t recognize the switch_to_blog() function.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘New Post via Email Piping’ is closed to new replies.