- poszlo / Send Form (HTML+PHP+Sweet Alert).md
- How to Get Email Alerts for Unhandled PHP Exceptions
- Written by Team LoginRadius
- How to send auto response email in PHP
- index.php
- database.php
- mail-process.php
- notification.php
- Send Email Alert to Admin when Error Exceptions occurs in Laravel
- 1. Handle Exceptions in Laravel Handler
- 2. Create Mail for Alert
- 3. Create Database Queue to process Jobs in background
- 4. Create Job for Alert
- 5. Start Queue, Setup SMTP & Done
poszlo / Send Form (HTML+PHP+Sweet Alert).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
$ to = ‘your@email.com’ . «\r\n»; |
$ subject = ‘Subject’ ; |
$ name = $ _POST [ ‘nombre’ ]; |
$ email = $ _POST [ ’email’ ]; |
$ message = » ************************************************** \r\n» . |
» Message from you website! \r\n» . |
» ************************************************** \r\n» . |
» Name: » . $ name . «\r\n» . |
» E-mail: » . $ email . «\r\n» . |
» Message: » . $ _POST [» message «] . «\r\n»; |
$ headers = » From: » . $ name . » < " . $ email . " >\r\n» . |
» Reply-To: » . $ email . «\r\n» . |
» MIME-Version: 1.0 » . «\r\n» . |
» Content-type:text/html;charset=UTF-8 » . «\r\n»; |
mail( $ to , $ subject , $ message , $ headers ); |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
$ ( document ) . ready ( function ( ) |
$ ( ‘#contact-form’ ) . on ( ‘submit’ , function ( e ) < //Don't foget to change the id form |
$ . ajax ( |
url : ‘contact.php’ , //===PHP file name==== |
data : $ ( this ) . serialize ( ) , |
type : ‘POST’ , |
success : function ( data ) |
console . log ( data ) ; |
//Success Message == ‘Title’, ‘Message body’, Last one leave as it is |
swal ( «¡Success!» , «Message sent!» , «success» ) ; |
> , |
error : function ( data ) |
//Error Message == ‘Title’, ‘Message body’, Last one leave as it is |
swal ( «Oops. » , «Something went wrong :(» , «error» ) ; |
> |
> ) ; |
e . preventDefault ( ) ; //This is to Avoid Page Refresh and Fire the Event «Click» |
> ) ; |
> ) ; |
Send mail php from html form without reload page and sending success and error alert with SweetAlert
- Add file needed by SweetAlert (js & css)
- don’t forget to link JQuery and main.js
- Create your form according to your needs, don’t forget the id
- Create PHP file according to your needs
- Enjoy!
How to Get Email Alerts for Unhandled PHP Exceptions
This tutorial provides a solution to get email alerts, when a unhandled error occurs. For this tutorial, we are using WordPress.
Imagine you created a php application and it’s running fine. A couple of days later, a user complains about an error which you’re totally unaware of. Such unhandled errors are very common in php applications. So, to find these issues, we are going to send email notifications whenever an error occurs.
Step 1. First of all we need to identify a common application file that include at top of application. in this case the file is “wp-config.php” at the root of your wordpress hosting directory.
Step 2. Let’s add the following line of code in this file under php tag.
set_error_handler('errorHandler'); // this line of code sets an error handler custom function to handle any php error. And here we pass our custom function that name “errorHandler” function createTable($array) if(is_array($array) && count($array)>0) $errorContent = ""; foreach ($array as $key => $val) $errorContent .= $key . " "; if(is_array($val) && count($val)>0) $errorContent .= createTable(json_decode(json_encode($val),true)) ; >else $errorContent .= print_r($val, true) ; > > $errorContent .= "
"; return $errorContent; > return ''; > /** * * @param type $errorNumber // This parameter returns error number. * @param type $errorString // This parameter returns error string. * @param type $errorFile // This parameter returns path of file in which error found. * @param type $errorLine // This parameter returns line number of file in which you get an error. * @param type $errorContext // This parameter return error context. */ function errorHandler($errorNumber, $errorString, $errorFile, $errorLine, $errorContext) $emailAddress = '[email protected]'; $emailSubject = 'Error on my Application'; $emailMessage = 'Error Reporting on :-
[' . date("Y-m-d h:i:s", time()) . ']'; $emailMessage .= "Error Number :-
".print_r($errorNumber, true).''; $emailMessage .= "Error String :-
".print_r($errorString, true).''; $emailMessage .= "Error File :-
".print_r($errorFile, true).''; $emailMessage .= "Error Line :-
".print_r($errorLine, true).''; $emailMessage .= "Error Context :-
".createTable($errorContext); $headers = "MIME-Version: 1.0" . "rn"; $headers .= "Content-type:text/html;charset=UTF-8" . "rn"; mail($emailAddress, $emailSubject, $emailMessage, $headers); // you may use SMTP, default php mail service OR other email sending process >
Step 3. Now save the file and upload on server.
Whenever we got any error in wordpress then above code will send an email to given email-address ([email protected]). You can use the above code on production websites to improve application performance and track all errors that’s are missed.
Similarly we can use above code in drupal, joomla, and many other content management systems.
Common config files on other platforms are listed below:
CMS/application name | Location of common file |
---|---|
4Images Gallery | /config.php |
B2 Evolution | /conf/_basic_config.php |
Boonex Dolphin | /inc/header.inc.php |
Concrete5 | /config/site.php |
Coppermine Photo Gallery | /include/config.inc.php |
Crafty Syntax Live Help | /config.php |
Cube Cart | /includes/global.inc.php |
dotProject | /includes/config.php |
Drupal | /sites/default/settings.php |
e107 | /e107_config.php |
FAQMasterFlex | /faq_config.php |
Gallery | /config.php |
Geeklog | /db-config.php or /siteconfig.php or /lib-common.php |
glfusion | /private/db-config.php |
Hotaru | /hotaru_settings.php |
Joomla | /configuration.php |
LiveSite | /livesite/config.php |
LifeType | /config/config.properties.php |
Mambo | /configuration.php |
Marketecture | /include/config.php |
MODx | /manager/includes/config.inc.php |
Moodle | /config.php |
MyBB | /inc/config.php |
Noahs Classifieds | /app/config.php |
Nucleus | /config.php |
ocPortal | /info.php |
OpenCart | /config.php or /admin/config.php |
osCommerce | /includes/configure.php or /admin/includes/configure.php |
Oxwall | /ow_includes/config.php |
PHP-Nuke | /config.php |
phpBB | /config.php |
phpFormGenerator | /index.php or /mysql.class.php |
phpFreeChat | /forms/admin/config.inc.php (only if you have saved form input to a database) |
PHPlist | /config/config.php |
phpMyDirectory | /defaults.php |
phpWCMS | /include/inc_conf/conf.inc.php |
phpWebSite | /conf/config.php |
PhpWiki | /admin.php or /lib/config.php |
Pligg | /libs/dbconnect.php |
Post-Nuke | /config.php |
PrestaShop | /config/settings.inc.php |
Saurus CMS | /config.php |
ShopSite | /includes/configure.php or /admin/includes/configure.php |
Siteframe | /config.php |
Simple Machines Forum | /Settings.php |
Soholaunch | /sohoadmin/config/isp.conf.php |
SugarCRM | /config.php |
Textpattern | /textpattern/config.php |
TikiWiki | /db/local.php |
Tomato Cart | /includes/configure.php |
TYPO3 | /typo3conf/localconf.php |
WebCalendar | /includes/settings.php |
WHMCS | /configuration.php |
WordPress | /wp-config.php |
X7 Chat | /config.php |
Xoops | /mainfile.php |
Zen Cart | /includes/configure.php or /admin/includes/configure.php |
Zikula | /config.php |
Written by Team LoginRadius
LoginRadius is a leading provider of cloud-based Customer Identity and Access Management (cIAM) platform.
How to send auto response email in PHP
In this example we using gmail SMTP for send auto resoponse email to the user who register a form.
To send mail first create a table in the database.
SQL query for create a table is :
CREATE TABLE `users` ( `userid` int(8) NOT NULL, `first_name` varchar(55) NOT NULL, `last_name` varchar(55) NOT NULL, `city_name` varchar(55) NOT NULL, `email` varchar(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
To send a response mail to the user, you must have email field in your form.
Here we using 3 file for send auto response mail in PHP.
index.php:To recieve user data.
database.php:For connect with database.
mail-process.php:For process the user data and send auto response mail to PHP.
notification.php:For process the user data and send auto response mail to both user and admin.
index.php
First name:
Last name:
City name:
Email Id:
database.php
mail-process.php
IsSMTP(); $mail->isHTML(true); $mail->SMTPDebug = 0; $mail->SMTPAuth = true; $mail->SMTPSecure = "ssl"; $mail->Host = "smtp.gmail.com"; $mail->Port = '465'; $mail->AddAddress($mailid); $mail->Username ="divyasundarsahu@gmail.com"; $mail->Password ="password@123"; $mail->SetFrom('divyasundarsahu@gmail.com','Divyasundar Sahu'); $mail->AddReplyTo("divyasundarsahu@gmail.com","Divyasundar Sahu"); $mail->Subject = $subject; $mail->Body = $message; $mail->AltBody = $message; if($mail->Send()) < echo "Thank you for register u got a notification through the mail you provide"; >> catch(phpmailerException $ex) < $msg = "".$ex->errorMessage().""; > ?>
If you want to send an alert mail to admin that some one register with us then use this process file instead of mail-process.php file.
notification.php
IsSMTP(); $mail->isHTML(true); $mail->SMTPDebug = 0; $mail->SMTPAuth = true; $mail->SMTPSecure = "ssl"; $mail->Host = "smtp.gmail.com"; $mail->Port = '465'; $mail->AddAddress($mailid); $mail->Username ="divyasundarsahu@gmail.com"; $mail->Password ="password@123"; $mail->SetFrom('divyasundarsahu@gmail.com','Divyasundar Sahu'); $mail->AddReplyTo("divyasundarsahu@gmail.com","Divyasundar Sahu"); $mail->Subject = $subject; $mail->Body = $message; $mail->AltBody = $message; if($mail->Send()) < echo "Thank you for register u got a notification through the mail you provide"; >> catch(phpmailerException $ex) < $msg = "".$ex->errorMessage().""; > require_once 'mailer/class.phpmailer.php'; /* creates object */ $mail = new PHPMailer(true); $mailid = "divyasundarsahu@gmail.com"; $subject = "Thank u"; $text_message = "Hello"; $message = "New User Registered. Mail id :".$mailid.""; try < $mail->IsSMTP(); $mail->isHTML(true); $mail->SMTPDebug = 0; $mail->SMTPAuth = true; $mail->SMTPSecure = "ssl"; $mail->Host = "smtp.gmail.com"; $mail->Port = '465'; $mail->AddAddress($mailid); $mail->Username ="divyasundarsahu@gmail.com"; $mail->Password ="password@123"; $mail->SetFrom('divyasundarsahu@gmail.com','Divyasundar Sahu'); $mail->AddReplyTo("divyasundarsahu@gmail.com","Divyasundar Sahu"); $mail->Subject = $subject; $mail->Body = $message; $mail->AltBody = $message; if($mail->Send()) < echo "You have a new user"; >> catch(phpmailerException $ex) < $msg = "".$ex->errorMessage().""; > ?>
Send Email Alert to Admin when Error Exceptions occurs in Laravel
It’s very tedious for a developer to check error logs every day and make sure that everything is running fine. But you can reduce this daily overhead by creating simple developer notifications to Admin Mail or Slack for example.
1. Handle Exceptions in Laravel Handler
There is default Handler for all these exceptions at app\Exceptions\Handler.php in Laravel 8. We handled the situation in reportable callback.
reportable(function (Throwable $e) < // Create Notification Data $exception = [ "name" =>get_class($e), "message" => $e->getMessage(), "file" => $e->getFile(), "line" => $e->getLine(), ]; // Create a Job for Notification which will run after 5 seconds. $job = (new JobDevNotification($exception))->delay(5); // Dispatch Job and continue dispatch($job); >); > >
2. Create Mail for Alert
Create the Mail app\Mail\ErrorAlert.php in Laravel using below command. Refer Mail documentation on https://laravel.com/docs/8.x/mail
php artisan make:mail ErrorAlert
exception = $exception; > /** * Build the message. * * @return $this */ public function build() < return $this->from('system@example.com', 'System')->subject("Error Alert on Server") ->view('emails.error_alert'); > >
You will also need a resources\views\emails\error_alert.blade.php to format a mail. Here you can directly use $exception variable as it is set public in ErrorAlert.php .
Hello Admin,
There is a > on Laravel Server.
Error: >
File: >
Time >
Please do the needful.
3. Create Database Queue to process Jobs in background
Now to run the Jobs in background we need to setup a database queue drivers. So we will update .env as below. Refer Queue documentation on https://laravel.com/docs/8.x/queues.
Create the Queue Table in Database with following commands
$ php artisan queue:table Migration created successfully! $ php artisan migrate Migrating: 2021_08_26_180839_create_jobs_table Migrated: 2021_08_26_180839_create_jobs_table (52.85ms)
4. Create Job for Alert
Let’s create a simple Job app\Jobs\JobDevNotification.php . Creating Job will make sure that your ongoing operations will not be affected.
exception = $exception; > /** * Execute the job. * * @return void */ public function handle() < // Send Mail Mail::to("admin@example.com")->send(new ErrorAlert($this->exception)); > >
5. Start Queue, Setup SMTP & Done
Now update you .env file for SMTP Gateway Details and you are good to go!