Add mail sent by code to History Mail

Hi,

i send mail by code how to i can add mail sent to history ??

$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 richiesta di finanziamento Pratica N ". $bean->numeropratica_c." Relativo a ".$bean->pratiche_name; 
	$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();

I never looked at this in detail, but my guess would be that you need to add a relationship between the email and the Contact (or Lead, or Account) records

Hi is present relationship
$bean->load_relationship(‘prt_pratiche_activities_1_emails’);
$bean->prt_pratiche_activities_1_emails->add($mail);
i have this error…

PHP Recoverable fatal error: Object of class SugarPHPMailer could not be converted to string in /home/BCL-CRM/data/SugarBean.php on line 4525

Yes, you can’t pass it the PHPMailer object… it’s probably expecting an email Bean, which you will need to create.

Your best shot is to find examples in the code where this is done. Although sometimes it’s simpler to just figure out what is happening in the database and go for direct SQL updates.

This seems to be a case of this getting added:

1 Like

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