Чтобы отправлять автоматические уведомления по SMS и электронной почте в PHP, вы можете использовать различные методы. Вот несколько примеров кода:
-
Использование встроенной функции PHP mail():
$to = 'recipient@example.com'; $subject = 'Notification'; $message = 'This is a notification email.'; // Set additional headers $headers = 'From: sender@example.com' . "\r\n" . 'Reply-To: sender@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); // Send the email $mailSent = mail($to, $subject, $message, $headers); if ($mailSent) { echo 'Email notification sent successfully.'; } else { echo 'Failed to send email notification.'; } -
Использование сторонней библиотеки, например PHPMailer:
require 'vendor/autoload.php'; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; $mail = new PHPMailer(true); try { $mail->setFrom('sender@example.com', 'Sender'); $mail->addAddress('recipient@example.com', 'Recipient'); $mail->Subject = 'Notification'; $mail->Body = 'This is a notification email.'; $mail->isSMTP(); // Configure your SMTP settings $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = 'your_username'; $mail->Password = 'your_password'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->send(); echo 'Email notification sent successfully.'; } catch (Exception $e) { echo 'Failed to send email notification. Error: ' . $mail->ErrorInfo; } -
Использование API шлюза SMS:
$apiUrl = 'https://api.example.com/sms/send'; $apiKey = 'your_api_key'; $phoneNumber = '1234567890'; $message = 'This is a notification SMS.'; $data = [ 'api_key' => $apiKey, 'phone_number' => $phoneNumber, 'message' => $message, ]; $options = [ 'http' => [ 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($data), ], ]; $context = stream_context_create($options); $response = file_get_contents($apiUrl, false, $context); if ($response === false) { echo 'Failed to send SMS notification.'; } else { echo 'SMS notification sent successfully.'; }