your code is great very easy to work on.
I've tested this with Mods for Hesk 2.5.5 and it works fine, the only change is that the hesk_mail function is different. Version for Mods for Hesk email_functions.inc.php is
Code: Select all
function hesk_mail($to, $subject, $message, $htmlMessage, $modsForHesk_settings, $cc = array(), $bcc = array(), $hasMessageTag = false)
{
$emaildomain = substr(strrchr($to, "@"), 1);
if($emaildomain == "user.twitter")
{
require_once('TwitterAPIExchange.php');
$split = explode('@',$to);
$name = $split[0];
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/direct_messages/new.json';
$requestMethod = 'POST';
/** POST fields required by the URL above. See relevant docs as above **/
$postfields = array(
'screen_name' => $name,
'text' => $message
);
/** Perform a POST request and echo the response **/
$twitter = new TwitterAPIExchange($settings);
$do = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
ob_end_clean();
return true;
}
else
{
global $hesk_settings, $hesklang, $ticket;
// Are we in demo mode or are all email fields blank? If so, don't send an email.
if (defined('HESK_DEMO')
|| (($to == NULL || $to == '')
&& ($cc == NULL || count($cc) == 0)
&& ($bcc == NULL || count($bcc) == 0))
) {
return true;
}
// Encode subject to UTF-8
$subject = "=?UTF-8?B?" . base64_encode(hesk_html_entity_decode($subject)) . "?=";
// Auto-generate URLs for HTML-formatted emails
$htmlMessage = hesk_makeURL($htmlMessage, '', false);
// Setup "name <email>" for headers
if ($hesk_settings['noreply_name']) {
$hesk_settings['from_header'] = "=?UTF-8?B?" . base64_encode(hesk_html_entity_decode($hesk_settings['noreply_name'])) . "?= <" . $hesk_settings['noreply_mail'] . ">";
} else {
$hesk_settings['from_header'] = $hesk_settings['noreply_mail'];
}
// Uncomment for debugging
# echo "<p>TO: $to<br >SUBJECT: $subject<br >MSG: $message</p>";
# return true;
// Use mailgun
if ($modsForHesk_settings['use_mailgun']) {
ob_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.mailgun.net/v2/" . $modsForHesk_settings['mailgun_domain'] . "/messages");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $modsForHesk_settings['mailgun_api_key']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
$postfields = array(
'from' => $hesk_settings['from_header'],
'to' => $to,
'h:Reply-To' => $hesk_settings['from_header'],
'subject' => $subject,
'text' => $message
);
if (count($cc) > 0) {
$postfields['cc'] = implode(',', $cc);
}
if (count($bcc) > 0) {
$postfields['bcc'] = implode(',', $bcc);
}
if ($modsForHesk_settings['html_emails']) {
$postfields['html'] = $htmlMessage;
}
if ($hasMessageTag && $modsForHesk_settings['attachments'] && $hesk_settings['attachments']['use'] && isset($ticket['attachments']) && strlen($ticket['attachments'])) {
$postfields = processDirectAttachments('mailgun', $postfields);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$result = curl_exec($ch);
curl_close($ch);
$tmp = trim(ob_get_contents());
ob_end_clean();
return (strlen($tmp)) ? $tmp : true;
}
$outerboundary = sha1(uniqid());
$innerboundary = sha1(uniqid());
if ($outerboundary == $innerboundary) {
$innerboundary .= '1';
}
$plaintextMessage = $message;
$message = "--" . $outerboundary . "\n";
$message .= "Content-Type: multipart/alternative; boundary=\"" . $innerboundary . "\"\n\n";
$message .= "--" . $innerboundary . "\n";
$message .= "Content-Type: text/plain; charset=" . $hesklang['ENCODING'] . "\n\n";
$message .= $plaintextMessage . "\n\n";
//Prepare the message for HTML or non-html
if ($modsForHesk_settings['html_emails']) {
$message .= "--" . $innerboundary . "\n";
$message .= "Content-Type: text/html; charset=" . $hesklang['ENCODING'] . "\n\n";
$message .= $htmlMessage . "\n\n";
}
//-- Close the email
$message .= "--" . $innerboundary . "--";
// Use PHP's mail function
if (!$hesk_settings['smtp']) {
// Set additional headers
$headers = '';
$headers .= "MIME-Version: 1.0\n";
$headers .= "From: $hesk_settings[from_header]\n";
if (count($cc) > 0) {
$headers .= "Cc: " . implode(',', $cc);
}
if (count($bcc) > 0) {
$headers .= "Bcc: " . implode(',', $bcc);
}
$headers .= "Reply-To: $hesk_settings[from_header]\n";
$headers .= "Return-Path: $hesk_settings[webmaster_mail]\n";
$headers .= "Date: " . date(DATE_RFC2822) . "\n";
$headers .= "Content-Type: multipart/mixed;boundary=\"" . $outerboundary . "\"";
// Add attachments if necessary
if ($hasMessageTag && $modsForHesk_settings['attachments'] && $hesk_settings['attachments']['use'] && isset($ticket['attachments']) && strlen($ticket['attachments'])) {
$message .= processDirectAttachments('phpmail', NULL, $outerboundary);
}
$message .= "\n\n" . '--' . $outerboundary . '--';
// Send using PHP mail() function
ob_start();
mail($to, $subject, $message, $headers);
$tmp = trim(ob_get_contents());
ob_end_clean();
return (strlen($tmp)) ? $tmp : true;
}
// Use a SMTP server directly instead
$smtp = new smtp_class;
$smtp->host_name = $hesk_settings['smtp_host_name'];
$smtp->host_port = $hesk_settings['smtp_host_port'];
$smtp->timeout = $hesk_settings['smtp_timeout'];
$smtp->ssl = $hesk_settings['smtp_ssl'];
$smtp->start_tls = $hesk_settings['smtp_tls'];
$smtp->user = $hesk_settings['smtp_user'];
$smtp->password = hesk_htmlspecialchars_decode($hesk_settings['smtp_password']);
$smtp->debug = 1;
// Start output buffering so that any errors don't break headers
ob_start();
// Send the e-mail using SMTP
$to_arr = explode(',', $to);
$headersArray = array(
"From: $hesk_settings[from_header]",
"To: $to",
"Reply-To: $hesk_settings[from_header]",
"Return-Path: $hesk_settings[webmaster_mail]",
"Subject: " . $subject,
"Date: " . date(DATE_RFC2822)
);
array_push($headersArray, "MIME-Version: 1.0");
array_push($headersArray, "Content-Type: multipart/mixed;boundary=\"" . $outerboundary . "\"");
if (count($cc) > 0) {
array_push($headersArray, "Cc: " . implode(',', $cc));
}
if (count($bcc) > 0) {
array_push($headersArray, "Bcc: " . implode(',', $bcc));
}
// Add attachments if necessary
if ($hasMessageTag && $modsForHesk_settings['attachments'] && $hesk_settings['attachments']['use'] && isset($ticket['attachments']) && strlen($ticket['attachments'])) {
$message .= processDirectAttachments('smtp', NULL, $outerboundary);
}
$message .= "\n\n" . '--' . $outerboundary . '--';
if (!$smtp->SendMessage($hesk_settings['noreply_mail'], $to_arr, $headersArray, $message)) {
// Suppress errors unless we are in debug mode
if ($hesk_settings['debug_mode']) {
$error = $hesklang['cnsm'] . ' ' . $to . '<br /><br />' .
$hesklang['error'] . ': ' . htmlspecialchars($smtp->error) . '<br /><br />' .
'<textarea name="smtp_log" rows="10" cols="60">' . ob_get_contents() . '</textarea>';
ob_end_clean();
hesk_error($error);
} else {
$_SESSION['HESK_2ND_NOTICE'] = true;
$_SESSION['HESK_2ND_MESSAGE'] = $hesklang['esf'] . ' ' . $hesklang['contact_webmsater'] . ' <a href="mailto:' . $hesk_settings['webmaster_mail'] . '">' . $hesk_settings['webmaster_mail'] . '</a>';
}
}
ob_end_clean();
return true;
}
} // END hesk_mail()