Change language

How to send email using PHPMailer?

|
Installation:the best way to install PHPMailer - use composer.Before proceeding, make sure to install composer .
  • Open a command prompt and change to the project directory where you want to use PHPMailer.
  • Run the following command:
    composer require phpmailer / phpmailer 
  • Wait for the installation to complete. It will load all the required classes into your project folder.
Using PHPMailer:
Import the PHPMailer class into the global namespace. 
Note.Make sure these lines are at the top of the script and not inside any function.
use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; 
Load the composer autoloader.
require ’vendor / autoload.php’; 
Create an object of the PHPMailer class.
$mail = PHPMailer() 
Configure server parameters:
  • SMTPDebug:is used to display messages about connection problems and sending emails. Has the following values:
    • 0: This is the default. Disable debugging.
    • 1: Show output messages sent by the client.
    • 2: 1 plus displays responses received from the server.
    • 3: as 2 plus optional initial connection information - this level can help diagnose STARTTLS failures.
    • 4: like 3, plus display even lower level information.
  • isSMTP() :install the mailer to use SMTP.
  • isMail():install the mailer to use the PHP mail function.
  • Host :Specifies the servers.
  • SMTPAuth:enable / disable SMTP authentication.
  • Username:provide username .
  • Password:provide a password.
  • SMTPSecure:specify an encryption method. Valid values ​​are "tls" or "ssl".
  • Port:specify the TCP port to connect to.
$mail-> SMTPDebug = 2; // Enable verbose debug output $mail-> isSMTP(); // Set mailer to use SMTP $mail-> Host = ’smtp.gfg.com;’; // Specify main SMTP server $mail-> SMTPAuth = true; // Enable SMTP authentication $mail-> Username = ’user @ gfg.com’; // SMTP username $mail-> Password = ’password’; // SMTP password $mail-> SMTPSecure = ’tls’; // Enable TLS encryption, ’ssl’ also accepted $mail-> Port = 587; // TCP port to connect to 
Add email recipients.
$mail-> setFrom (’from @ gfg.com’,’ Name’); // Set sender of the mail $mail-> addAddress (’receiver1 @ gfg.net’); // Add a recipient $mail-> addAddress (’receiver2 @ gfg.com’,’ Name’); // Name is optional 
Add attachments (if any).
$mail-> addAttachment (’url’,’ filename’); // Name is optional 
Add content.
  • isHTML():if passed to true sets the email format to HTML.
  • Subject:Set the subject of the email.
  • Body:Set the content of the mail.
  • AltBody: alternative body if the mail client does not support HTML.
$mail-> isHTML (true); $mail-> Subject = ’Subject’; $mail-> Body = ’HTML message body in  bold !’; $mail-> AltBody = ’Body in plain text for non-HTML mail clients’; 
Finally, send your email.
$mail-> send(); 
And your email will be sent.Program:Complete PHP program for sending email using PHPMailer. use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require ’ vendor / autoload.php’ ; $mail = new PHPMailer (true); try { $mail -> SMTPDebug = 2; $mail -> isSMTP(); $mail -> Host = ’smtp.gfg.com;’ ; $mail -> SMTPAuth = true; $mail -> Username = ’user @ gfg.com’ ; $mail -> Password = ’password’ ; $mail -> SMTPSecure = ’tls’ ; $mail -> Port = 587; $mail -> setFrom ( ’ from @ gfg.com’ , ’ Name’ ); $mail -> addAddress ( ’receiver1 @ gfg.com’ ); $mail -> addAddress ( ’receiver2 @ gfg.com’ , ’ Name’ ); $mail -> isHTML (true); $mail -> Subject = ’Subject’ ; $mail -> Body = ’HTML message body in bold  ’ ; $mail -> AltBody = ’Body in plain text for non-HTML mail clients’ ; $mail -> send(); echo "Mail has been sent successfully! " ; } catch (Exception $e ) { echo "Message could not be sent. Mailer Error: {$mail-> ErrorInfo}" ; }  
?>

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method