Skip to content

Instantly share code, notes, and snippets.

@abackstrom
Last active December 21, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abackstrom/6386146 to your computer and use it in GitHub Desktop.
Save abackstrom/6386146 to your computer and use it in GitHub Desktop.
Series of scripts to send syntax-highlighted email from the command line. Sample usage: git diff | highlight | vipe | pmail -t team@company.com -s "Code Review"
#!/bin/bash
if [ -n "$1" ] ; then
pygmentize -O full -f html "$@"
else
pygmentize -O full -f html -g
fi
#!/bin/env php
<?php
const FROM_ADDR = 'you@example.com';
require_once 'bootstrap.php';
$options = getopt("t:s:");
if (!isset($options['t'])) {
echo "'To' missing! (-t)\n";
exit(1);
}
if (!isset($options['s'])) {
echo "'Subject' missing! (-s)\n";
exit(1);
}
$stdin = fopen('php://stdin', 'r');
$body_html = '';
while (!feof($stdin)) {
$body_html .= fread($stdin, 8192);
}
$from = FROM_ADDR;
$to = $options['t'];
$subject = $options['s'];
$body_text = strip_tags($body_html);
$namespace = null;
$mime_boundary = base64_encode(time());
$headers = array(
"MIME-Version" => "1.0",
"Content-type" => "multipart/alternative; boundary=\"$mime_boundary\"",
"To" => $to,
"From" => $from,
"Subject" => $subject,
"Date" => date('r'),
"Message-Id" => '<' . md5($body_html . uniqid(microtime())) . '@' . php_uname('n') . '>',
);
$body = <<<EOF
--{$mime_boundary}
Content-type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
$body_text
--{$mime_boundary}
Content-type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit
$body_html
--{$mime_boundary}--
EOF;
array_walk($headers, function(&$v, $k) { $v = "$k: $v"; });
$headers = implode("\r\n", $headers);
mail($to, $subject, $body, $headers);
// vim:set filetype=php:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment