Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

How to send attachments using PHP Mail()

How to send attachments using PHP Mail()

In this post, we will going to learn how to can send an email with attachments using PHP mail() function. Sending an attachment with PHP mail() function is painful so people prefer to use PHP Mailer library, but in this post we are not going to use any library, we send attachments using PHP  mail() function and will Jquery and Ajax, so can send mail with out any page refresh.

We have to create to file on question.php , send.php , question.php .
Create question.php with the code given below better copy paste in body tag…

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<form id="preparingMathForm" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-12 form-group">
<label for="">Student's Name *</label>
<input type="text" name="student_name" class="form-control" placeholder="Name of Student " required/>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="">Parent / Guardian's Name *</label>
<input type="text" name="parent_name" class="form-control" placeholder="Parent / Guardian's Name" required/>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label for="">Contact No *</label>
<input type="text" name="contact_no" class="form-control" placeholder="Contact No" required/>
</div>
<div class="col-md-6 form-group">
<label for="">Email ID *</label>
<input type="text" name="email" class="form-control" placeholder="Email ID" required/>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label for="">School Name *</label>
<input type="text" name="school_name" class="form-control" placeholder="School Name" required/>
</div>
<div class="col-md-6 form-group">
<label for="">Select a Class *</label>
<select name="student_class" id="student_class" class="change form-control" required>
<option value="">Select a Class</option>
<option value="V">V</option>
<option value="VI">VI</option>
<option value="VII">VII</option>
<option value="VIII">VIII</option>
<option value="IX">IX</option>
<option value="X">X</option>
</select>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label for="">Topic Name *</label>
<input type="text" name="topic_name" class="form-control" placeholder="Topic Name" required/>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="">Write the Problem *</label>
<textarea name="problem" class="form-control" rows="7" placeholder="Write the Problem" required></textarea>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="">Attach the Screen shot ( Can use Jpg, PNG, Maximum size: 2MB)*</label>
<input type="file" name="my_files[]" />
</div>
</div>
<div class="row">
<div class="col-md-4 form-group">
<button type="submit" name="submit" class="btn btn-success btn-md ">Submit</button>
</div>
<div class="col-md-8 form-group">
<div id="processpm"></div>
</div>
</div>
</form>
<script>
$(document).ready(function(){
$("#preparingMathForm").on('submit',function(e){
e.preventDefault();
var formData = new FormData($(this)[0]);

$("#processpm").html('<i class="fa fa-spinner fa-spin fa-3x fa-fw margin-bottom"></i><span class="sr-only">Loading...</span>');

$.ajax({
url:'send.php',
type:'post',
dataType: "json",
data: formData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false,
enctype: 'multipart/form-data',
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(sp) // A function to be called if request succeeds
{
if(sp.res== 'error'){
$('#processpm').html('<div class="alert alert-danger">'+sp.msg+'</div>');
}else{
$('#processpm').html('<div class="alert alert-success">'+sp.msg+'</div>');
//$('#preparingMathForm')[0].reset();
}
}

});

});
});
</script>

Create File send.php and use code give below, don’t forget to change email id of to and from.

<?php
//var_dump($_POST);
function cspamcheck($field) {
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
return TRUE;
} else {
return FALSE;
}
}

$msg=array();

$student_name=$_POST['student_name'];
$parent_name=$_POST['parent_name'];
$contact_no=$_POST['contact_no'];
$email=$_POST['email'];
$school_name=$_POST['school_name'];
$student_class=$_POST['student_class'];
$topic_name=$_POST['topic_name'];
$problem=$_POST['problem'];

$tmp_name = $_FILES['my_file']['tmp_name']; // get the temporary file name of the file on the server
$name = $_FILES['my_file']['name']; // get the name of the file
$size = $_FILES['my_file']['size']; // get size of the file for size validation
$type = $_FILES['my_file']['type']; // get type of the file
$error = $_FILES['my_file']['error']; // get the error (if any)

if($student_name==""){
$msg['res']='error';
$msg['msg']="Enter Student Name";
}else if($parent_name==""){
$msg['res']='error';
$msg['msg']="Enter Parent / Guardian Name";
}else if($contact_no==""){
$msg['res']='error';
$msg['msg']="Enter a Contant no.";
}else if($email==""){
$msg['res']='error';
$msg['msg']="Enter a Email ID";
}else if($school_name==""){
$msg['res']='error';
$msg['msg']="Enter School Name";
}else if($student_class==""){
$msg['res']='error';
$msg['msg']="Select a Class";
}else if($size>=200000){
$msg['res']='error';
$msg['msg']="Check File Size";
}else{
$mailcheck = cspamcheck($email);
if ($mailcheck==FALSE) {
$msg['res']='error';
$msg['msg']="Email ID is not Valid ";
}
else {
error_reporting(E_ALL);
ini_set('display_errors', 1);
$subject="ASK A QUESTION";
$to='[email protected]';
$from='[email protected]';

$attachments = $_FILES['my_files'];
$file_count = count($attachments['name']); //count total files attached
$boundary = md5("codentricks.com");
// $headers = "From: $from";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from."\r\n";
$headers .= "Reply-To: ".$to."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

$msgi="Name : ".$student_name."\r\n";
$msgi.="Parent / Guardian Name : ".$parent_name."\r\n";
$msgi.="Contact no. : ".$contact_no."\r\n";
$msgi.="School : ".$school_name."\r\n";
$msgi.="Class : ".$student_class."\r\n";
$msgi.="Topic Name : ".$topic_name."\r\n";
$msgi.="Problem : ".$problem."\r\n";

$attachments = $_FILES['my_files'];

$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($msgi));


for ($x = 0; $x < $file_count; $x++){
if(!empty($attachments['name'][$x])){

if($attachments['error'][$x]>0) //exit script and output error if we encounter any
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
print $mymsg[$attachments['error'][$x]];
exit;
}

//get file info
$file_name = $attachments['name'][$x];
$file_size = $attachments['size'][$x];
$file_type = $attachments['type'][$x];

//read file
$handle = fopen($attachments['tmp_name'][$x], "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)

$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
}
}if(mail($to, $subject, $body, $headers)){
$msg['res']='success';
$msg['msg']="Your request sent, we will soon notify you regarding your query";}else{
$msg['res']='error';
$msg['msg']="Something went Wrong";
}
}
}
echo json_encode($msg);
?>

Python program to sum all the digits of an input number

Python program to sum all the digits of an input number

In this tutorial, we are going to learn how to sum all the digits of an input number using a Python program, this kind of simple problem can be asked in CBSE class XI / XII “Computer Science with Python” Examination.

Environment used :

  • OS : Linux
  • Python Version : Python 3.6.9
  • Text Editor : Atom ( you can use Notepad, Notepad++, Kate , Geany, IDLE …)

Lets move to our program  how to sum all the digits of an input number using a Python , for this we have to create a file named  addDigit.py with code given below, now open terminal /cmd, use CD command to go there and run

for Linux : python3 addDigit.py

for Windows user : python addDigit.py

# Programe : how to sum all the digits of an input number
# Created by Sanjay Prasad
no = int(input("Enter a Positive Number: "))
result = 0
noORG = no

while no > 0:
    rem = no % 10
    result = result + rem
    no = int(no/10)

print("Sum of all digits of no. ", noORG, "is: ", result)

Screen shot of Out put :

How to install Xampp on Linux

How to install Xampp on Linux

In this tutorial we are going to learn Installing latest version of Xampp on Linux , setting Linux htdocs folder permissions issue, How to add a Linux environment variable for PHP, Installing Composer “A Dependency Manager for PHP”.

XAMPP is a completely free, easy to install Apache distribution containing MySQL / MariaDB, PHP, and Perl. The XAMPP open source package has been set up to be incredibly easy to install and to use. Best part is Xampp is cross-platform tool available for Windows , Mac and Linux.

We are going to break this tutorial in several parts to make this tutorial easy to use :

Part 1 : Xampp Installation

visit https://www.apachefriends.org to download latest version of xampp. like I have downloaded xampp in /home/sanjay/download directory

Now Open Terminal / Konsole and using cd command move
to /home/sanjay/download

Now run command
sudo chmod 755 xampp-linux-*-installer.run
sudo ./xampp-linux-*-installer.run

Part 2 :  Fixing Linux htdocs folder permissions issue

Now Open Terminal / Konsole and run commands given below

sudo chown -R daemon:daemon /opt/lampp/htdoc
sudo chmod 777 -R /opt/lampp/htdocs

Part 3 : Setting PHP environment variable

Method 1 (use when method 2 fails) :
sudo nano /etc/environment
add
:/opt/lampp/bin/php
in last and save and run
sudo ln -s /opt/lampp/bin/php /usr/local/bin/php

Method 2 (Recommended) :
export PATH=$PATH:/opt/lampp/bin/php
sudo ln -s /opt/lampp/bin/php /usr/local/bin/php

Part 4 : Installing Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Part 5: Xampp Control Panel GUI

sudo nano /usr/share/applications/firefox-developer.desktop
and paste

[Desktop Entry]
Encoding=UTF-8
Exec=opt/lampp/manager-linux-x64.run
Icon=opt/lampp/htdocs/favicon.ico
Type=Application
Terminal=false
Comment=XAMPP Control Panel
Name=XAMPP Control Panel
GenericName=XAMPP Control Panel
StartupNotify=false
Category=Development;System;

then press CTRL+X and type y and Press Enter.

Xampp Common commands

sudo /opt/lampp/lampp start (Will Start Xammp)
sudo /opt/lampp/lampp stop (will Stop Xampp)
sudo /opt/lampp/lampp restart (will restart Xampp)

Xampp configurable files

The main XAMPP configuration files are located as follows:

Apache configuration file: /opt/lampp/etc/httpd.conf, /opt/lampp/etc/extra/httpd-xampp.conf
PHP configuration file: /opt/lampp/etc/php.ini
MySQL configuration file: /opt/lampp/etc/my.cnf
ProFTPD configuration file: /opt/lampp/etc/proftpd.conf

If you still facing any problem then please comment your problem , I will try to fix those problems.

MX Linux 19.2 with First KDE Plasma desktop officially released

MX Linux 19.2 with First KDE Plasma desktop officially released

So wait is over, MX Linux releases its much awaited First KDE Plasma desktop Centric “MX Linux 19.2 KDE edition”, right now on distrowatch.com MX Linux is trending at no. 1 for long time. The new version 5.14.5 (KDE Plasma) ships with  MX Tools , antiX Live usb system, and snapshot technology.

As per MX Linux “MX-19.2 KDE is an Advanced Hardware Support (AHS) enabled 64-bit only version of MX featuring the KDE/plasma desktop. Applications utilizing Qt library frameworks are given a preference for inclusion on the iso. ”

MX Linux Specification

  • KDE/plasma 5.14.5 Debian buster version)
  • GIMP 2.10.12
  • MESA 20.0.7 (AHS)
  • MX AHS firmware package
  • Debian (AHS) 5.6 kernel
  • Browser: Firefox 79
  • Video Player: VLC 3.0.11
  • Music Manager/Player: Clementine 1.3.1
  • Email client: Thunderbird 68.11
  • Office suite: LibreOffice 6.1.5 (plus security fixes)

MX Linux 19.2 KDE Edition  Download Link

For Downloading MX Linux 19.2 KDE edition   Click Here

If you want to read Release Announcement then Click Here

Samsung Galaxy M01 Budget Smart Phone for India Consumer

Samsung Galaxy M01 Budget Smart Phone for India Consumer

Right now Indian’s started boycotting Chines Origin Smart Phone but lack of option push Indian’s to buy Samsung smart phone, now to grab the opportunity Samsung comes with a new Smart phone named Samsung Galaxy M01 with 3GB Ram and 32GB Rom.

Samsung priced this phone around Rs. 8999 but one can get it with some discount on online festival sale on Flipkart or amazon.

Samsung Galaxy M01 hardware specification

with Snapdragon 439 SOC at this price it seems a good buy.

Connectivity and OS :
Dual Sim (4G + 4G), Nano+Nano
Android v10 (Q)  with Samsung One UI
Wi-Fi 802.11, b/g/n
Bluetooth v4.2
A-GPS, Glonass
Face unlock
Remember it lacks finger Print Sensor

Display :
5.71 inches (14.5 cm) , 720 x 1560 pixels ,
Aspect Ratio : 19:9 ,Waterdrop notch, 295 ppi
IPS LCD, Screen to Body Ratio :77.43 %

Processor, RAM and Storage :
Qualcomm Snapdragon 439
3GB RAM , 32GB Storage, Up to 512GB,OTG supported

Battery : 4000 mAh

Camera :
Front : 5MP
Rear : 13 MP +2MP with LED Flash

Conclusion :

Samsung Galaxy M01 is a good budget oriented smart phone that provides you better display with decent camera and its face unlock 8/10 times.

Linux command line tutorial for Beginners

Linux command line tutorial for Beginners

In this tutorial we are going to learn Linux’s basic commands that can make your life easier, if you are new to Linux  then this tutorial is for you. I am trying to make this tutorial easy to use as purely target to beginners, if you have any doubt then comment, I will try to solve your queries.

Lets move to the topic,  if you are going learn commands first  you have to about Linux.

What is Linux ?

As per Wikipedia “Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds”, In simple terms its a operating system like Windows 7, 10 , MAC.

Now move to Linux Commands, Just like Windows XP, 7 or 10 “command prompt” , Linux has more powerful alternative called Terminal, Konsole ….

What is  a Command ?

“A command is a directive to a computer program to perform a specific task. It may be issued via a command-line interface, such as a shell, or as input to a network service as part of a network protocol, or as an event in a graphical user interface triggered by the user selecting an option in a menu”.

For Example if you run command ls in terminal , it will display list of files and folder. lets have a look on Screen shot given below

List of basic Linux Commands

Below in the table I am trying to list basic commands and their usages

Command Usages
cd also known as chdir (change directory), Open terminal and run
cd foldername
pwd also known as present working directory, Open terminal and run
pwd
it will output like home/sanjay
mkdir also known as make directory, it used to create directory, Open terminal and run
mkdir codentricks
it will create a directory codentricks
ls also known as list directory, Open terminal and run
ls
it will display list of files and folder
cat Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives their content as output. It helps us to create, view, concatenate files. So let us see some frequently used cat commands., open terminal and run

cat sanjay.txt

if will display content of sanjay.txt, for creating a file name codentricks.docx open terminal and run

cat >codentricks.docx

Lava Z66 a Made in India Budget Smart Phone

Lava Z66 a Made in India Budget Smart Phone

Now Indians willing to buy phone made by Indian companies but actually option is limited, if you go to buy a smart phone from Amazon or Flipkart than almost 90% phones are Chinese origin. So to grab the opportunity Indian Company Lava released Lava Z66 on 4th August 2020.

Lava Z66 hardware specification

If you talk about its specification then it can’t compete Chinese origin phone, but if we buy Indian Origin phones then in long run we will see Indian companies can compete Chinese brands. Now comes to topic lets review Lava Z66 hardware specification :

Connectivity and OS :
Dual Sim (4G + 4G), Nano+nano
Android v10 (Q)
Wi-Fi 802.11, b/g/n
Bluetooth v4.2
A-GPS, Glonass
finger Print Sensor

Display :
6.08 inches (15.44 cm) , 720 x 1560 pixels ,
Aspect Ratio : 19:9 ,Waterdrop notch, 283 ppi
IPS LCD, Screen to Body Ratio :79.17 %

Processor, RAM and Storage :
Octa core, 1.6 GHz , Lava didn’t reveal processor information but some benchmark shows it uses Unisoc processor.
3GB RAM , 32GB Storage, Up to 128 GB,OTG supported

Battery :
3950 mAh , Talktime : 16h for 3G and 4G, 38hr for 2G*
Standby : 720 Hr with Data Off, 650 Hr with data On (Single SIM)*

Camera :
Front : 13 MP f/2.2 Primary Camera(1.12µm pixel size), Flash
Rear : 13 MP f/2.0 Primary Camera(1.12µm pixel size)5 MP, Depth Camera,Flash

Conclusion :

Overall good phone priced at Rs. 7777, in this price you can’t think about gaming, this phone seriously not for gaming, its good for internet surfing, YouTube streaming …. with weak processor you can’t do you can feel sluggish interface despite of 3GB RAM.

How to install latest Firefox Developer Edition on Linux

How to install latest Firefox Developer Edition on Linux

Firefox developer edition  is the special version of Firefox developed for developer. In this tutorial we will learn how to install Firefox developer edition on any Linux Distro

We have tested this tutorial on Kubuntu 18.04 and it works and installed version 80.0b6 64 bit version, lets divide this tutorial in 3 part :

  1. Downloading Firefox Developer Edition
  2. Extracting and copying to /opt/ folder
  3. Creating a desktop / menu icon by creating .desktop file

 

1. Download a Firefox developer Edition

Just google Firefox developer Edition , you get link or click here Download
I have downloaded file named “firefox-80.0b6.tar.bz2” in download folder.

2. Extracting and copying to /opt/ folder

Open Terminal /Konsole and using command “cd” navigate to download folder and run the command given below
tar -xvfz ./firefox-80.0b6.tar.bz2 /opt
or
sudo tar -xvfz ./firefox-80.0b6.tar.bz2 /opt

3. Creating Firefox developer edition desktop / menu icon by creating .desktop file

Open Terminal and run command
nano /usr/share/applications/firefox-developer.desktop
or
sudo nano /usr/share/applications/firefox-developer.desktop

and paste

Desktop Entry]
Encoding=UTF-8
Exec=/opt/firefox/firefox
Icon=/opt/firefox/browser/chrome/icons/default/default64.png
Type=Application
Terminal=false
Comment=Firefox Developer Edition
Name=Firefox Developer Edition
GenericName=Firefox Developer
StartupNotify=false
Categories=Browser;

then press CTRL+X and type y and Press Enter.

Watch How to install latest Firefox Developer Edition on Linux Video

Laravel Tutorial Part 1 : Installation

Laravel Tutorial Part 1 : Installation

In this tutorial we will going to learn how to install Laravel latest version on Xampp or Lampp, Basic requirements to run Laravel, how to use composer command to create laravel projects and much more ..

Offical Website :
https://laravel.com/

Laravel Project Requirements :

  • Xampp / Lampp / Homestead / Valet
  • Composer
  • PHP >= 7.2.5
  • BCMath PHP Extension
  • Ctype PHP Extension
  • Fileinfo PHP extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

Video Tutorial : Installing Xampp and Laravel on Linux

Creating Laravel Project :

Using Composer :

composer create-project --prefer-dist laravel/laravel Projectname

Using Laravel Installer :
First we need to install Laravel install via composer, Open Terminal / CMD and run command

composer global require "laravel/installer"

then create project

laravel new Projectname

Testing Laravel

Go Inside Projectname and open terminal/cmd and
Run command
php artisan serve
and visit
http://localhost:8000

Laravel Tutorial Part 1 : Installation Video

GeckoLinux an OpenSUSE based Linux Distro

GeckoLinux an OpenSUSE based Linux Distro

Do you want to use OpenSuse but problem installing media codecs then you can try GeckoLinux, a desktop-oriented distribution based on openSUSE. Recently GeckoLinux linux released updated spins of its STATIC series(152.X), generated from openSUSE Leap 15.2 and Packman package repositories.

Why Gecho Linux ?

GeckoLinux continues to be focused on eliminating pain points and polishing its unique out-of-the-box configuration on top of the stable and flexible openSUSE base. Proprietary media formats play out of the box, and additional user-installed multimedia applications work automatically with restricted media codecs thanks to the prioritized inclusion of the Packman repository. Google and Skype repositories are also configured out-of-the-box for optional installation by the user of proprietary applications from those vendors. Third-party RPM packages can be easily installed using the graphical YaST package manager. GeckoLinux uses the Calamares system installer at version 3.2.15, providing easy but powerful options for reliable installation of the live system.

To go through Project’s Release Announcement click here

Download GeckoLinux

A variety of GeckoLinux ISO spins are available with polished desktop environments to suit every need and preference.

Do you have any Query / Suggestion