Php send mail smtp authentication

To send an email using SMTP authentication in PHP, you can use the mail() function or a PHP mail library such as PHPMailer. Here's an example of how to use the mail() function with SMTP authentication:

<?php
// Define the SMTP server settings
$smtp_server = 'smtp.example.com';
$smtp_port = 587;
$smtp_username = 'your_email_username';
$smtp_password = 'your_email_password';

// Define the email settings
$from_email = '[email protected]';
$to_email = '[email protected]';
$subject = 'Test email';
$message = 'This is a test email';

// Set the SMTP server settings
ini_set('SMTP', $smtp_server);
ini_set('smtp_port', $smtp_port);

// Set the authentication settings
ini_set('auth_username', $smtp_username);
ini_set('auth_password', $smtp_password);

// Send the email
mail($to_email, $subject, $message, 'From: '. $from_email);

?>

In this example, we're using the ini_set() function to set the SMTP server settings, authentication settings, and other email settings. We're then using the mail() function to send the email.

If you're using PHPMailer, you can use the following code:

<?php
require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';

// Create a new PHPMailer instance
$mail = new PHPMailer\PHPMailer\PHPMailer();

// Set the SMTP server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email_username';
$mail->Password = 'your_email_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Set the email settings
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
$mail->Subject = 'Test email';
$mail->Body = 'This is a test email';

// Send the email
if (!$mail->send()) {
    echo 'Error sending email: '. $mail->ErrorInfo;
} else {
    echo 'Email sent successfully!';
}
?>

In this example, we're using the PHPMailer library to create a new instance of the PHPMailer class. We're then setting the SMTP server settings, authentication settings, and other email settings using various methods provided by the PHPMailer class. Finally, we're sending the email using the send() method.

Make sure to replace the placeholders (smtp.example.com, your_email_username, your_email_password, [email protected], and [email protected]) with your actual email settings.