Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send e-mail from localhost with phpmailer?

okay, so I already try it for many times. The results was not error but I didn't receive any e-mail in my inbox or spam folder

here is my mail.php

    <?php


require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer();
//$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // SMTP server
//IsSMTP(); // send via SMTP
$mail->SMTPDebug = true;
$mail->IsSMTP();
$mail->Host     = "smtp.gmail.com"; // SMTP server Gmail
$mail->Mailer   = "gmail";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587;   


$mail->Username = "[email protected]"; // 
$mail->Password = "******"; // SMTP password
$webmaster_email = "[email protected]"; //Reply to this email ID
$email = "[email protected]"; // Recipients email ID
$name = "Hendrikus Anthony"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Anthony";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Anthony");
$mail->WordWrap = 50; // set word wrap

$mail->IsHTML(true); // send as HTML
$mail->Subject = "Ini adalah Email HTML";
$mail->Body = "Ini adalah email contoh"; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

please someone, I really need help. do I need a hosting? or there are something wrong with my syntax? whether sendmail.ini and php.ini affect the mail.php?

like image 445
Henrikus Anthony Avatar asked Dec 20 '22 06:12

Henrikus Anthony


2 Answers

Here is my solution which I found out from couple of articles.

<?php

require_once "vendor/autoload.php";

$mail = new PHPMailer;

//Enable SMTP debugging. 
$mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "[email protected]";                 
$mail->Password = "password";                           
//If SMTP requires TLS encryption then set it
//$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 587;                                   

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->smtpConnect(
    array(
        "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false,
            "allow_self_signed" => true
        )
    )
);

$mail->addAddress("[email protected]", "Recepient Name");

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

This won't require any server settings on your localhost.

$mail->smtpConnect(
    array(
        "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false,
            "allow_self_signed" => true
        )
    )
); 

This part of code asks smtp not to verify any connection and can send the mail without verifying the sender. Also you need to enable IMAP settings from your mailbox settings.

Here are the links for reference. https://www.sitepoint.com/sending-emails-php-phpmailer/ https://github.com/PHPMailer/PHPMailer/issues/368#issuecomment-75821110

like image 181
Dhaval Chheda Avatar answered Dec 24 '22 01:12

Dhaval Chheda


You need an SMTP server to send out mail. Assuming you want to use this for testing purposes, try downloading a free SMTP local server such as this one.

If you want to actually send out mail in a production environment, consider using an external service such as SendGrid or MailChimp. Alternatively, if you want to stick with SMTP, you are going to need your own web server to send mail from.

like image 37
jperezov Avatar answered Dec 24 '22 02:12

jperezov