Hi…
I made cronjob for sending mail with attachment but it send e-mail without attachment, although there is an icon for attachment but it is empty. How can I solve this problem.
I had to require the CMail class:
require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
And then I went to send the mail I made sure to define $filepath and $filename as arrays.
$filepath = array();
$filename = array();
$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext);
$mailfile->sendfile();
It is hard for me to tell what you are doing to “code-wise” for this cronjob without actually seeing some code on how you are sending emails/
msayyed
January 10, 2022, 7:59am
#3
playcock:
?php
require DOL_DOCUMENT_ROOT . "/core/class/CMailFile.class.php";
$ret = 0;
class export{
public $db;
public function __construct($db)
{
$this->db = $db;
}
public function exportAndsendEmail(){
$sql="Test"
$sql .= " = curdate() AND curdate()+1 AND curdate()+2 ;";
$result = $this->db->query($sql) or die("Sql error: ".$this->db->error());
if (!$result) die('Couldn\'t fetch records');
$headers = $result->fetch_fields();
foreach($headers as $header) {
$head[] = $header->name;
}
$dateiname = "export";
$dateiname.= date('d-m-y');
$dateiname.=".csv";
$fp = fopen($dateiname, 'w');
$message ="";
if ($fp && $result) {
// header('Content-Type: text/csv');
// header('Content-Disposition: attachment; filename="export.csv"');
// header('Pragma: no-cache');
// header('Expires: 0');
fputcsv($fp, array_values($head), ",",'"',"\\");
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row), ",",'"',"\\");
$message .= "<tr><td>".array_values($row)."</td></tr>";
;
}
// die;
}
// Send E-mail
// include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
// $formmail = new FormMail($db);
// $formmail->trackid = $trackid; // $trackid must be defined
// $attachedfiles = $formmail->add_attached_files($file,"text/csv");
// $filepath = $attachedfiles[$file];
// Lord �
$subject = "test send email";
// The recipient
$sendto = '@';
// � a person
$replyto = 'test@test.com';
//$mimefilename_list = array();
// The attachment
// $filepath = "C:\\xampp\\htdocs\\dolibarr\\htdocs\\cron";
$filepath = array("C:\\xampp\\htdocs\\dolibarr\\htdocs\\cron");
// $filename = array();
// Attachment � �
$mimetype = array("text/csv");
$filename = array($dateiname);
$cc="";
$ccc="";
$deliveryreceipt="";
$msgishtml =1 ;
$errors_to="test@test.com";
$css="";
$trackid="";
$moreinheader="";
$sendcontext="";
$mimefilename_list =array($dateiname);
// $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,'',$mimetype);
// $mailfile->sendfile();
// � capacity
$mail = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext);
// //$mail = new CMailFile("export", "", "","HIII", "", $mimetype_list = array(), $mimefilename_list = array(), $addr_cc = "", $addr_bcc = "", $deliveryreceipt = 0, $msgishtml = 1);
// $mail->write_files ($filename, $mimetype, $mimefilename_list );
$mail->sendfile();
return 0;
}
}
thank you for answer here is my code it would be nice to solve my problem.
This class should make csv file out of sql data and send an e-mail with attachment.
It make csv send email with attchment icon without csv file.
@msayyed
I am reviewing your code. I collected a log on an email using a CSV file I used to send to an internal user to see what the system logs in the CMailFile.class.php file for the _construct function.
I don’t think it has anything to do with your mimetype being array("text/csv");
as I believe that is the correct setting.
Maybe it is the filepath as I do not see the filename in the path - array("C:\\xampp\\htdocs\\dolibarr\\htdocs\\cron");
These are my findings:
filepath
filename_list: Array
(
[0] => /var/www/path/temp/new7.csv
)
mimetype
mimetype_list: Array
(
[0] => application/octet-stream
[1] => application/vnd.ms-excel
)
filename
mimefilename_list: Array
(
[0] => new7.csv
)
If none of this helps, try setting up a log in the CMailFile.class.php file _construct function to what values are being sent to the constructor for filename_list, mimetype_list, and mimefilename_list. Then maybe we can go from there.
msayyed
January 10, 2022, 3:23pm
#5
thank you!!
the problem was as you said there was no filename in the path.
Awesome! Glad you are on your way this cron.