I m tryinng to send mail using following script. This scrips works fine if I execute this script in browser But if I run this script through command-line(cmd ) than getting smrp connection failed() error. code :
function sendMail($subject,$cc,$bcc,$emailcontent){
global $_mailmsg;
$emailcontent=$_mailmsg;
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->Port = '465';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username@gmail.com'; // SMTP username
$mail->Password = 'XXXXXXXX';
$mail->SMTPSecure = 'ssl';
$mail->From = 'username@gmail.com';
$mail->FromName = 'Notification';
$mail->addAddress('abc@gmail.com'); // Add a recipient
$mail->addReplyTo('username@gmail.com');
if(!empty($cc)){ $mail->addCC($cc); }
if(!empty($bcc)){ $mail->addBCC($bcc);}
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $emailcontent;
try
{
if($mail->send()) {
return 1;
exit;
}
else
{
echo 'Mailer Error: ' . $mail->ErrorInfo;
return 0;
}
}
catch(Exception $e)
{
return 0;
}
}
I also config php.ini file
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 465
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = username@gmail.com
To run php file i wrote the following things in cmd
php filename.php