Why PHPMailer?
Contents
- Download PHPMailer Library
- Install PHPMailer
- Import PHPMailer Library
- Create a new project folder
- Create new PHP file
- Import PHPMailer code
- Run the program
- Conclusion
Getting Started with PHPMailer
To start using PHPMailer, you'll need to install it on your server. Once you have PHPMailer installed, you can create a new PHP file and begin coding your email. In this guide, we'll cover the basic steps for sending an email with PHPMailer:
- Import the PHPMailer library
- Set up your email
- Send your email
How to install PHPMailer
- Download PHPMailer
- Composer to install
Importing the PHPMailer Library
The first step in using PHPMailer is to import the library into your project. You can do this by downloading the latest version of PHPMailer from the official GitHub repository and placing the PHPMailer directory in your project. Alternatively, you can use Composer to install PHPMailer.
Can you install the PHPMailer library in your PHP project by running the following command in Composer
composer require phpmailer/phpmailer
After Installations
Set up and Send your email using PHPMailer
// Set the SMTP server and port $mail->isSMTP(); $mail->Host = 'smtp.example.com'; //or $mail->Host = 'mail.example.com'; $mail->Port = 587; // Set SMTP authentication $mail->SMTPAuth = true; $mail->Username = 'your_email@example.com'; $mail->Password = 'your_password'; // Set SMTP encryption $mail->SMTPSecure = 'tls';
Error Messages in PHPMailer
$mail->ErrorInfo;
You can send email from a local web server by using the code below.
<?php//Import PHPMailer classes into the global namespace//These must be at the top of your script, not inside a functionuse PHPMailer\PHPMailer\PHPMailer;use PHPMailer\PHPMailer\SMTP;use PHPMailer\PHPMailer\Exception;//Load Composer's autoloaderrequire 'vendor/autoload.php';//Create an instance; passing `true` enables exceptions$mail = new PHPMailer(true);$mail = new PHPMailer;$mail->isSMTP();//$mail->SMTPDebug = 2;$mail->Debugoutput = 'html';$mail->Host = 'mail.yourhost.com';$mail->SMTPAuth = true;$mail->Username = 'youremail@example.com';$mail->Password = 'yourpassword';$mail->SMTPSecure = 'tls';$mail->Port = 587;$mail->setFrom('from@gmail.com');$mail->addAddress('receive@gmail.com'); //Add a recipient//$mail->addAddress('ellen@example.com'); //Name is optional$mail->addReplyTo('no-reply@example.com');$mail->addCC('addCC@example.com');$mail->addBCC('bcc@example.com');//Attachments$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name//Content$mail->isHTML(true); //Set email format to HTML$mail->Subject = 'Here is the subject';$mail->Body = 'This is the HTML message body <b>in bold!</b>';$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';if($mail->send()){//$mail->send();echo 'Message has been sent';}else{echo "Message could not be sent. <p>";echo "Mailer Error: " . $mail->ErrorInfo;exit;}?>
Before sending an email through SMTP, hostname, port number, and encryption are required. Also, you may need a username and password for authentication.
Finally enter this url in your browser: http://localhost/phpmailer/enquiry.php
We hope this guide has been helpful. If you have any questions or need further assistance, Please feel free to comment below, with your suggestion and problems you face - we are here to solve your problems.
0 Comments