Tutorial || How To Send An Email Using PHP
What will I learn?
- You will learn basics of php
- You will learn how to send a mail using php
- You will learn how to design a template in HTML
Requirements
- Apache Xampp/Wamp Server
- PHPMailer Library
- Text editor
Difficulty
Intermediate
Tutorial Contents
Introduction to php
Creating (Declaring) PHP Variables:
In PHP, a variable starts with the $ sign, followed by the name of the variable.
Example-
$x
$chotho
Classes and Objects in php
Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class.
Example-
class Chotho
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
How to Install PHPMailer library
PHPMailer is available on Packagist (using semantic versioning), and installation via composer is the recommended way to install PHPMailer. Just add this line to your composer.json file:
"phpmailer/phpmailer": "~6.0"
or run
composer require phpmailer/phpmailer
After Installation check whether you have all these files in PHPMailer folder
If any of the file is missing then please repeat the installation process.
Steps to write a code to Send Email in php
1)Create a file Mail.php
2)Include PHPMailer library in "Mail.php"
require_once('YourPathToPHPMailerFolder/PHPMailerAutoload.php');
3)Create a class Mail in "Mail.php"
class Mail
{
public static function sendMail($subject, $body, $address) {
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "[email protected]";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'John Doe');
//Set the subject line
$mail->Subject = $subject;
$mail->Body = $body;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
4)Create an object of class "Mail"
$m = new Mail();
5)Call "sendMail()" function of class "Mail" with subject, your message** & sender's email id as parameter
$m->sendMail("My first mail",
"This is a testing mail",
"[email protected]");
Write the Codes as follows:
How to create HTML Template
<form action="Mail.php" method="POST">
Sender Email: <input type="email" name="Sender_Email">
Subject: <input type="text" name="Subject">
Message: <textarea name="Message"></textarea>
<button type="submit">Send Email</button>
</form>
Output
Posted on Utopian.io - Rewarding Open Source Contributors
Congratulations @chotho! You have received a vote as a way to thank you for supporting my program.
Your contribution cannot be approved because it does not follow the Utopian Rules.
There is already a similar tut out there that covers this: https://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php
You can contact us on Discord.
[utopian-moderator]
Hello chotho!
Congratulations! This post has been randomly Resteemed! For a chance to get more of your content resteemed join the Steem Engine Team