Don Alexander Eckford's code for standard email:


Warning: Undefined array key "SMTP_HOST" in /home/eckfordd/public_html/citw185/examples/email4.php on line 35

Warning: Undefined array key "SMTP_USER" in /home/eckfordd/public_html/citw185/examples/email4.php on line 37

Warning: Undefined array key "SMTP_PASS" in /home/eckfordd/public_html/citw185/examples/email4.php on line 38

Warning: Undefined array key "SMTP_PORT" in /home/eckfordd/public_html/citw185/examples/email4.php on line 40

Warning: Undefined array key "EMAIL_FROM" in /home/eckfordd/public_html/citw185/examples/email4.php on line 43

Warning: Undefined array key "EMAIL_FROM_NAME" in /home/eckfordd/public_html/citw185/examples/email4.php on line 43

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/eckfordd/public_html/citw185/phpmailer/PHPMailer.php on line 1314
Email sending failed: Invalid address: (From):
 

CODE FOLLOWS

<?php

    
include('includes/header.php');

    
error_reporting(E_ALL);
    
ini_set('display_errors'1);

    echo(
"<h1>Don Alexander Eckford's code for standard email:</h1>");

    use 
PHPMailer\PHPMailer\PHPMailer;
    use 
PHPMailer\PHPMailer\Exception;
    use 
PHPMailer\PHPMailer\SMTP;

    
// Include PHPMailer files
    
require '../phpmailer/Exception.php';
    require 
'../phpmailer/PHPMailer.php';
    require 
'../phpmailer/SMTP.php';

    
// Manually parse .env file
    
$envFile __DIR__ '../.env';
    if (
file_exists($envFile)) {
        
$lines file($envFileFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES);
        foreach (
$lines as $line) {
            list(
$key$value) = explode('='$line2);
            
$_ENV[trim($key)] = trim($value);
        }
    }

    try {
        
// Create a new PHPMailer instance
        
$mail = new PHPMailer(true);

        
// SMTP Configuration
        
$mail->isSMTP();
        
$mail->Host $_ENV['SMTP_HOST']; // SMTP server
        
$mail->SMTPAuth true;
        
$mail->Username $_ENV['SMTP_USER']; // SMTP username
        
$mail->Password $_ENV['SMTP_PASS']; // SMTP password
        
$mail->SMTPSecure PHPMailer::ENCRYPTION_STARTTLS// Encryption protocol
        
$mail->Port $_ENV['SMTP_PORT']; // SMTP port

        // Sender and recipient information
        
$mail->setFrom($_ENV['EMAIL_FROM'], $_ENV['EMAIL_FROM_NAME']); // From email and name
        
$mail->addAddress($_ENV['EMAIL_TO']); // Recipient email

        // Email content
        
$mail->Subject 'Manually Downloaded PHPMailer Example';
        
$mail->isHTML(true); // Set email format to HTML
        
$mail->Body '<h1>Email sent using manually downloaded PHPMailer!</h1><p>Enjoy your email.</p>';
        
$mail->AltBody 'This is the plain text version of the email content.'// Plain-text version

        // Send the email
        
$mail->send();
        echo 
"Email sent successfully!";
    } catch (
Exception $e) {
        
// Handle errors
        
echo "Email sending failed: {$mail->ErrorInfo}";
    }

    include(
'includes/footer.php');
?>