Add mail sent by code to History Mail

Solved >>>

$emailObj = new Email(); 
	$defaults = $emailObj->getSystemDefaultEmail(); 

	$mail = new SugarPHPMailer(); 
	$mail->setMailerForSystem(); 
	$mail->From = $defaults['email']; 
	$mail->FromName = $defaults['name']; 
	$mail->Subject = "Conferma presa in carico pratica N ". $bean->numeropratica_c;
	$mail->Body = $body;
        $mail->AddAttachment($dirAllegato . $bean->numeropratica_c.'.pdf', $bean->numeropratica_c, 'base64', 'application/pdf');
	$mail->isHTML(true);
	$mail->prepForOutbound();
	$mail->AddAddress('massimo.capuano.bari@gmail.com');
	$mail->Send();
	
	$objDateTime = new DateTime('NOW');
 

	$MyEmail = BeanFactory::newBean('Emails');
	$MyEmail->name="Conferma presa in carico pratica N ". $bean->numeropratica_c;
	$MyEmail->type="out";
	$MyEmail->status="sent";
	$MyEmail->parent_id=$bean->id;
	$MyEmail->date_sent_received=$objDateTime;
	$MyEmail->description_html = $body;
	$MyEmail->save();
	$sender=$defaults['name'];
	$object_arr= array('Contacts' => '123');
	$bodymod= "'".$body."'";
        $bean->load_relationship('prt_pratiche_activities_1_emails');
        $bean->prt_pratiche_activities_1_emails->add($MyEmail);
	$idmail="'".$MyEmail->id."'";
	
	//$mail->AddAttachment($fileLocation, $bean->numeropratica_c.'.pdf', 'base64', 'application/pdf');

	 $note = BeanFactory::newBean('Notes');
                        $note->id = create_guid();
			$note->description = "Inviato";
                        $note->new_with_id = true; // duplicating the note with files
                        $note->parent_id = $bean->id;
                        $note->parent_type = $bean->module_dir;
                        $note->name = $bean->numeropratica_c.'.pdf';
                        $note->filename = $note->id;
                        $note->file_mime_type = 'application/pdf';
			
                        $dest = "upload://{$note->id}";
                        if (!copy($dirAllegato.$bean->numeropratica_c.'.pdf', $dest)) {
                            $GLOBALS['log']->debug("EMAIL 2.0: could not copy attachment file to $dirAllegato => $dest");
                        }

                        $note->save();
2 Likes