Monday, November 12, 2018

Send Email with Attachment in PHP

Sending email from the script is the very useful functionality in the web application. Most of the website used the email sending feature to send the notifications. If your web application uses PHP, it’s very easy to send email from the script using PHP.
PHP provides an easy way to send email from the website. Using mail() function in PHP, you can send text or HTML email. But sometimes email functionality needs to be extended for sending an attachment with the mail. In this tutorial, we will show you how to send email with attachment in PHP. Our example script makes it simple to send text or HTML email including any types of files as an attachment (like image, .doc, .docx, .pdf, .txt, etc.) using PHP.

Send HTML Email with Attachment

The PHP mail() function with some MIME type headers is used to send email with attachment in PHP. You need to specify the recipient email ($to), sender name ($fromName), sender email ($from), subject ($subject), file to attach ($file), and body content to send ($htmlContent). The following script lets you send the both type of message (text or HTML) with attachment file to the email.
<?php//recipient$to 'recipient@example.com';
//sender$from 'sender@example.com';$fromName 'CodexWorld';
//email subject$subject 'PHP Email with Attachment by CodexWorld'//attachment file path$file "codexworld.pdf";
//email body content$htmlContent '<h1>PHP Email with Attachment by CodexWorld</h1>
    <p>This email has sent from PHP script with attachment.</p>';
//header for sender info$headers "From: $fromName"." <".$from.">";
//boundary $semi_rand md5(time()); $mime_boundary "==Multipart_Boundary_x{$semi_rand}x"//headers for attachment $headers .= "\nMIME-Version: 1.0\n" "Content-Type: multipart/mixed;\n" " boundary=\"{$mime_boundary}\""//multipart boundary $message "--{$mime_boundary}\n" "Content-Type: text/html; charset=\"UTF-8\"\n" ."Content-Transfer-Encoding: 7bit\n\n" $htmlContent "\n\n"//preparing attachmentif(!empty($file) > 0){
    if(is_file($file)){
        $message .= "--{$mime_boundary}\n";
        $fp =    @fopen($file,"rb");
        $data =  @fread($fp,filesize($file));

        @fclose($fp);
        $data chunk_split(base64_encode($data));
        $message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" . 
        "Content-Description: ".basename($file)."\n" .
        "Content-Disposition: attachment;\n" " filename=\"".basename($file)."\"; size=".filesize($file).";\n" . 
        "Content-Transfer-Encoding: base64\n\n" $data "\n\n";
    }
}$message .= "--{$mime_boundary}--";$returnpath "-f" $from;
//send email$mail = @mail($to$subject$message$headers$returnpath); 
//email sending statusecho $mail?"<h1>Mail sent.</h1>":"<h1>Mail sending failed.</h1>";
The example script allows you to send single attachment to the email.
Sending Email to Multiple Recipients:
You can send email to the multiple recipients at once with Cc and Bcc. Use the Cc and Bcc headers for sending email with attachment to multiple recipients in PHP.
<?php// Cc email$headers .= "\nCc: mail@example.com";
// Bcc email$headers .= "\nBcc: mail@example.com";

Conclusion

Here we have provided the easiest way to send email with attachment in PHP. You don’t need to include any library to send HTML email with attachment. Using PHP default mail() function you can easily send email with attachment.



Wednesday, October 10, 2018

Front End Post Submission Form in WordPress


The process for creating a front-end submission form for posts, pages, or custom post types is actually quite simple, even though it is often thought of as being very difficult. There are just two major components to a front-end submission form:
  1. An HTML form for the data to be entered into
  2. A function that listens for the form submission and then processes the submitted data
The code written in the video, which consists of just two functions, is less than 80 lines and contains everything needed for a pretty decent front-end submission form, even if it is pretty basic. It introduces everything you need to know for getting started with building these kinds of forms and opens up the door to countless possibilities.
<?php
function create_post_form() {
ob_start();
if(isset($_GET['post'])) {
switch($_GET['post']) {
case 'successfull':
echo '<p class="success">' . __('Job created', 'gocloud') . '</p>';
break;
case 'failed' :
echo '<p class="error">' . __('Please fill in all the info', 'gocloud') . '</p>';
break;
}
}
?>
<form id="pippin_create_post" action="" method="POST">
<fieldset>
<input name="job_title" id="job_title" type="text"/>
<label for="job_title">Job Title</label>
</fieldset>
<fieldset>
<input name="user_name" id="user_name" type="text"/>
<label for="user_name">Your Name</label>
</fieldset>
<fieldset>
<input name="user_email" id="user_email" type="email"/>
<label for="user_email">Your Email</label>
</fieldset>
<fieldset>
<label for="job_desc">Job Description</label>
<textarea name="job_desc" id="job_desc"></textarea>
</fieldset>
<fieldset>
<input name="inquiry_email" id="inquiry_email" type="email"/>
<label for="inquiry_email">Inquiry Email</label>
</fieldset>
<fieldset>
<?php wp_nonce_field('jobs_nonce', 'jobs_nonce_field'); ?>
<input type="submit" name="job_submit" value="<?php _e('Submit Job Posting', 'gocloud'); ?>"/>
</fieldset>
</form>
<?php
return ob_get_clean();
}
add_shortcode('post_form', 'create_post_form');

function process_post_creation() {
if(isset($_POST['jobs_nonce_field']) && wp_verify_nonce($_POST['jobs_nonce_field'], 'jobs_nonce')) {

if(strlen(trim($_POST['job_title'])) < 1 || strlen(trim($_POST['job_desc'])) < 1) {
$redirect = add_query_arg('post', 'failed', home_url($_POST['_wp_http_referer']));
} else {
$job_info = array(
'post_title' => esc_attr(strip_tags($_POST['job_title'])),
'post_type' => 'jobs',
'post_content' => esc_attr(strip_tags($_POST['job_desc'])),
'post_status' => 'pending'
);
$job_id = wp_insert_post($job_info);

if($job_id) {
update_post_meta($job_id, 'ecpt_postedby', esc_attr(strip_tags($_POST['user_name'])));
update_post_meta($job_id, 'ecpt_posteremail', esc_attr(strip_tags($_POST['user_email'])));
update_post_meta($job_id, 'ecpt_contactemail', esc_attr(strip_tags($_POST['inquiry_email'])));
$redirect = add_query_arg('post', 'successfull', home_url($_POST['_wp_http_referer']));
}
}
wp_redirect($redirect); exit;
}
}
add_action('init', 'process_post_creation');
?>

Note: This article is taken from (https://pippinsplugins.com/custom-front-end-post-submission-form/)

Saturday, June 9, 2018

Measuring the distance between two coordinates in PHP

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
      return ($miles * 1.609344);
  } else if ($unit == "N") {
      return ($miles * 0.8684);
  } else {
      return $miles;
  }
}

Results
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>";

Note: This code is taken from: http://www.geodatasource.com/developers/php

Tuesday, March 6, 2018

Dreamweaver fatal error XML parsing

Step 1: Close DW, Navigate to C:\User\uruser\AppData\Roaming\Adobe\Dreamweaver CS5\en_US\Configureation\Workspace\ and delete the specific xml file

Re-open DW. DW should auto-create your Workspace layout XML file again. See if it works fine.

PROCEED TO STEP 2 ONLY IF STEP 1 DOESN'T SOLVE THE ISSUE

Step 2: Navigate to C:\Users\useruser\AppData\Roaming\Adobe\Dreamweaver CS5\en-US\Configuration and delete the entire 'CONFIGURATION' folder. Re-open DW. DW should auto-create your configuration folder based on predefined layouts and config options. This will definitely fix your issue.

 Reason for this issue: This issue would have been caused due to a malformed workspace layout configuration. That may happen due to customizations you may have done to the layout/ improper file permissions/ improper shutdown on Windows.

NOTE: Reference link is https://forums.adobe.com/thread/996876

Sunday, March 4, 2018

Remove White space in Dreamweaver

Isn’t it insanely annoying some times when you open a CSS File or a HTML File only to find Dreamweaver has added loads of blank lines, if your CSS is more white space than code then running this script will be very helpful.
I’ve used a tonne of CSS Compressors to fix Dreamweavers fail of blank lines in the past, but most of them do not support CSS 3, and the ones that do – seemingly do not support the transition effect.  This however, works fine with PHP, HTML and CSS (self tested).  So no more tears for those added line breaks in your PHP or CSS, just run this fix!
Open the file in Adobe Dreamweaver, press CTRL+F to load the Find & Replace dialog box (or Edit > Find and Replace).
Do the search on the source code view.
Check the box “Use regular expression” and un-check any other boxes.
Find: [\r\n]{2,}
Replace: \n
The hit “replace all”
It may take a while so fit back and wait whilst your CSS / HTML or PHP Document is flushed of all those annoying line breaks!
NOTE:  This Article is taken from http://www.itsadam.co.uk/dreamweaver-fix-removing-extra-line-breaks-in-css-html-php/

Friday, March 2, 2018

Simple php function to get coordinates from address through google services

Here I am showing you how to get coordinates from address using google api (Thanks to Google). The result will solely depends on how google support it in the future. Because google will set the 1st index result for the most relevant, thus this works quite well in general.
Function in php:
function getCoordinates($address){
 
$address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
 
 
$response = file_get_contents($url);
 
$json = json_decode($response,TRUE); //generate array object from the response from the web
 
return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
 
}


echo getCoordinates('740 Story Rd San Jose CA 95122');



Note*: This article is taken from https://colinyeoh.wordpress.com/2013/02/12/simple-php-function-to-get-coordinates-from-address-through-google-services/

How to get coordinates from address through google services

Here is the code

<!DOCTYPE html>
<html>
<body>
<div id="googleMap" style="width:100%;height:800px;"></div>

<script>
function myMap() {
var mapProp= {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

google.maps.event.addListener(map, 'click', function(event) {
alert(event.latLng.lat() + ", " + event.latLng.lng());
});

}
</script>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyADIXqFBFbbBy3fs-IRL-LH1ozRN4QhgJM&callback=myMap"></script>
</body>
</html>

How to change the PHP version for subfolders or subdomains

  How to change the PHP version for subfolders or subdomains Setting a specific PHP version for a specific websites, subfolders or subdomain...