PHP Send Email From Localhost

PHP Send Email from localhost using PHPMailer library, PHPMailer library is the easiest way to send email from localhost. its very commonly used library now for email sending

Send Email from localhost is one of the difficult tasks in PHP, But PHPMailer library is the easiest way to send email from localhost. it was released in 2001 now it is a very commonly used library.
You can also PHP send email from localhost using PHP built-in function mail() but first, you will configure PHP.ini file and some more modification, but still used PHP mail() built-in function is the hard way. PHPMailer library now used by most popular CMS(content management system) WordPress, Joomla and Drupal.
php send email


Before work with PHPMailer:

Before work with PHPMailer, you need to modify some things
1-First login to your Gmail account and go to this link Lesssecureapps and update Allow less secure apps to ON by default its OFF.
2-Secondly go to your php.ini file and search for openssl and enabled openssl if you have already enabled its ok otherwise change ;openssl to openssl remove semicolon before openssl.


Installing PHPMailer library:

1-Installing PHPMailer library using composer.

[composer require phpmailer/phpmailer]

 2-Download PHPMailer library from GitHub 

Goto on this URL Download PHPMailer and download PHPMailer library.
when you download the PHPMailer library first extract the zip folder and after place your PHPMailer library in htdocs folder, your folder will look like this
php send email

Now create index.php file in your main folder which name is PHPMailer-master in my case
and copy the entire code and paste in your index.php file

[<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;                               // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'add your gmail account here';                 // SMTP username
$mail->Password = 'add your gmail account password here';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to
$mail->SMTPDebug = 3;
$mail->setFrom('add your gmail account here', 'Your name');
$mail->addAddress('add recipient email address ', 'recipient name');     // Add a recipient
// $mail->addAddress('ellen@example.com');               // Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
// $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Email From PHPMailer';
$mail->Body    = 'Hello brother how are you this email is send form phpmailer library its very easy!</b>';
$mail->AltBody = 'Hello brother how are you this email is send form phpmailer library its very easy!</';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}]

PHP Send Email with attachment:

PHP Send email with attachment create new image folder in your main directory in my case my directory name is PHPMailer-master and place your image there which image you want to send like this 
php send email



















Now copy the entire code for file attachment

[<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;                               // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'your gmail account';                 // SMTP username
$mail->Password = 'your gmail account password';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to
$mail->SMTPDebug = 3;
$mail->setFrom('your gmail account', 'Your name');
$mail->addAddress('recipient gmail account', 'recipient name');     // Add a recipient
// $mail->addAddress('ellen@example.com');               // Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
$mail->addAttachment('images/php-send-email.png');         // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Email From PHPMailer';
$mail->Body    = 'Hello brother how are you this email is send form phpmailer library its very easy!</b>';
$mail->AltBody = 'Hello brother how are you this email is send form phpmailer library its very easy!</';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}]
for error handling enabled this line in your code
[$mail->SMTPDebug = 3;]
and put the value from 1 to 4 it will give you all information about an error.
for more information watch the video.

Conclusion:

PHP send email from localhost using PHPMailer library is the easy way. read all article carefully and follow the instructions. if still, you have any issue then write down your problem in the comment box and you can also ask the question from PHPMailer support team.

COMMENTS

Name

CSS,2,Example,3,HTML,2,PHP,1,
ltr
item
webtipstricks: PHP Send Email From Localhost
PHP Send Email From Localhost
PHP Send Email from localhost using PHPMailer library, PHPMailer library is the easiest way to send email from localhost. its very commonly used library now for email sending
https://1.bp.blogspot.com/-XNx-LW-_B5I/WZkfKtKfGlI/AAAAAAAABTY/A8eve1RmF6Q8TKAo9YjcSJZlzFS6RbRLwCLcBGAs/s640/php-send-email-from-localhost.jpg
https://1.bp.blogspot.com/-XNx-LW-_B5I/WZkfKtKfGlI/AAAAAAAABTY/A8eve1RmF6Q8TKAo9YjcSJZlzFS6RbRLwCLcBGAs/s72-c/php-send-email-from-localhost.jpg
webtipstricks
http://www.webtipstricks.com/2017/08/php-send-email-from-localhost.html
http://www.webtipstricks.com/
http://www.webtipstricks.com/
http://www.webtipstricks.com/2017/08/php-send-email-from-localhost.html
true
4027471107773963481
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy