Sunday, 17 February 2013

Send a Pre-Formatted .EML File in PHP


I’m not going to go into any magical back-story about how the “.EML” text-file format for email works or how it’s a good choice in preparing an email template for PHP scripts. I’m just going to get straight to the point:

Easiest way to send an “.EML” file in PHP

$emailpath = 'email.eml'; 
$sendmail = sprintf("cat $emailpath | %s -oi -f %s -t", escapeshellcmd("/usr/sbin/sendmail"), escapeshellarg(; //Message that address if any errors that may occur 
@$mail = popen($sendmail, 'w'); 
$raw = pclose($mail);

Make sure your “email.eml” file contains all the email headers you may need, particularly:
  1. To:
  2. From:
  3. Subject:
I would strongly advice you review my use of “escapeshellcmd” and “escapeshellarg” to prepare the variables to be interpreted by the shell.

Parsing “.EML” files and sending them in PHP:

Since the “.EML” is a text based format, they provide versatility to serve as templates. In this example I supply email header data through pre-set variables. I later use regular expressions to change existing text in the .EML file (in this case [$FirstName] [$LastName] [$EmailAddress] and [$Greeting]) with their corresponding variable as supplied.

I choose to keep the file stream open rather than send the whole of the parsed content into a single command. No particular reason, just thought I should…

Here is the example:

    $sendmailpath = '/usr/sbin/sendmail'; 
    $emailpath = 'emailTemplate.eml'; 
    $messagecontent = file_get_contents($emailpath);
    //Variables for the email 
    $FirstName = "Alex"; 
    $LastName = "Casamassima"; 
    $EmailAddress = ""; 
    $Greeting = "Good day kind sir,"; 
    $parsed_message = preg_replace('/\[\$([01-9A-Za-z_]+)\]/e', "$$1", $messagecontent); 
    $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($sendmailpath), escapeshellarg("")); 
    if(!@$mail = popen($sendmail, 'w')) { 
        throw new Exception("Couldn't open the sendmail program!"); 
    fputs($mail, $parsed_message); 
    $result = pclose($mail); 
    if($result != 0) { 
      throw new Exception("Couldn't send email!"); 

.EML files are useful as they offer great facility to provide MulitPart content, including text versions of an email and embedded attachments. As well, modifying them is as simple as opening them up in your text editor of choice.

Easiest way to make a beautiful .EML file

This may be a personal preference of mine. But, Creating a new draft in “Windows Live Mail” often suffices for me. The menu inside the email client allows you to “Save as file” that defaults to an “.EML” file.

Using a client such as “Windows Live Mail” or “Outlook” allows for quick and easy use of embedded pictures, fonts, colours, and special features.

Alternative resources on PHP and .EML:
  3. search for (EML file) on page

Monday, 4 February 2013

Basic Bitwise made easy for VB.NET

If you are searching BitWise in VB.Net on the web, you will come across a billion references telling you every little detail about the math in how they work. I won’t do that….

All I will give you is the synopsis:

'Define your flags: (FlagsAttribute warns the compiler)
<Flags()> _
Enum SettingFlags AS Integer 
    SettingA = 1
    SettingB = 2
    SettingC = 4
    SettingD = 8
    SettingE = 16
    SettingF = 32
    SettingG = 64
    SettingH = 128
    SettingI = 256
    SettingJ = 512
    SettingK = 1024
    SettingL = 2048
    SettingM = 4096
    SettingN = 8192
    SettingO = 16384
    SettingP = 32728 
    CominationAtoD = SettingA or SettingB or SettingC or SettingD ' = 15
End Enum
'Define your settings (all flags are off)
Dim YourSettings AS SettingFlags = 0
'Turn some flags on (using OR):
YourSettings = YourSettings OR SettingsFlags.SettingE
YourSettings = YourSettings OR SettingsFlags.CombinationAtoD
YourSettings = YourSettings OR 32
'Now YourSetting=63 meaning SettingA,B,C,D,E,F are all "on"
'Turning a flag off (using AND NOT)
YourSettings = YourSettings AND NOT SettingsFlags.SettingB
'Now YourSettings=61 meaning SettingsA,C,D,E,F are all "on"
'Toggle a flag (using XOR)
YourSettings = YourSettings XOR SettingsFlags.SettingsG
YourSettings = YourSettings XOR SettingsFlags.CombinationAtoD
'Now YourSettings=114 meaning SettingsB,E,F,G are "on" 
'Determine if a flag is on (using AND MASK = MASK)
IF (YourSettings AND SettingsFlags.SettingsB) = SettingsFlags.SettingsB THEN
    'In this case true, because SettingsB is "on"

In the end, all you have is a integer variable definition (make it ulong if you need more bits) that can act as a grouping of Boolean variables via the binary representation of the integer, where 1 is true, and 0 is false. I avoid demonstrating “Bit Shifting” because different programming languages handle it differently.

Now the above is self-explanatory, and my comments give examples and explanations. If you want more information or education you can sift through a million references I found on the subject below.


Other than that, copy and paste my code. And use it. It’s useful for calling functions, combining similar variables/settings, or simplifying your database.

Feel free to leave your comments or suggestions!



Page Hits