Blat is a lightweight, open-source Windows command-line utility designed to send files or text via SMTP. Historically, when PHP developers lacked modern dependencies like PHPMailer, they utilized Blat as a quick script hack to bypass Windows’ lack of a native sendmail utility.
By calling Blat directly from a PHP script using system commands, you can automate text or HTML alerts and file attachments seamlessly on Windows servers. Why Developers Combined PHP and Blat
Bypasses Windows mail limits: Native PHP mail() requires a local SMTP service configuration in php.ini, which is notoriously tedious to set up on Windows.
No Composer required: Ideal for legacy or standalone scripts where modern, heavy libraries cannot be installed.
Robust attachment handling: Blat easily attaches server logs, CSV reports, and PDFs via simple command arguments. Quick PHP Script Example Using Blat
This quick script sets up the necessary variables and uses PHP’s built-in exec() function to pass an email delivery command down to the Windows command prompt.
<?php // 1. Define Email Variables \(blatPath = 'C:\path\to\blat.exe'; // Absolute path to your downloaded Blat executable \)smtpServer = ‘mail.yourdomain.com’; // Your SMTP host \(port = '25'; // Port (Note: Blat natively supports non-SSL ports like 25 or 587) \)username = ‘your_username’; // SMTP Authentication username \(password = 'your_password'; // SMTP Authentication password \)to = ‘[email protected]’; \(from = '[email protected]'; \)subject = ‘Automated Windows Server Alert’; \(body = 'This is a quick automated email generated by a PHP script using Blat.'; \)attachment = ‘C:\logs\daily_report.csv’; // 2. Build the Command Line String // Arguments match Blat documentation requirements \(command = "\"{\)blatPath}\” - -body \“{\(body}\""; \)command .= ” -to \“{\(to}\""; \)command .= ” -f \“{\(from}\""; \)command .= ” -s \“{\(subject}\""; \)command .= ” -server \“{\(smtpServer}:{\)port}\”“; \(command .= " -u \"{\)username}\”“; \(command .= " -pw \"{\)password}\”“; \(command .= " -attach \"{\)attachment}\”“; // 3. Execute the Command in Windows Background \(output = []; \)returnVar = 0; exec(\(command, \)output, \(returnVar); // 4. Check Success if (\)returnVar === 0) { echo “Email sent successfully via Blat!”; } else { echo “Email failed. Error code: ” . \(returnVar . "<br>"; echo "Details: " . implode("\n", \)output); } ?> Use code with caution. Core Blat Syntax Breakdown
When building the string inside your PHP script, use these standard flags:
-: Instructs Blat to expect the email body text directly from the command string instead of reading an external .txt file. -body: Specifies the actual text message content. -to: The recipient’s email address.
-f: The sender’s email address (must be authorized by your SMTP). -s: The subject line enclosed in quotation marks. -attach: Path to a specific file you want to send along. Modern Realities & Drawbacks
While this strategy works well for quick background tools or legacy systems, consider its critical limitations: Blat – Command Line Emailer saves me time – Scott Hanselman