Mailchimp API email sending an embedded audio

  |   By  |  0 Comments

I have discovered that the MailChimp API is pretty awful at handling audio tags when sending an email – It’ll come through looking like a playable audio player but nothing works in my email client!

So I created a code snippet to turn an audio tag into a Play Audio link button…

	//this first line extracts the src and puts it in $output_array
	preg_match_all('/<audio [^>]*><source[^>]* src=\"([^\"]*)\"[^>]*>/', $content, $output_array);
    //$new_link variable is an email friendly button with a link to the audio src URI 
    $new_link="Sorry this didn't work";//just in case!
    if(!empty($output_array[1][0]))$new_link='<table width="100%" border="0" cellspacing="0" cellpadding="0" style="padding:20px"><tr><td><table border="0" cellspacing="0" cellpadding="0"><tr><td align="center" style="border-radius: 3px;" bgcolor="#e9703e"><a href="'.$output_array[1][0].'" target="_blank" style="font-size: 16px; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; text-decoration: none;border-radius: 3px; padding: 12px 18px; border: 1px solid #e9703e; display: inline-block;">Play audio</a></td></tr></table></td></tr></table>';
    //removes the WordPress mess around the audio tag (block embed) and the audio tag itself and replaces with the new button link
    $regex = '#<figure class="wp-block-embed (.*?)</figure>#s';
    $content =preg_replace($regex,$new_link, $content);