# Attach file using CMail in Dolibarr

**URL:** <https://www.dolibarr.org/forum/t/attach-file-using-cmail-in-dolibarr/21681>\
**Category:** Developing for Dolibarr\
**Tags:** Windows\
**Created:** [January 7, 2022, 1:49pm UTC](https://www.dolibarr.org/forum/t/attach-file-using-cmail-in-dolibarr/21681 "2022-01-07T13:49:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msayyed](https://www.dolibarr.org/forum/letter_avatar_proxy/v4/letter/m/f0a364/32.png) [@msayyed](https://www.dolibarr.org/forum/u/msayyed)\
**Post date:** [January 7, 2022, 1:49pm UTC](https://www.dolibarr.org/forum/t/attach-file-using-cmail-in-dolibarr/21681/1 "2022-01-07T13:49:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![playcock](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/playcock/32/6009_2.png) [@playcock](https://www.dolibarr.org/forum/u/playcock)\
**Post date:** [January 7, 2022, 4:04pm UTC](https://www.dolibarr.org/forum/t/attach-file-using-cmail-in-dolibarr/21681/2 "2022-01-07T16:04:42Z")

</div>

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.

```auto
$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/

---

<div class="post-metadata">

**Author:** ![msayyed](https://www.dolibarr.org/forum/letter_avatar_proxy/v4/letter/m/f0a364/32.png) [@msayyed](https://www.dolibarr.org/forum/u/msayyed)\
**Post date:** [January 10, 2022, 7:59am UTC](https://www.dolibarr.org/forum/t/attach-file-using-cmail-in-dolibarr/21681/3 "2022-01-10T07:59:42Z")

</div>

> [@playcock](#):
>
> ```auto
> ?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.

---

<div class="post-metadata">

**Author:** ![playcock](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/playcock/32/6009_2.png) [@playcock](https://www.dolibarr.org/forum/u/playcock)\
**Post date:** [January 10, 2022, 2:09pm UTC](https://www.dolibarr.org/forum/t/attach-file-using-cmail-in-dolibarr/21681/4 "2022-01-10T14:09:20Z")

</div>

@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

```auto
filename_list: Array
(
    [0] => /var/www/path/temp/new7.csv
)

```

mimetype

```auto
mimetype_list: Array
(
    [0] => application/octet-stream
    [1] => application/vnd.ms-excel
)

```

filename

```auto
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.

---

<div class="post-metadata">

**Author:** ![msayyed](https://www.dolibarr.org/forum/letter_avatar_proxy/v4/letter/m/f0a364/32.png) [@msayyed](https://www.dolibarr.org/forum/u/msayyed)\
**Post date:** [January 10, 2022, 3:23pm UTC](https://www.dolibarr.org/forum/t/attach-file-using-cmail-in-dolibarr/21681/5 "2022-01-10T15:23:53Z")

</div>

thank you!!  
the problem was as you said there was no filename in the path.

---

<div class="post-metadata">

**Author:** ![playcock](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/playcock/32/6009_2.png) [@playcock](https://www.dolibarr.org/forum/u/playcock)\
**Post date:** [January 10, 2022, 3:25pm UTC](https://www.dolibarr.org/forum/t/attach-file-using-cmail-in-dolibarr/21681/6 "2022-01-10T15:25:57Z")

</div>

Awesome! Glad you are on your way this cron.
