Код использует PHPMailer для отправки электронного письма, когда пользователь отправляет свой адрес электронной почты. Вот код, который я использую:
Код: Выделить всё
require 'phpmailer/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function send_live_album_email($email, $album_url, $qr_code_file) {
return send_qr_email($email, $album_url, $qr_code_file, "Live Album Access");
}
function send_longterm_album_email($email, $album_url) {
$qr_code_file = "";
return send_qr_email($email, $album_url, $qr_code_file, "Long-term Album Access");
}
function send_qr_email($email, $album_url, $qr_code_file) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "my_email"; // email censored
$mail->Password = "my_password"; // password censored
$mail->IsHTML(true);
$mail->AddAddress($email);
$mail->setFrom("my_email", "Photo Album Access | Photographer App");
// Subject and Body
$mail->Subject = "Photographer | Photo Album QR Code";
$qr_code_image_url = "url_here" . htmlspecialchars($qr_code_file ?? ''); //url censored
$email_template = "
Hello!
Here is your link to access the photo album:
[url=$album_url]Click here to view album[/url]
Regards,
[h4]Photographer App[/h4]
";
$mail->Body = $email_template;
$mail->AltBody = strip_tags($email_template);
try {
$mail->send();
return true;
} catch (Exception $e) {
return false;
}
}
if (isset($_POST['send_email'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$album_url = "url_here"; // url censored
if ($isLive) {
if ($qr_code_file) {
if (send_live_album_email($email, $album_url, $qr_code_file)) {
echo "alert('Live album sent successfully!');";
} else {
echo "alert('Failed to send live album email.');";
}
} else {
echo "alert('QR code not available for live album.');";
}
} else {
if (send_longterm_album_email($email, $album_url)) {
echo "alert('Album email sent successfully!');";
} else {
echo "alert('Failed to send long-term album email.');";
}
}
}
Email
Submit
Подробнее здесь: https://stackoverflow.com/questions/791 ... finityfree