Skip to content

Instantly share code, notes, and snippets.

@froutsis
Last active March 2, 2016 14:29
Show Gist options
  • Save froutsis/4dbc37b0bc83a95d75b0 to your computer and use it in GitHub Desktop.
Save froutsis/4dbc37b0bc83a95d75b0 to your computer and use it in GitHub Desktop.
db-error.php file for custom action on Error Establishing Connection on a Wordpress site. Uses SMTP Credentials. Add this after you set the correct settings in the wp-content folder of your wordpress installation.
<?php
$protocol = $_SERVER["SERVER_PROTOCOL"];
if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ) $protocol = 'HTTP/1.0';
header( "$protocol 503 Service Unavailable", true, 503 );
header( 'Content-Type: text/html; charset=utf-8' );
header( 'Retry-After: 600' );
$temp = mysql_error();
$url = curPageURL();
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; }
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
// EDIT HERE ========================================================
$smtp_host = 'mail.***.gr';
$smtp_user = 'webmaster';
$smtp_pass = '***';
$alert_email_from = array(
'email' => 'webmaster@***.gr',
'name' => 'Uptime Alerts',
);
$alert_email_address = array(
'email' => '[email protected]',
'name' => 'Fotis Routsis',
);
$alert_email_subject = 'Database Connection Error on '.$url;
// You could set HTML mail text
$alert_email_body_html = 'This is an automated message,<br /> There is a SQL error, here is the message: <br />'.$temp.'.<br /> Website URL is : '.$url;
// You could set alt simple mail text
$alert_email_body_alt = '';
// STOP EDITING =====================================================
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
$mail = new PHPMailer( true );
$mail->CharSet = "UTF-8";
$mail->isSMTP();
$mail->SMTPDebug = 0; //Change this for debug
$mail->Debugoutput = 'html';
$mail->Host = $smtp_host;
$mail->Port = 25;
$mail->SMTPSecure = '';
$mail->SMTPAutoTLS = false;
$mail->SMTPAuth = true;
$mail->Username = $smtp_user;
$mail->Password = $smtp_pass;
$mail->setFrom($alert_email_from['email'], $alert_email_from['name']);
$mail->addAddress($alert_email_address['email'], $alert_email_address['name']);
$mail->Subject = $alert_email_subject;
$mail->msgHTML($alert_email_body_html);
if($alert_email_body_alt !='')
$mail->AltBody = $alert_email_body_alt;
$mail->send();
?>
<!DOCTYPE HTML>
<html dir="ltr" lang="en-US">
<head>
<title>503 Service Temporarily Unavailable</title>
<style type="text/css">
h1, p {
font-family: Helvetica, sans-serif;
font-size: 24px;
color: #333;
}
p {
font-size: 14px;
}
</style>
</head>
<body>
<h1>Something went wrong..!</h1>
<p>Our website is currently experiencing technical issues &mdash; Please check back soon!</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment