Last active
February 6, 2024 14:47
-
-
Save only-cliches/e0f8927dc4be72e2c30b63246f9755f4 to your computer and use it in GitHub Desktop.
Hubspot Import Marketing Emails
This file contains hidden or 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
// Parse an HTML from hubspot and get a clean HTML representation. | |
function get_email_html($email) { | |
$html = ""; | |
// The email HTML comes in via an array of widgets | |
$widgets = array(); | |
foreach($email->widgets as $widget) { | |
$widgets[] = $widget; | |
} | |
// The wdigets must be sorted to get the content in order | |
usort($widgets, function($a, $b) { | |
if ($a->order == $b->order) return 0; | |
if ($a->order < $b->order) return -1; | |
return 1; | |
}); | |
// Print the email into HTML | |
foreach($widgets as $widget) { | |
// make sure each widget has HTML and is not the email header. | |
if (isset($widget->body->html) && strpos($widget->body->html, "margin-top: -120px;") === false && strpos($widget->body->html, "text-align: right;") === false) { | |
$html .= "<div class='msr-hs-section'>"; | |
$html .= str_replace("color: #ffffff;", "color: #000;", $widget->body->html); | |
$html .= "</div>"; | |
} | |
} | |
return $html; | |
} | |
function hubspot_get_emails() { | |
$curl = curl_init(); | |
// get the most recent 30 emails | |
curl_setopt_array($curl, [ | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_SSL_VERIFYHOST => false, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_PORT => 443, | |
CURLOPT_URL => "https://api.hubapi.com/marketing-emails/v1/emails?hapikey=XXXXXXXXXXXX&limit=30&orderBy=-created" | |
]); | |
$result = json_decode(curl_exec($curl)); | |
$emails = array(); | |
// loop through all emails we got from API | |
foreach($result->objects as $msr) { | |
// Ommit emails that are not MSR. | |
$has_subject = $msr->subject == "Morning Security Report" || $msr->subject == "Cybersecurity Collaborative Morning Security Report"; | |
// Omit Naco MSR | |
$is_naco = strpos($msr->name, "NACo"); | |
// make sure the emails are published and published recently (just a safety check) | |
if ($has_subject && $msr->isPublished == true && $msr->publishedAt > 1594038000 * 1000 && $is_naco == false) { | |
$emails[] = $msr; | |
} | |
} | |
return $emails; | |
} | |
// Import Hubspot MSR into Wordpress | |
function hubspot_import_msr() { | |
$emails = hubspot_get_emails(); | |
foreach($emails as $key => $email) { | |
$cleanHTML = "<div class='msr-hs-email'>" . get_email_html($email) . "</div>"; | |
echo $cleanHTML: | |
} | |
} | |
hubspot_import_msr(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment