Here is my PHP page code which I will send emails but I am not able to give a path to the DB connection file which is perfectly working if I run the script through the browser.
Edit: I have updated code with Include and File Directory. Now I’m getting other errors – screenshot updated.
#!/usr/bin/php70
include __DIR__.'/db_connection.php';
include __DIR__.'/PHPMailer-master/PHPMailerAutoload.php';
/* Date time sent emails */
$date = new DateTime('now', new DateTimeZone('Asia/Karachi'));
$email_query = "SELECT *FROM marketing_email WHERE email_timer = '1' ";
$email_result = mysqli_query($db, $email_query);
if (mysqli_num_rows($email_result) > 0) {
while ($email_rows = mysqli_fetch_assoc($email_result)) {
$attechment_query = "SELECT *FROM marketing_email_attechements WHERE marketing_email_id = '".$email_rows['id']."' AND status = '1' ";
$attechment_result = mysqli_query($db, $attechment_query);
if ($attechment_result) {
$path = array();
while ($attechment_rows = mysqli_fetch_assoc($attechment_result)) {
$path[] = '../uploads/email-files/'.date('dmY').'-'.$attechment_rows['filename'];
}
}
// exit();
if ($date->format('Y-m-d H:i:00') >= $email_rows['email_datetime']) {
/* Send email */
$subject = $email_rows['subject'];
$msg = $email_rows['message'];
$to = $email_rows['receiver_email'];
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
$mail->Port = 587;
for($ct=0;$ct<count($path);$ct++){
$mail->AddAttachment($path[$ct]);
}
// $mail->AddAttachment($path);
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = '[email protected]';
//Password to use for SMTP authentication
$mail->Password = "abdullah999";
//Set who the message is to be sent from
$mail->From = '[email protected]';
$mail->FromName = 'Job-interview';
//Set an alternative reply-to address
//$mail->addReplyTo(‘[email protected]', 'hidaya');
//Set who the message is to be sent to
$mail->addAddress($to,'Job-interview');
//Set the subject line
$mail->Subject = $subject;
$mail->msgHTML($msg);
//send the message, check for errors
if (!$mail->send()) {
"Mailer Error: " . $mail->ErrorInfo;
//header("location:manage_users.php?flag=3");
}
else {
$update_query = "UPDATE marketing_email SET email_timer = '0' WHERE id = '".$email_rows['id']."' ";
$update_result = mysqli_query($db, $update_query);
}
}
else{
echo "time not exist";
}
}
?>
<script type="text/javascript">
//location.href="../email-list.php";
console.log('Thank you email has been sent');
</script>
<?php
}
else{
echo "Data not found";
}
?>
I am trying to schedule emails for that I created a cron job which gave me an error on file which works fine from the browser.
Here is screenshot from my cpanel when i am trying to create cron job and testing it’s file if it runs or not.
2
Answers
You could rename your cron command to
/usr/bin/php70 -f /path/to/your.php
, and make sure/usr/bin/php70
is a valid path.I am struggling with a similar problem.
I think I see one thing that looks wrong. On line 19 your code says
$path[] = ‘../uploads/email-files/’.date(‘dmY’).’-‘.$attechment_rows[‘filename’];
This is a relative path which will not work from the command line, which is how cron jobs run, but works fine in a browser.
Try changing the start of that line to something like:
$path[] = dirname( DIR ) . ‘/uploads/email-files/’.date(‘dmY’)…
(DIR should have two underscores before and after it) If you try a few echo statements you will get the path right.