Skip to content

Instantly share code, notes, and snippets.

@silsha
Last active January 23, 2022 14:14
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 silsha/11b1eec219c0afdbca8ec2e242592418 to your computer and use it in GitHub Desktop.
Save silsha/11b1eec219c0afdbca8ec2e242592418 to your computer and use it in GitHub Desktop.
This is a quick hack which allows sending files from a mautic form as mail attachment.
<?php
// This is a quick hack which allows sending files from a mautic form as
// mail attachment.
// We use sendgrid for sending the mail.
// Please note: Since this was a one-off workaround, some customizations are needed:
// Line 27: check for the correct form id
// Line 33-36: Replace 12…15 with your form field id
// Line 50: Enter your sendgrid api key
// Everywhere: Replace „attachment_X“ with your form field name
class webhook {
public function getRequest()
{
$rawData = file_get_contents("php://input");
return json_decode($rawData);
}
public function sendMail($requestData) {
$submissionData = $requestData->{'mautic.form_on_submit'}[0]->{"submission"};
$id = $submissionData->id;
$formid = $submissionData->{"form"}->{"id"};
// Only trigger for form id 2
if($formid != 2) {
die();
}
$results = $submissionData->results;
$a1 = dirname(__FILE__) . "/media/files/form/" . $formid . "/12/" . $results->{'attachment_1'};
$a2 = dirname(__FILE__) . "/media/files/form/" . $formid . "/13/" . $results->{'attachment_2'};
$a3 = dirname(__FILE__) . "/media/files/form/" . $formid . "/14/" . $results->{'attachment_3'};
$a4 = dirname(__FILE__) . "/media/files/form/" . $formid . "/15/" . $results->{'attachment_4'};
$html = "
<p>You've got some files:</p>
Attachment 1: ".$results->{'attachment_1'}."<br>
Attachment 2: ".$results->{'attachment_2'}."<br>
Attachment 3: ".$results->{'attachment_3'}."<br>
Attachment 4: ".$results->{'attachment_4'}."
";
$url = 'https://api.sendgrid.com/';
$user = 'api_key';
$pass = 'SENDGRID_APIKEY';
$params = array(
'to' => 'to@example.com',
'subject' => 'You’ve got files',
'html' => $html,
'from' => 'from@example.com',
);
if(is_file($a1)) {
$params['files[a1_'.$results->{'attachment_1'}.']'] = new CURLFile($a1);
}
if(is_file($a2)) {
$params['files[a2_'.$results->{'attachment_2'}.']'] = new CURLFile($a2);
}
if(is_file($a3)) {
$params['files[a3_'.$results->{'attachment_3'}.']'] = new CURLFile($a3);
}
if(is_file($a4)) {
$params['files[a4_'.$results->{'attachment_4'}.']'] = new CURLFile($a4);
}
$request = $url.'api/mail.send.json';
$header[] = 'Authorization: Bearer '.$pass;
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER,$header);
$response = curl_exec($session);
curl_close($session);
}
}
$webhook = new webhook;
$requestData = $webhook->getRequest();
$webhook->sendMail($requestData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment