Technical interview questions for software engineers — languages, databases, web, cloud, DevOps, and fundamentals. Browse all topics.

  • PHP Interview Questions

  • OOP Interview Questions

  • jQuery Interview Questions

  • MySQL Interview Questions

  • JavaScript Interview Questions

  • HR Interview Questions

  • HTML Interview Questions

  • C/C++ Interview Questions

  • Showing posts with label how to. Show all posts
    Showing posts with label how to. Show all posts

    How To Send Mail with Attachment in PHP

    How To Send Mail with Attachment in PHP
    I this tutorial,I will explain how to send mail with attachments in PHP.
    you can send attachments using a multipart/mixed content header.then convert the attachment into a base64 string.We are  creating a very simple script with php.
    the script sends an email to that address with a pdf file(attached file)that I specify in the script.

    Here's a simple php example of sending html mail with attached file:

    Example: How To Send Mail with Attachment in PHP
    <?php
    $file_path="files/test.pdf";//// Path to the file 
    $file_path_type = "application/pdf"; 
    $subject = "Testing File attachment"; 

    $headers = "From: ".$from. "\r\n" .
    "CC: php@example.com";
    $from ="test@example.com"; //From email.
    $message = "Hello!!<br>";
    $to ="rajesh@example.com"; //To email
    $file_path_name="test.pdf";

    $file = fopen($file_path,'rb');
    $data = fread($file,filesize($file_path));
    fclose($file);
    $semi_rand = md5(time());

    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    //Headers
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    $message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $message .= "\n\n";

    //convert the attachment into a base64 string
    $data = chunk_split(base64_encode($data));

    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$file_path_type};\n" .
    " name=\"{$file_path_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data .= "\n\n" .
    "--{$mime_boundary}--\n";

    $sent = @mail($to, $subject, $message, $headers);

    if($sent) {
    echo "Sent Succesfully";

    } else {
    die("Email not Delivered");
    }

    ?>

    I hope this will help you to send email with attachments in PHP.


    Bookmark and Share

    Difference between in_array() and array_search() in php?

    Difference between in_array() and array_search() in php?
    In this tutorial,we will study difference between in_array and array_search.

    in_array : -Checks if a value exists in an array
    array_search() :-searches an array for a given value and returns the  corresponding key if successful.

    in_array Syntax:
    in_array(search value, array , strict Boolean);
    search value:- will be the value to search in an array
    array: will the array to search
    strict Boolean: is optional.

    Example:

    <?php
    $name = array("Rajesh", "Sharma", "PHP", "Linux");
    if (in_array("Rajesh", $name)) 
    {
        echo "Yes";
    }
    else
    {
    echo "False";
    }
    ?>

    Output: Yes

    array_search() :-searches an array for a given value and returns the  corresponding key if successful.

    Syntax:
    array_search()(search value, array , strict Boolean);
    search value:- will be the value to search in an array
    array: will the array to search
    strict Boolean: is optional.

    Example:

    <?php
    $name = array(0=>"Rajesh", 1=>"Sharma", 2=>"PHP", 3=>"Linux");
    echo $key = array_search("Rajesh", $name);

    ?>
    Output: 0


    Bookmark and Share