Search code examples
phpsieve-language

Read sieve conf file in php


I am migrating our mail platform,

I have for each user a file (I don't have access to Sieve host)

that looks like this

require "vacation";
if not header :contains "Precedence" ["bulk","list"] {
vacation
:days 15
:addresses ["[email protected]", "[email protected]"]
:subject "Out of office Subject"
"Some out of office tekst

With multiple lines 

another line"
;}

I want to get the subject and message in PHP as a variable for each. How can accomplish this?


Solution

  • A solution without regex looks like this:

    <?php
    
    $input = file_get_contents("input");
    
    $needle = ":subject";
    
    $theString = substr($input, strpos($input, $needle) + strlen($needle));
    
    $newLinePosition = strpos($theString, "\n");
    
    $subject = substr($theString, 0, $newLinePosition);
    
    $content = substr($theString, $newLinePosition + 1);
    
    echo "SUBJECT IS \n" . $subject . "\n";
    
    echo "CONTENT IS \n" . $content . "\n";
    

    Explanation:

    • we load the contents of the file into $input (you can loop an array of filenames, of course)
    • we define our needle, which is :subject, our useful content starts from the end of this needle up until the end of the string
    • we extract the useful content and store it into $theString
    • we find the first newline's position inside our useful content, knowing that the subject is before it and the content is after it
    • we extract the subject and the content into their own variables
    • we output these values