Skip to content

Instantly share code, notes, and snippets.

@hansspiess hansspiess/mail.php
Last active Feb 25, 2016

Embed
What would you like to do?
Script for sending E-Mails from Localhost for testing purposes. More (german language) at http://www.hansspiess.de/html-e-mail-entwicklung-und-testing-mit-foundation-for-emails-gmail-und-postfix/698/
<?php
// get args from command line
// http://stackoverflow.com/questions/6826718/pass-variable-to-php-script-running-from-command-line
if ( defined('STDIN') ) {
$dir = $argv[1] ? $argv[1] : './';
$ext = $argv[2] ? $argv[2] : 'html';
} else {
die( 'not on a local machine.' );
}
// set mail vars
$recipients = array( 'mail@gmail.com', 'mail@anotherservice.com', 'mail@yourdomain.com' );
$sender = 'mail@yourdomain.com';
$reply = 'mail@yourdomain.com';
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From: ' . $sender . "\r\n";
$header .= 'Reply-To: ' . $reply . "\r\n";
// loop over files in given directory:
// http://is.php.net/manual/en/function.glob.php
foreach ( glob( $dir . '*.' . $ext ) as $file ) {
// set subject to a unique still readable string to be found easily in the inbox
$subject = time() . ' ' . $file;
// grab content of inlined file
$body = file_get_contents( $file );
// and of we go!
foreach ( $recipients as $recipient ) {
mail( $recipient, $subject, $body, $header );
}
}
{
"scripts": {
"mail" : "php mail.php ./dist/ html"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.