Created
April 8, 2019 18:10
-
-
Save tylerhall/5dac8a804388708ae7fa598cd9defffa to your computer and use it in GitHub Desktop.
Parse an inbound email webhook and create a new GitHub issue containing any attached images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?PHP | |
// Note: This script relies on Postmark to parse the email's contents into JSON. | |
// More info here: https://postmarkapp.com/ | |
$post = trim(file_get_contents('php://input')); | |
$email = json_decode($post); | |
if(is_null($email)) { | |
exit; | |
} | |
// If you're dealing with multiple repos (apps), you can setup any number of | |
// forwarding email addresses and then post to the appropriate repo based on | |
// which address you forwarded the user's email to. | |
if($email->To == '[email protected]') { | |
create_github_issue('github-username', 'app1repo', $email); | |
} | |
if($email->To == '[email protected]') { | |
create_github_issue('github-username', 'app2repo', $email); | |
} | |
function create_github_issue($owner, $repo, $email) { | |
// Create a Personal Access Token here... | |
// https://github.com/settings/tokens | |
// Token must have full repo permissions. | |
$github_personal_access_token = ''; | |
$headers = array("Authorization: token $github_personal_access_token", 'User-Agent: Email-To-Issue-Bot'); | |
$attachment_dir = 'github-attachments/'; // Path to where you want image attachments saved. Can be relative to this script or absolute. | |
$attachment_url = 'https://mydomain.com/github-attachments/'; // Public URL to directory where attachments are available. | |
$json = array(); | |
$json['title'] = $email->Subject; | |
$json['body'] = $email->TextBody; | |
$json['labels'] = array('via-email'); // List any tags you want applied to the new issue. You must create these tags in GitHub first. | |
foreach($email->Attachments as $a) { | |
if(strpos($a->ContentType, 'image') !== false) { | |
// Get the attachment's file extension | |
$parts = explode('.', $a->Name); | |
$ext = array_pop($parts); | |
// Create a unique filename | |
$fn = md5(microtime() . $a->Name) . ".$ext"; | |
// Open a writable file handle and save the attachment data | |
$fp = fopen(rtrim($attachment_dir, '/') . '/' . $fn, 'w'); | |
$data = base64_decode($a->Content); | |
fwrite($fp, $data); | |
fclose($fp); | |
// Embed the attachment's public URL as a Markdown image in the issue's body content so we can see it on GitHub | |
$url = rtrim($attachment_url, '/') . '/' . $fn; | |
$json['body'] .= "\n\n"; | |
} | |
} | |
// Create the new GitHub issue | |
$ch = curl_init("https://api.github.com/repos/$owner/$repo/issues"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json)); | |
curl_exec($ch); | |
curl_close($ch); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment