reasonableframework/helper/mailgun.api.php

49 lines
1.4 KiB
PHP
Raw Normal View History

2019-04-12 05:57:45 +00:00
<?php
/**
* @file mailgun.api.php
* @date 2019-04-12
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief Mailgun REST API interface module
* @documentation https://documentation.mailgun.com/en/latest/api-sending.html
*/
2019-04-15 06:55:05 +00:00
if(!check_function_exists("mailgun_get_config")) {
function mailgun_get_config() {
$config = get_config();
return array(
"domain" => get_value_in_array("mailgun_domain", $config, ""),
"name" => get_value_in_array("mailgun_name", $config, "John Doe"),
2019-04-15 07:08:13 +00:00
"from" => get_value_in_array("mailgun_from", $config, ""),
2019-04-15 06:55:05 +00:00
"apikey" => get_value_in_array("mailgun_apikey", $config, ""),
);
}
}
2019-04-12 05:57:45 +00:00
if(!check_function_exists("mailgun_send_message")) {
2019-04-15 07:08:13 +00:00
function mailgun_send_message($content, $to, $subject="Untitled") {
2019-04-15 06:55:05 +00:00
$response = false;
2019-04-15 07:08:13 +00:00
// get mailgun configuration
$cnf = mailgun_get_config();
2019-04-15 06:55:05 +00:00
2019-04-15 07:08:13 +00:00
// make request
2019-04-15 06:55:05 +00:00
if(loadHelper("webpagetool")) {
$response = get_web_json(sprintf("https://api.mailgun.net/v3/%s/messages", $domain), array(
"headers" => array(
"Content-Type" => "multipart/form-data",
2019-04-15 07:08:13 +00:00
"Authentication" => array("Basic", "api", $cnf['apikey']),
2019-04-15 06:55:05 +00:00
),
"data" => array(
2019-04-15 07:08:13 +00:00
"from" => sprintf("%s <%s>", $cnf['name'], $cnf['from']),
2019-04-15 06:55:05 +00:00
"to" => $to,
"subject" => $subject,
"text" => $content,
),
));
}
return $response;
2019-04-12 05:57:45 +00:00
}
}