Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active February 22, 2018 18:33
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 lgaetz/16f52b10983d1b8eaae02d9ec0ddf310 to your computer and use it in GitHub Desktop.
Save lgaetz/16f52b10983d1b8eaae02d9ec0ddf310 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
/*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
* email_notify.php
* FreePBX AGI file that generates an email message to an email address or to a list of addresses
* in an external file.
*
* Latest Version: https://gist.github.com/lgaetz/16f52b10983d1b8eaae02d9ec0ddf310
*
* Usage: call AGI from FreePBX dialplan with required args
* exten => s,n,AGI(email_notify.php,${email}[,${attachment}])
* $email_file = full path and name to file containing email list OR single email address
* $attachment = full path and filename to attach to outgoing messages optional
*
* License: GNU/GPL3
*
* Version History
* 2017-08-30 First revision
* 2018-02-22 add support for single email address from dialplan
*
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***/
// set to true for verbose Asterisk logging
$debug = true;
// Load FreePBX bootstrap environment - tested on FreePBX 14
include '/etc/freepbx.conf';
// Connect to AGI
require_once "phpagi.php";
$AGI = new AGI();
// example collection of channel variables - can be used in the outgoing emails
$ampuser = get_var($AGI,"AMPUSER");
if ($debug) $AGI->verbose("AMPUSER: ".$ampuser, 0);
$calleridnum = get_var($AGI,"CALLERID(number)");
if ($debug) $AGI->verbose("CALLERID(number): ".$calleridnum, 0);
$calleridname = get_var($AGI,"CALLERID(name)");
if ($debug) $AGI->verbose("CALLERID(name): ".$calleridname, 0);
// message details
$msg_from_email="anon@example.com";
$msg_from_name="Name";
$msg_subject="Subject";
$msg_body="Email body";
// Check if arg[1] is a valid file name, and if not assume it's an email address
if (file_exists($filename)) {
// email addresses into array parsed from file
$text = file_get_contents($argv[1]);
$foo=preg_match_all('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/si',$text, $matches);
} else {
$matches[0][]=$argv[1];
}
// send the emails
foreach($matches[0] as $match) {
if ($debug) $AGI->verbose("sending email to: ".$match, 0);
$em = new \CI_Email();
$em->from($msg_from_email,$msg_from_name);
$em->to($match);
$em->subject($msg_subject);
$em->message($msg_body);
$em->attach($argv[2]);
$em->send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment