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
Related
I have php code that loops through client emails and sends them reports using PHPMailer. When run in a browser, it works perfectly. I created a cron job that is able to open and run the php file. However when the cron job runs, the first email is sent but the program stops sending emails. After testing I realized after the first email is sent, the rest of the code stops working and it never loops through to the next 'while' condition.
cron job used (which does open and run the file):
/usr/bin/php -q site1/temp/test.php
The code below will send 3 emails when opened in a browser, but only sends one when run with a cron job:
<?php
$index = 0;
while($index < 3){
require (dirname(__FILE__) . DIRECTORY_SEPARATOR .'PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'my smtp provider'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'name'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->From = 'email';
$mail->FromName = 'ME';
$mail->AddAddress( "test#yahoo.com" );
$mail->isHTML(true);
$mail->Subject = 'Test email';
$mail->Body = "test";
$mail->send();
// nothing else is sent at this point after the first loop through
}//end while
?>
NOTE: using Linux Server (Parallels Plesk)
Try this code :
<?php
require (dirname(__FILE__) . DIRECTORY_SEPARATOR .'PHPMailer/PHPMailerAutoload.php');
$index = 0;
while($index < 3){
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'my smtp provider'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'name'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->From = 'email';
$mail->FromName = 'ME';
$mail->AddAddress( "test#yahoo.com" );
$mail->isHTML(true);
$mail->Subject = 'Test email';
$mail->Body = "test";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
$mail->clearAddresses();
$index++;
}
?>
Using $mail->clearAddresses(); within the loop. and Check which type of error you get.
I'm using php mailer for mail triggering.
Its working fine. But I gave 2 to 5 recipients, it sends the mail to only one recipient. In future, I have to trigger a mail to nearly 100 recipients..
I've shared my code below.. Please check it..
require 'phpmailer/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 = 'karthick****#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('karth*******#gmail.com', 'A**n');
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
You're code looks like it should be doing the trick. Make sure that $address doesn't contain any whitespace for the entries. For safe measure, add the trim()
function.
$mail->AddAddress(trim($address));
If that does not work, make sure that you're recipient addresses are real.
Additionally, in case the privacy of the recipients is of concern, I would recommend you use AddBCC() instead of AddAddress() so that their addresses are not revealed.
A basic idea is that make different connection (object) for each your mailing address like below if You don't have so much addresses in your array.
require 'phpmailer/PHPMailerAutoload.php';
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
$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 = 'karthick****#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('karth*******#gmail.com', 'A**n');
$mail->AddAddress($address);
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
UPDATE :
The second idea is that you can remove recepients each time and add new one and then send like below
require 'phpmailer/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 = 'karthick****#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('karth*******#gmail.com', 'A**n');
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
// for clear last recipients
$mail->ClearAllRecipients( )
$mail->AddAddress($address);
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
}
$message.="<img src='cf_logo.png'/>";
the above code does not show the image in php
I need to show the image as body of the message.Not an attachment.
You need the full path:
<img src='add your domain here'/cf_logo.png>
Try below code:
$mail = new PHPMailer();
$mail->isSMTP(); // send via SMTP
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
//$mail->SMTPDebug = 2;
$mail->Host = $host; //SMTP server
$mail->Port = $port; //set the SMTP port; 587 for TLS
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
if (strlen($username)) {
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
} else {
$mail->SMTPAuth = false;
}
$mail->From = $from; // FROM EMAIL
$mail->FromName = $fromName; // FROM NAME
$mail->addAddress($to);
$mail->addReplyTo($from);
$mail->IsHTML($html);
$mail->Subject = $subject; // YOUR SUBJECT
$mail->Body = $message; // YOUR BODY
$mail->addAttachment($attachment); // YOUR ATTACHMENT FILE PATH
if ($mail->send()) {
echo "Message Sent";exit;
} else {
echo "Message Not Sent<br>";
echo "Mailer Error: " . $mail->ErrorInfo;exit;
}
Note: You need to include php mailer class.
Try this, it might work., Use http:// in the front of your image url like this
http://yoursite/folder/image.jpg
date_default_timezone_set('Asia/Dubai');
include("classes/class.phpmailer.php");
$mail = new PHPMailer();
$body = "this is <strong>testing</strong> mail ". date('Y-m-d H:i:s');
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = 'my#email.com';
$mail->Password = '*******';
$mail->SetFrom('my#email.com', 'First Last');
$mail->AddReplyTo('my#email.com','First Last');
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "to#email.com"; // add your address here
$mail->AddAddress($address, "Gmail Test");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I have script like this.Its working fine with localhost but when i moving to windows or linux servers it won't work.I want to work on both windows and linux servers.What should i do?
Error like this:
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.
try using this :
require_once ( 'class.phpmailer.php' ); // Add the path as appropriate
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server
$Mail->SMTPDebug = 2; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "tls"; //Secure conection
$Mail->Port = 587; // set the SMTP port
$Mail->Username = 'MyGmail#gmail.com'; // SMTP account username
$Mail->Password = 'MyGmailPassword'; // SMTP account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'Test Email Using Gmail';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = 'MyGmail#gmail.com';
$Mail->FromName = 'GMail Test';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->AddAddress( $ToEmail ); // To:
$Mail->isHTML( TRUE );
$Mail->Body = $MessageHTML;
$Mail->AltBody = $MessageTEXT;
$Mail->Send();
$Mail->SmtpClose();
if ( $Mail->IsError() ) {
echo "ERROR<br /><br />";
}
else {
echo "OK<br /><br />";
}
What version of PHPMailer you are using ? if you are using old version try with
$mail->Port = 587;
try to update PHPMailer version that will solve i hope !!!
Im trying to test my emails functions in localhost with phpmailer class.
But I´m getting this error: SMTP Error: Could not authenticate..
And this error its because I dont have a localhost email configured.
I already did this configuration in my Xampp php.ini to try test emails on localhost:
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = localhost
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = myemail#hotmail.com
But its not working, Somebody there knows How to do this work on windows 8 with xampp?
<?php
define('MAILUSER','myemail#hotmail.com');
define('MAILPASS','');
define('MAILPORT','587');
define('MAILHOST','smtp.live.com');
define('SITENAME', 'Site Name');
function sendMail($subject,$message,$sender,$senderName,$destination,$destinationName, $reply = NULL, $replyNome = NULL){
require_once('mail/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->IsHTML(true);
$mail->Host = MAILHOST;
$mail->Port = MAILPORT;
$mail->Username = MAILUSER;
$mail->Password = MAILPASS;
$mail->From = utf8_decode($sender);
$mail->FromName = utf8_decode($senderName);
if($reply != NULL){
$mail->AddReplyTo(utf8_decode($reply),utf8_decode($replyNome));
}
$mail->Subject = utf8_decode($assunto);
$mail->Body = utf8_decode($mensagem);
$mail->AddAddress(utf8_decode($destino),utf8_decode($destinationName));
if($mail->Send()){
return true;
}else{
return false;
}
}
?>
Here I send the email:
$msg = 'hello';
sendMail('Sent email!',$msg,MAILUSER,SITENAME,$assoc['email'],$assoc['name']);
Maybe your SMTP configuration needs to be updated? See this article about the Hotmail SMTP settings:
http://www.serversmtp.com/en/smtp-hotmail