Smtp mail send php

Here is an example of how to send an email using SMTP in PHP:

<?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 sent using PHP';

// Create a new PHPMailer object
$mail = new PHPMailer();

// Set the SMTP server settings
$mail->isSMTP();
$mail->Host = $smtp_server;
$mail->SMTPAuth = true;
$mail->Username = $smtp_username;
$mail->Password = $smtp_password;
$mail->SMTPSecure = 'tls'; // or 'ssl'
$mail->Port = $smtp_port;

// Set the email settings
$mail->setFrom($from_email);
$mail->addAddress($to_email);
$mail->Subject = $subject;
$mail->Body = $message;

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

?>

This code uses the PHPMailer library to send an email using SMTP. You'll need to install PHPMailer and configure it with your SMTP server settings.

Here's a breakdown of the code:

  1. We define the SMTP server settings, including the server name, port, username, and password.
  2. We define the email settings, including the from email, to email, subject, and message.
  3. We create a new PHPMailer object and set the SMTP server settings using the isSMTP(), Host, SMTPAuth, Username, Password, SMTPSecure, and Port methods.
  4. We set the email settings using the setFrom(), addAddress(), Subject, and Body methods.
  5. We send the email using the send() method. If the email is sent successfully, we echo a success message. If there's an error, we echo an error message with the error information.

Note that you'll need to replace the placeholders (your_email_username, your_email_password, [email protected], etc.) with your actual email settings.