Read sieve conf file in php

144 views Asked by At

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?

2

There are 2 answers

2
Lajos Arpad On BEST ANSWER

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
0
Adedoyin Akande On

You can use the preg_match to perform a regular expression match:

<?php
$script = file_get_contents('./file.sieve');

// Regular expression to match the subject and message lines
$pattern = '/:subject "(.+)"/';

// Use preg_match to apply the regular expression and extract the subject and message
preg_match($pattern, $script, $matches);

// The subject is in the first capture group, and the message is in the second capture group
$subject = trim(str_replace(':subject', '', $matches[0]));
$message = $matches[1];

print_r($subject);
print_r("\n");
print_r($message);