here is the code I use to want to send an email with a pdf, I use html2canva to screenshot the user's screen then I generate a pdf with it, after all that I want to send it via mailjet with my API
EDIT : my pdf is generated and I can see it in a doc.save in the function, but now the problem is that I must be able to send it by email via the MAILJET API, so I created the function send_email a little lower, which should normally send it.. I knew that I would have had problems between js and php,how can i do this ?
echo '<form>
<label for="email">Adresse e-mail :</label>
<input type="email" id="email" name="email" required>
<button id="generate-pdf" type="button">Capture d\'écran et envoi par e-mail</button>
</form>';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['email'])) {
$email = $_POST['email'];
echo '<script>
function generate_pdf() {
html2canvas(document.body).then(function(canvas) {
var imgData = canvas.toDataURL("image/png");
var doc = new jsPDF();
doc.addImage(imgData, "PNG", 10, 10);
var pdf = doc.output("datauristring");
var email = document.getElementById("email").value;
});
sendpdf();
}
document.getElementById("generate-pdf").addEventListener("click", generate_pdf);
</script>';
}
}
function sendpdf(){
$pdf = base64_encode(file_get_contents('test.pdf'));
$apiKey = "apikey";
$apiSecret = "apisecret";
$body = [
'Messages' => [
[
'From' => [
'Email' => "
'Name' => "
],
'To' => [
[
'Email' => "
'Name' => "
]
],
'Subject' => "test",
'TextPart' => "test",
'HTMLPart' => "test",
'Attachments' => [
[
'ContentType' => "application/pdf",
'Filename' => "test.pdf",
'Base64Content' => $pdf
]
]
]
]
];
$mailjet_client = new Client($apiKey, $apiSecret);
$response = $mailjet_client->post(Resources::$Email, ['body' => $body]);
$response->success() && var_dump($response->getData());
}