How can PHP code be configured to work with different mail servers without using web-based accounts?
To configure PHP code to work with different mail servers without using web-based accounts, you can utilize the PHPMailer library which allows you to specify the SMTP server settings directly in your code. This way, you can easily switch between different mail servers without having to rely on web-based accounts.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@example.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
//Recipients
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.net', 'Recipient Name');
//Content
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Keywords
Related Questions
- How can the PHP code be modified to ensure that the included file appears correctly within the table column?
- How can the security and integrity of scanned documents be maintained when using MD5 checksum for file identification in a database?
- How can the use of the "httponly" parameter in setcookie function impact cookie functionality in PHP?