Hi everyone, tried to send email from localhost using smtp mail function but unable to do so. The error is "Could not instantiate mail function. Mailer Error: Could not instantiate mail function." Appreciate if you could advise how to solve this issue? Thanks.

  require_once "PHPMailerAutoload.php";
    $name = $row['name'];
            $email=$row['email'];
            echo $email;
            echo $name;

$mail = new PHPMailer;

//Enable SMTP debugging. 
$mail->SMTPDebug = 2;                               
//Set PHPMailer to use SMTP.
//ail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "localhost";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                        
//Provide username and password     
$mail->Username = "smtp_username";             
$mail->Password = "smtp_password";                      
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 25;                                   

$mail->From = ($row['email']);
$mail->FromName = ($row['username']);

$mail->addAddress("sophia@gmail.com", "Sophia");

$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";
}

Hi,

in order to send an email through SMTP you need to setup the connection correctly, right now you are pointing the host to localhost instead of something like smtp.domain.tld and the port to 25, which is not correct if connecting through tsl or ssl, it may be port 465, 587 or 993 or something completely different, depending on the host configuration. Also you have to set $mail->isSMTP();. You can raise the debug level to 4 and add a debug function to get more information:

// Debug
$log_file = sprintf(__DIR__ . '/mail-%s.txt', (new \DateTime)->format('Ymd'));

$mail->SMTPDebug   = 4;
$mail->Debugoutput = function($message, $level) use($log_file) {
            error_log($log_file, sprintf('(%u) %s', $level, $message));
            return ;
      };

The Debugoutput closure will create, in the same path of the script, a log file named mail-20170504.txt with the flow of the connection between the client (the script) and the SMTP server, from there you should be able to see the server response and why it fails to complete the request. If you need help with the debug, you can attach the log file to the forum, but remember to remove passwords and other details you want to keep private.

Hi Cereal, thanks a lot for your reply. I've made the changes as per your advice and the the error message now is " Warning: error_log() expects parameter 2 to be integer, string given in C:\xampp\htdocs\agentsignup.php on line 56". Any idea where to get the smtp username and password? Please advise? Thanks.

require_once "PHPMailerAutoload.php";
    $username = $row['username'];
    $email=$row['email'];
   $mail = new PHPMailer;
//Enable SMTP debugging. 
//$mail->SMTPDebug = 4;  
         // Debug
$log_file = sprintf(__DIR__ . '/mail-%s.txt', (new \DateTime)->format('Ymd'));
$mail->SMTPDebug   = 4;
$mail->Debugoutput = function($message, $level) use($log_file) {
            error_log($log_file, sprintf('(%u) %s', $message,$level));
            return ;

     };
//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 = false;                        
//Provide username and password     
$mail->Username = "smtp_username";             
$mail->Password = "smtp_password";                      
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 587;                                   
$mail->From = ($row['email']);
$mail->FromName = ($row['username']);
$mail->addAddress("sophia@gmail.com", "Sophia");
$mail->isHTML(true);
$mail->Subject = "Test email";
$mail->Body = "<i>Test email</i>";
$mail->AltBody = "Test email function in php";
if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

Ops! My fault, I confused error_log() with file_put_contents() syntax :D

Sorry, please replace the error log line with:

error_log(sprintf("(%s) %s\r\n", $level, $message), 3, $log_file);

And it should work fine.

Any idea where to get the smtp username and password?

Those would, usually, be the email address and the related password. In case of Gmail, however, you should generate and use an application password.

Hi cereal, thanks for your replies. I've changed the error log as per your comment and the following error displayed. Please advise? Thanks.

Mailer Error: The following From address failed: admin@web.com : MAIL FROM command failed,Authentication Required. Learn more at https://support.google.com/mail/?p=WantAuthError g66sm7809168pfj.11 - gsmtp ,530,5.5.1SMTP server error: MAIL FROM command failed Detail: Authentication Required. Learn more at https://support.google.com/mail/?p=WantAuthError g66sm7809168pfj.11 - gsmtp SMTP code: 530 Additional SMTP info: 5.5.1SMTP server error: MAIL FROM command failed Detail: Authentication Required. Learn more at https://support.google.com/mail/?p=WantAuthError g66sm7809168pfj.11 - gsmtp SMTP code: 530 Additional SMTP info: 5.5.1

Okay,

you set up admin@web.com in the FROM header, but the authentication is failing. Is this the account that is authenticating to the SMTP server? From the log it seems you are using GMail SMTP.

Can you explain how this script would work? From who (client email address) to who (website email address)? Or reverse? Or client to client? I ask it, because you may need to set the sender in the REPLY-TO header and the authenticating email address in the FROM header:

$mail->Username = "smtp_username"; # authentication
$mail->From     = "smtp_username"; # from
$mail->AddReplyTo($row["email"]);  # reply-to

This because the script acts like a mail carrier (technically an SMTP Relay), which gets the message and sends it through his SMTP server.

Hi Cereal, thanks a lot for your reply. The admin@web.com is the smtp server. The script flow is once the user register, and upon clicking submit button, the notification email will be sent to admin to notify of the client who has just registered (client to admin). I've made the changes as per your recommendation above and the error is "Mailer Error: You must provide at least one recipient email address." Please advise? Thanks.

   require_once "PHPMailerAutoload.php";
    $username = $row['username'];
    $email=$row['email'];
   $mail = new PHPMailer;
            // Debug
$log_file = sprintf(__DIR__ . '/mail-%s.txt', (new \DateTime)->format('Ymd'));
$mail->SMTPDebug   = 4;
$mail->Debugoutput = function($message, $level) use($log_file) {
            error_log(sprintf("(%s) %s\r\n", $level, $message), 3, $log_file);
            return ;
     };
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "admin.web.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = false;                        
//Provide username and password     
$mail->Username = ($row['email']);             
//$mail->Password = "";                      
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 587;                                   
$mail->From = ($row['email']);
$mail->FromName = ($row['username']);
$mail->AddReplyTo("sophia@gmail.com", "Sophia"); # reply-to
$mail->isHTML(true);
$mail->Subject = "Test email";
$mail->Body = "<i>Test email</i>";
$mail->AltBody = "Test email function in php";
if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

Almost done. You need to set the TO header, in this case it can be the SMTP username or another email address of yours:

$mail->AddAddress($row['email']);

Line 17 will probably need to be set to TRUE:

$mail->SMTPAuth = true;

Otherwise it will not attempt to authenticate, you can set it to FALSE if the SMTP does not require the authentication or if you are using the POP-before-SMTP auth method, see their example:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.