PHP WebShell
Текущая директория: /var/www/bitcardoApp/lib
Просмотр файла: mailer.php
<?php
// lib/mailer.php
// Central SMTP helper with rich debug to error_log + a test function.
if (!defined('MAIL_DEBUG')) define('MAIL_DEBUG', true); // set false in prod
function mailer_last_error(?string $set = null) {
static $e = '';
if ($set !== null) $e = $set;
return $e;
}
function send_email($to, $subject, $bodyHtml): bool {
mailer_last_error(''); // reset
// Basic sanity of config
$host = defined('SMTP_HOST') ? SMTP_HOST : 'relay.mailbaby.net';
$port = defined('SMTP_PORT') ? (int)SMTP_PORT : 587;
$user = defined('SMTP_USER') ? SMTP_USER : 'mb82616';
$pass = defined('SMTP_PASS') ? SMTP_PASS : 'nfg2WdHzYp24K5jh8ZVH';
$secure = defined('SMTP_SECURE') ? SMTP_SECURE : 'tls'; // 'tls' or 'ssl' or ''
$from = defined('SMTP_FROM') ? SMTP_FROM : 'no-reply@bitcardo.net';
$fromName = defined('SMTP_FROM_NAME') ? SMTP_FROM_NAME : 'Bitcardo';
// Helpful hints in logs
if (MAIL_DEBUG) {
error_log("[MAIL] Using host=$host port=$port secure=$secure user=".($user ? 'yes' : 'no'));
// DNS sanity
if ($host) {
$ipv4 = @gethostbyname($host);
error_log("[MAIL] DNS resolve: {$host} -> {$ipv4}");
}
}
// If PHPMailer is available, use it
if (class_exists('PHPMailer\PHPMailer\PHPMailer')) {
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
try {
if ($host) {
$mail->isSMTP();
$mail->Host = $host;
$mail->Port = $port ?: 587;
// TLS vs SSL guidance (common pitfall)
// - 587 + 'tls'
// - 465 + 'ssl'
if ($secure) $mail->SMTPSecure = $secure;
// Some hosts require explicit TLS: $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
// Auth only if user or pass set
$mail->SMTPAuth = (bool)($user || $pass);
if ($mail->SMTPAuth) {
$mail->Username = $user;
$mail->Password = $pass;
}
// Allow self-signed if your SMTP uses it (turn off when you can)
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
]
];
if (MAIL_DEBUG) {
$mail->SMTPDebug = 2; // verbose
$mail->Debugoutput = function($str, $level) {
error_log("[MAIL][SMTP:$level] " . trim($str));
};
}
}
$mail->CharSet = 'UTF-8';
$mail->setFrom($from, $fromName);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = $bodyHtml;
$mail->AltBody = strip_tags($bodyHtml);
$mail->send();
if (MAIL_DEBUG) error_log("[MAIL] PHPMailer send() returned OK");
return true;
} catch (\Throwable $e) {
$msg = '[MAIL] PHPMailer error: ' . $e->getMessage();
mailer_last_error($msg);
error_log($msg);
// fall through to mail()
}
}
// Fallback to PHP mail()
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: {$fromName} <{$from}>\r\n";
$ok = @mail($to, $subject, $bodyHtml, $headers);
if (!$ok) {
$msg = '[MAIL] mail() returned false. Check server mail config (sendmail/postfix) or hosting policy (outbound SMTP blocked).';
mailer_last_error($msg);
error_log($msg);
} else if (MAIL_DEBUG) {
error_log('[MAIL] mail() returned true (but delivery not guaranteed)');
}
return $ok;
}
/** Quick test helper from a page: send_test_mail('you@example.com') */
function send_test_mail($to): array {
$subject = 'Bitcardo SMTP Test ' . date('Y-m-d H:i:s');
$html = '<p>This is a test message from Bitcardo.</p>';
$ok = send_email($to, $subject, $html);
return ['ok' => $ok, 'error' => mailer_last_error()];
}
Выполнить команду
Для локальной разработки. Не используйте в интернете!