Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created July 18, 2018 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/24ff0faac417376faf28bb7cdd41ef25 to your computer and use it in GitHub Desktop.
Save kobus1998/24ff0faac417376faf28bb7cdd41ef25 to your computer and use it in GitHub Desktop.
PHP Validate mail body
<?php
/**
* Make sure lines are right length in mail body
* @param string mail body
* @param int max line length (default 68)
* @param bool only return bool instead of modified body (default false)
* @return string|bool validated body
*/
function validateMailBody($sBody, $iMaxLen = 68, $bReturnBool = false)
{
$bIsValid = preg_match('//u', $sBody);
$aResult = [];
foreach(preg_split("/\r\n|\n/", $sBody) as $sPart)
{
if (strlen($sPart) > $iMaxLen)
{
if ($bReturnBool)
{
$bIsValid = false;
break;
}
$aResult = array_merge($aResult, str_split($sPart, $iMaxLen));
continue;
}
$aResult[] = $sPart;
}
if ($bReturnBool)
{
return $bIsValid;
}
return implode("\r\n", $aResult);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment