This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendmail.php
75 lines (54 loc) · 2.37 KB
/
sendmail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
require __DIR__ . '/vendor/autoload.php';
require_once "start.php";
use PHPMailer\PHPMailer\PHPMailer;
use AppleMusic\DB as db;
$db = new db();
global $daysInterval;
header("Content-Type: application/json");
// récup les nom/prénom/mail/id des users
$users = $db->getNotifiedUsers();
$env = explode(":", file_get_contents(__DIR__ . '/.env'));
$mail_nom = $env[3] ? $env[3] : null;
$mail_psw = $env[4] ? $env[4] : null;
foreach ($users as $user) {
$user_id = $user["id"];
$user_name = $user["prenom"];
$user_email = $user["mail"];
$json_albums = $db->getUserAlbums($user_id);
$json_songs = $db->getUserSongs($daysInterval, $user_id);
$albums = $songs = "";
foreach (json_decode($json_albums) as $item) {
$albums .= "<li><a href='https://itunes.apple.com/fr/album/" . $item->id . "' about='blank'>" . $item->name . "</a> par " . $item->artistName . " (" . date("d/m", strtotime($item->date)) . ")</li>";
}
foreach (json_decode($json_songs) as $item) {
$songs .= "<li><a href='https://itunes.apple.com/fr/album/" . $item->collectionId . "?i=" . $item->id . "' about='blank'>" . $item->trackName . "</a> par " . $item->artistName . " (" . date("d/m", strtotime($item->date)) . ")</li>";
}
$body = "<h3>Albums</h3><ul>$albums</ul><h3>Morceaux</h3><ul>$songs</ul>";
$mail = new PHPMailer;
try {
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
// $mail->SMTPDebug = 3;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = $mail_nom;
$mail->Password = $mail_psw;
$mail->setFrom($mail_nom);
$mail->FromName = 'Apple Music - Update';
$mail->addAddress($user_email, $user_name); // app user who's received all the news
$mail->addReplyTo('noreply', 'noreply');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "Nouveautés du vendredi " . date("d/m/Y");
$mail->Body = $body;
$mail->send();
$db->logMail("Mail envoyé", $user_id);
echo json_encode(array("response" => true, "data" => array()));
} catch (Exception $e) {
$db->logMail("Mail non envoyé", $user_id);
echo json_encode(array("response" => false, "error" => "Message could not be sent. Mailer Error: ', $mail->ErrorInfo"));
}
}