Complete Guide to Installing and Using PHPMailer for Contact Forms
PHPMailer is a widely-used, open-source PHP library designed to simplify email sending through advanced SMTP functionality. With support for authentication, HTML content, attachments, and robust error handling, it’s an essential tool for modern web developers.
This guide walks you through the entire process of downloading, installing, configuring, and using PHPMailer to handle email functionality in your contact forms on Domain India hosting platforms.
🔽 1. How to Download and Install PHPMailer
Setting up PHPMailer is the first step toward integrating professional-grade email functionality into your PHP application. You can either download it manually or install it via Composer — the recommended method.
📦 Option 1: Manual Download via GitHub
This method is ideal if you do not use Composer or have limited access to terminal commands.
Steps:
-
Go to the official GitHub repository: PHPMailer GitHub
-
Click the Code button, then select Download ZIP to get the latest version.
-
Extract the ZIP file on your local system.
-
Upload the extracted
src
directory to your hosting server. -
Place it in a publicly accessible location, for example:
public_html/PHPMailer/
Note: Make sure the PHPMailer
folder contains files like PHPMailer.php
, SMTP.php
, and Exception.php
inside the src
directory.
⚙️ Option 2: Install with Composer (Preferred Method)
Composer is a dependency manager for PHP. If you have terminal or SSH access, this method ensures future updates and integrations are seamless.
Steps:
-
Open your terminal or access the command-line interface via your hosting control panel.
-
Navigate to your project root (where your PHP scripts reside).
-
Run the following command:
composer require phpmailer/phpmailer
This will:
-
Download the PHPMailer package and its dependencies
-
Create a
vendor/
folder with PHPMailer inside it -
Generate an
autoload.php
file you can include in your PHP scripts
Benefits of using Composer:
-
Handles dependencies and updates
-
Cleaner integration
-
Widely supported in modern PHP development workflows
✅ Next Step: After installation, proceed to the setup section to learn how to include and use PHPMailer in your scripts effectively.
🔧 2. Basic PHPMailer Setup
After downloading or installing PHPMailer, you need to include it in your PHP scripts to start using its features. The method of inclusion depends on how you obtained PHPMailer.
📁 Manual File Inclusion (GitHub Download)
If you downloaded PHPMailer manually from GitHub and uploaded it to your server, you need to include each required file individually:
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
Ensure the file paths match your server directory structure. For example, if your PHPMailer folder is located in public_html/PHPMailer
, update the paths accordingly:
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
📦 Composer-Based Inclusion (Recommended)
If you installed PHPMailer using Composer, inclusion is much simpler and cleaner. Just use the Composer autoload file:
require 'vendor/autoload.php';
This one line will automatically load PHPMailer and any of its dependencies, making your codebase cleaner and easier to manage.
Tip: This method also makes future updates and integrations easier, especially in larger projects.
✅ Once included, you can begin creating and sending emails using PHPMailer. Make sure your script has the correct namespace declarations as shown in the next section.
✉️ 3. Sending Email Using PHPMailer (Example)
Here’s a basic example of using PHPMailer with SMTP:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'mail.yourdomain.com';
$mail->SMTPAuth = true;
$mail->Username = 'your-email@yourdomain.com';
$mail->Password = 'your_email_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('your-email@yourdomain.com', 'Your Website');
$mail->addAddress('recipient@example.com');
$mail->isHTML(true);
$mail->Subject = 'New Contact Form Submission';
$mail->Body = '<h1>Message from Contact Form</h1><p>This is a sample message.</p>';
$mail->AltBody = 'This is a plain text message.';
$mail->send();
echo 'Message sent successfully';
} catch (Exception $e) {
echo "Message could not be sent. Error: {$mail->ErrorInfo}";
}
📂 4. Adding Attachments
PHPMailer makes it easy to send files along with your email messages. This is useful for attaching invoices, documents, or images submitted via a contact form.
Example:
$mail->addAttachment('/path/to/invoice.pdf', 'Invoice.pdf');
-
The first parameter is the file path.
-
The second parameter is the optional name you want to assign to the file in the email.
✅ You can add multiple attachments by repeating the line with different file paths.
🌐 5. Using HTML in Emails
PHPMailer supports full HTML formatting, allowing you to send rich content such as styled text, headings, and layout structures.
Example:
$mail->isHTML(true);
$mail->Body = '<h2>Thank You!</h2><p>Your message has been received.</p>';
$mail->AltBody = 'Thank you! Your message has been received.';
-
isHTML(true)
enables HTML support. -
Body
contains the HTML content of the email. -
AltBody
is a fallback plain-text message for email clients that do not support HTML.
💡 Always include AltBody
for maximum compatibility.
👥 6. Adding Multiple Recipients
PHPMailer allows you to send emails to multiple recipients, along with optional carbon copy (CC) and blind carbon copy (BCC) addresses.
Example:
$mail->addAddress('user1@example.com');
$mail->addAddress('user2@example.com');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
-
Use
addAddress()
for primary recipients. -
Use
addCC()
to copy others visibly. -
Use
addBCC()
to send hidden copies.
✅ This feature is particularly useful for sending newsletters or team-wide notifications.
🤖 7. Debugging and Troubleshooting
Enable SMTP debug output:
$mail->SMTPDebug = 2;
Common Issues
-
Authentication errors: Check your username and password
-
Wrong host or port: Confirm server details
-
Firewall restrictions: Make sure ports 587/465 are open
🔒 8. Security Best Practices
-
Always use TLS or SSL for secure communication
-
Sanitize and validate all form input
-
Never hardcode passwords in scripts — use environment variables:
-
$mail->Password = getenv('EMAIL_PASSWORD');
-
📚 9. Conclusion
PHPMailer is a robust email handling solution for PHP developers. With rich SMTP support, HTML capabilities, and secure configuration options, it’s ideal for implementing contact forms and transactional email systems in Domain India hosting.
For more details, visit:
Need assistance? Submit a support ticket and our team will help you configure PHPMailer on your hosting account.