Sunday, 3 March 2024

php send email SMTP

  if (empty($nameErr) && empty($emailErr) && empty($contactNumberErr)) {

        $dotenv = parse_ini_file('.env');
        // Retrieve SMTP server settings and email details from the .env file
        $smtpServer = $dotenv['MAIL_HOST'] ?? null;
        $smtpPort = $dotenv['MAIL_PORT'] ?? null;
        $username = $dotenv['MAIL_USERNAME'] ?? null;
        $password = $dotenv['MAIL_PASSWORD'] ?? null;
        // Email information
        $to = $dotenv['MAIL_SEND_TO']?? null;
        $subject = "Contact Form Submission";
        $headers = "From: " . ($dotenv['MAIL_FROM_NAME'] ?? null) . " <" . ($dotenv['MAIL_FROM_ADDRESS'] ?? null) . ">\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        // Email body - format details into a table

        $message='
        <!DOCTYPE html>
        <html lang="en">
       
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Contact Form Submission</title>
            <style>
                /* Resetting default margin and padding */
                body, h1, p, table {
                    margin: 0;
                    padding: 0;
                }
       
                /* Centering content */
                body {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    height: 100vh;
                    background: #EDF2F7;
                }
       
                /* Styling main container */
                .v1MsoNormalTable {
                    width: 70%;
                    max-width: 480px;
                    background: white;
                    border-radius: 2px;
                    box-shadow: 0 2px 0 rgba(0, 0, 150, 0.025), 2px 4px 0 rgba(0, 0, 150, 0.015);
                    margin: 0;
                    padding: 24px;
                    box-sizing: border-box;
                }
       
                /* Styling header */
                .v1MsoNormalTable h1 {
                    font-size: 13.5pt;
                    color: #3D4852;
                    margin-bottom: 10px;
                }
       
                /* Styling paragraphs */
                .v1MsoNormalTable p {
                    font-size: 11.5pt;
                    color: #74787E;
                    line-height: 18px;
                }
       
                /* Styling table */
                .v1MsoNormalTable table {
                    width: 100%;
                    border-collapse: collapse;
                    margin-bottom: 10px;
                }
       
                /* Styling table cells */
                .v1MsoNormalTable td {
                    padding: 7.5pt;
                    border-bottom: 1px solid #EDEFF2;
                }
       
                /* Styling table header */
                .v1MsoNormalTable thead td {
                    border-bottom: none;
                }
       
                /* Styling inner table cells */
                .innerTable .firstchildtd {
                    border-left: 3px solid #2D3748; /* Set left border with color */
                    padding-left: 10px; /* Add some padding to align content */
                }
                 .v1MsoNormalTable p.thanks {
                    text-align: left;
                    margin-top: 20px; /* Add some space between the table and the "Thanks, TastyBite" text */
                }
            </style>
        </head>
       
        <body>
            <table class="v1MsoNormalTable" border="0" cellspacing="0" align="center" cellpadding="0">
                <tbody>
                    <tr>
                        <td>
                            <div align="center">
            <h1><img src="path_to_your_logo_image" alt=" Logo"> Invitratech</h1>
        </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div align="center">
                                <h2>Hi Admin,</h2>
                                <p>We have received a new contact request. Please check the details below.</p>
                                <table class="innerTable"> <!-- Add innerTable class to apply custom styles -->
                                    <tr>
                                        <td class="firstchildtd">Name</td>
                                        <td>'.$name.'</td>
                                    </tr>
                                    <tr>
                                        <td class="firstchildtd">Email</td>
                                        <td>'.$email.'</td>
                                    </tr>
                                    <tr>
                                        <td class="firstchildtd">Contact No.</td>
                                        <td>'.$contact_number.'</td>
                                    </tr>
                                   
                                </table>
                                <p class="thanks">Thanks,<br>Invitratech</p>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </body>
       
        </html>
        ';
        // SMTP server configuration
        $smtpServer = "mail.invitratech.com";
        $smtpPort = 25;
        $username = "auth@invitratech.com";
        $password = "Au@2050@ITPL";

        // Create a socket connection to the SMTP server
        $socket = fsockopen($smtpServer, $smtpPort, $errno, $errstr, 30);

        if (!$socket) {
            echo "Failed to connect to SMTP server: $errstr ($errno)";
        } else {
            // Wait for the server to respond
            $response = fread($socket, 4096);

            // Send EHLO command to identify ourselves to the SMTP server
            fputs($socket, "EHLO example.com\r\n");
            $response = fread($socket, 4096);

            // Check if server supports STARTTLS (assuming it's not needed here)

            // Authenticate with the server
            fputs($socket, "AUTH LOGIN\r\n");
            $response = fread($socket, 4096);

            // Send username and password in base64 encoding
            fputs($socket, base64_encode($username) . "\r\n");
            $response = fread($socket, 4096);
            fputs($socket, base64_encode($password) . "\r\n");
            $response = fread($socket, 4096);

            // Send MAIL FROM command
            fputs($socket, "MAIL FROM:<$username>\r\n");
            $response = fread($socket, 4096);

            // Send RCPT TO command
            fputs($socket, "RCPT TO:<$to>\r\n");
            $response = fread($socket, 4096);

            // Send DATA command to start email transmission
            fputs($socket, "DATA\r\n");
            $response = fread($socket, 4096);

            // Send email headers and body
            fputs($socket, "Subject: $subject\r\n");
            fputs($socket, "$headers\r\n");
            fputs($socket, "$message\r\n");
            fputs($socket, ".\r\n");

            // Send QUIT command to close the connection
            fputs($socket, "QUIT\r\n");

            // Close the socket connection
            fclose($socket);

            header("Location: index.php?success=true");
            exit();
        }
    } else {
        include('index.php');
        exit; // Stop further execution

    }

Laravel Export data to csv

 use Illuminate\Http\Response; // Define a function to export data to CSV function exportToCSV($exportData, $columns) {     $filename = ...