How to Send Mail With Smtp Authentication In Php?

6 minutes read

To send mail with SMTP authentication in PHP, you can use the PHPMailer library which provides an easy way to send emails using SMTP. First, you need to download and include the PHPMailer library in your project. Next, you need to set up the SMTP configuration including the SMTP server address, port, username, password, and authentication method. Then, create a new instance of PHPMailer and set the From, To, Subject, and Body of the email. Finally, call the send() method to send the email using SMTP authentication. Make sure to handle any errors that may occur during the sending process.

Best Cloud Hosting Services of December 2024

1
Vultr

Rating is 5 out of 5

Vultr

  • Ultra-fast Intel Core Processors
  • Great Uptime and Support
  • High Performance and Cheap Cloud Dedicated Servers
2
Digital Ocean

Rating is 4.9 out of 5

Digital Ocean

  • Professional hosting starting at $5 per month
  • Remarkable Performance
3
AWS

Rating is 4.8 out of 5

AWS

4
Cloudways

Rating is 4.7 out of 5

Cloudways


How to enable SMTP authentication in PHPMailer?

To enable SMTP authentication in PHPMailer, you need to set the SMTPAuth property to true and provide the username and password for the SMTP server.


Here is an example code snippet to configure SMTP authentication in PHPMailer:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

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

// Set SMTP settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_smtp_username';
$mail->Password = 'your_smtp_password';
$mail->SMTPSecure = 'ssl'; // Enable TLS if required
$mail->Port = 465; // Set the SMTP port

// Additional settings
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('to@example.com', 'Recipient Name');
$mail->isHTML(true);
$mail->Subject = 'Test email with SMTP authentication';
$mail->Body = 'This is a test email sent using SMTP authentication.';

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


Make sure to replace the placeholders with your actual SMTP server details, username, and password. Also, ensure that you have PHPMailer installed and autoloaded in your project before running this code.


How to send email with SMTP authentication in PHP?

To send an email with SMTP authentication in PHP, you can use the PHPMailer library. Here is an example code snippet to send an email with SMTP authentication using PHPMailer:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php'; // Include PHPMailer autoload file

$mail = new PHPMailer(true); // Create a new PHPMailer instance
try {
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host = 'smtp.example.com'; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = 'your@example.com'; // SMTP username
    $mail->Password = 'yourpassword'; // SMTP password
    $mail->SMTPSecure = 'ssl'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port = 465; // TCP port to connect to

    $mail->setFrom('your@example.com', 'Your Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');

    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = 'Subject of the email';
    $mail->Body    = 'This is the HTML message body';

    $mail->send();
    echo 'Email has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>


In this code snippet, you need to replace the SMTP server details, username, password, sender, recipient, subject, and message body with your own values. This code uses the PHPMailer library to send emails with SMTP authentication in PHP.


What is required to enable SMTP authentication in PHP?

To enable SMTP authentication in PHP, you need to configure the php.ini file with the following settings:

  1. Set the SMTP server address using SMTP = your_smtp_server_address.
  2. Set the smtp_port using smtp_port = your_smtp_port (typically 25 or 587).
  3. Enable authentication by setting auth_username and auth_password with your SMTP server username and password.
  4. Set auth to true to enable authentication.
  5. You can also specify the auth_method if needed (such as LOGIN, PLAIN, or CRAM-MD5).


After making these changes, remember to restart your web server for the changes to take effect.


How to use PHP to send email with SMTP authentication?

To send an email with PHP using SMTP authentication, you can use the PHPMailer library. Here is an example code snippet on how to achieve this:


Step 1: Install PHPMailer library via Composer

1
composer require phpmailer/phpmailer


Step 2: Create a PHP script to send email

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

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

// Set mailer to use SMTP
$mail->isSMTP();

// Specify the SMTP host
$mail->Host = 'smtp.yourmailserver.com';

// Enable SMTP authentication
$mail->SMTPAuth = true;

// SMTP username
$mail->Username = 'your_smtp_username';

// SMTP password
$mail->Password = 'your_smtp_password';

// Enable TLS encryption
$mail->SMTPSecure = 'tls';

// TCP port to connect to
$mail->Port = 587;

// Set email content
$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Subject of the email';
$mail->Body = 'Content of the email';

// Send the email
if (!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}


Replace 'smtp.yourmailserver.com', 'your_smtp_username', 'your_smtp_password', 'your_email@example.com', 'Your Name', and 'recipient@example.com', 'Recipient Name' with your own SMTP server settings and email addresses.


This script uses PHPMailer to set up SMTP authentication and send an email with the specified content. Make sure to include error handling to catch any potential issues with sending the email.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To create SMTP credentials for a WordPress website, you will need to first sign up for an SMTP service provider such as SendGrid, Mailgun, or SMTP.com. Once you have signed up for an account, you will need to obtain your SMTP credentials, which typically inclu...
To send SMTP mail from localhost, you need to have a local SMTP server installed on your computer. This can be done by setting up a mail server such as Postfix, Sendmail, or Exim. Once the SMTP server is installed and configured, you can use a programming lang...
To set up multiple SMTP servers in WordPress, you can use a plugin like WP Mail SMTP. Install and activate the plugin, then go to WP Mail SMTP » Settings. From there, you can add multiple SMTP servers by clicking on the &#39;Add new SMTP server&#39; button. En...
To send SMTP mail with command prompt, you need to have access to a command prompt or terminal window on your computer. First, open the command prompt and type in the command &#34;telnet smtp.yourmailserver.com 25&#34; where &#34;yourmailserver.com&#34; is the...
To send an email using an SMTP server, you will first need to set up an SMTP client on your computer or device. This client will allow you to establish a connection to the SMTP server and send your email through it.Next, you will need to configure the SMTP set...
To solve the send mail ORA-29279 SMTP issue, you can try the following troubleshooting steps:Check your SMTP server settings to ensure they are correct.Verify that your firewall or antivirus software is not blocking the SMTP connection.Make sure that the recip...