Php smtp mail code

Here is an example of PHP code that uses SMTP to send an email:

<?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 and SMTP.';

// Create a new SMTP connection
$smtp = fsockopen($smtp_server, $smtp_port, $errno, $errstr, 30);

// Check if the connection was successful
if (!$smtp) {
    echo "Error connecting to SMTP server: $errstr ($errno)";
    exit;
}

// Send the SMTP commands
fputs($smtp, "HELO example.com\r\n");
fputs($smtp, "AUTH LOGIN\r\n");
fputs($smtp, base64_encode($smtp_username). "\r\n");
fputs($smtp, base64_encode($smtp_password). "\r\n");
fputs($smtp, "MAIL FROM: <". $from_email. ">\r\n");
fputs($smtp, "RCPT TO: <". $to_email. ">\r\n");
fputs($smtp, "DATA\r\n");
fputs($smtp, "Subject: ". $subject. "\r\n");
fputs($smtp, $message. "\r\n");
fputs($smtp, ".\r\n");

// Check if the email was sent successfully
if (ferror($smtp)) {
    echo "Error sending email: ". ferror($smtp);
    exit;
}

// Close the SMTP connection
fclose($smtp);

echo "Email sent successfully!";

?>

This code uses the fsockopen function to create a new socket connection to the SMTP server, and then sends the necessary SMTP commands to authenticate, send the email, and close the connection.

You'll need to replace the placeholders ($smtp_server, $smtp_port, $smtp_username, $smtp_password, $from_email, $to_email, $subject, and $message) with your own values.

Note that this is just an example, and you may need to modify the code to fit your specific needs. Additionally, you should be aware of the security implications of using plain text passwords and consider using a more secure method of authentication, such as TLS or SSL.