How to Become a Chatbot Ninja in 7 Days
Module 6: Case 6.1. Using PHPMailer to Send emails from a Flow
Case 6.1. Using PHPMailer to Send emails from a Flow
There have been several requests to send email straight from within ManyChat besides sending it to Admin Account holders only. I found a great solution using PHPMailer.
It uses multiple library files, we searched for the easiest solution to implement it without composer (a tool for dependency management in PHP). You can simply upload the files (in the dowload section at the bottom of the case/lesson) to your server and you're almost good to go.
Let's have a look.
The Flow
First, we create a flow where we insert a dynamic response after asking some details, such as an email address to send the email to.
The purple block is the dynamic response, you can see it up close here. Pay close attention to the JSON code and just copy what you see there. The code after the word secret, is just a code you make up that is not easy to guess. You will need it later in one of the PHP files (line 32 of mailer.php):
The Script
There are several PHP scripts needed for this feature to work, as well as one HTML file with the contents of your actual email.
I will discuss the ones here that require editing.
First we have this file, it is called mailer.php. Pay attention to the green lines (32, 35-39, 46, 49), the text in red is what you want to modify to suit your own specifics.
Then there's the HTML file you can edit. It holds two variables, however you can expand that if you have some programming experience.
Let's have a look at it.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN"> <html> <head> <meta name="generator" content="HTML for your email"> <title>My email to %1$s</title> </head> <body> <h3>Hi %1$s,</h3> What do you think of my <strong>HTML formatted</strong> email to you? <br /> It's pretty cool, don't you think? <br />It's also possible to add an attachement, like a PDF or an Image file. Perfect to give value! %2$s <br /><br /> Cya around, %1$s! <br /> Don't forget, at ChatbotDojo.com we will teach you all these cool things! <br /><br /> <br /><br /> </body> </html>
If you look at the code in the HTML here, you'll notice these two variables: %1$s and %2$s, these two correspond to the code in the PHP file earlier in this lesson, on line 63.
... $mail->msgHTML(sprintf(file_get_contents($var_htmlmsgloc), $var_name, $var_freetext)); ...
What these %1$s and %2$s do, is replace the two variables with $var_name and $var_freetext.
Finally, we use this sendmail.php script to create the dynamic response code to post back to ManyChat. You will find the complete package to upload in the download section. This next PHP file does not need any changes.