Skip to main content
Answer

How do I programmatically attach files to an email? Might be a bug.

  • September 12, 2025
  • 2 replies
  • 71 views

MichaelShirk
Captain II
Forum|alt.badge.img+5


Would someone be willing to review this sample customization and confirm if it’s a bug that needs to be reported, or if I’m not attaching the file correctly? 


You can see the screenshots below, showing the UploadFile.Name field in the Database, before and after sending the email. This change to the file name happens during the PXNoteAttribute.AttachFile() method call. The record key fields in the file name are changed to a GUID. 

Files Names before sending the email.
File Names after sending the email.



I was able to solve this by programmatically creating a copy of the file, then attaching that to the email. However, we really don’t want to be creating that many duplicate files.

This causes an issue in our production instance: We have a lot of revisions on our sales orders, and with this bug, when new files are uploaded after sending the email, a duplicate is created, rather than a new file revision, because the names don’t match anymore.
 

Best answer by MichaelShirk

Here’s what was happening. 

The PXNoteAttribute.AttachFile() method correctly renames the file with the key fields of the record the files are being attached to. In this case, since it was an email (CRActivity in the db), the key field was a GUID. This then meant that the original file attached to the Sales Order was also renamed, and when sales order revision documents were uploaded, they didn’t upload as a new version of the same file, but as a separate file. 

The solution is to instead use the PXNoteAttribute.SetFileNotes() method to link the existing files to the email. This retains the same file names and solves the issue.

2 replies

Forum|alt.badge.img+1
  • Jr Varsity I
  • September 12, 2025

Hi Michael, here is an another approach I have seen in the past which creates a copy to attach to the email instead, although I am not sure if this helps much as it would still be creating a copy. Just wanted to share in case this alternate method could work for you.

 

UploadFileMaintenance uploadGraph = PXGraph.CreateInstance<UploadFileMaintenance>();
foreach (var note in files)
{
    PX.SM.FileInfo file1 = uploadGraph.GetFile(note);

    PX.SM.FileInfo newFileInfo = new PX.SM.FileInfo(
        Guid.NewGuid(),
        null,
        file1.Name,
        file1.OriginalName,
        file1.Link,
        file1.BinData,
        file1.Comment
    );

    if (uploadGraph.SaveFile(newFileInfo, FileExistsAction.CreateVersion))
    {
        try
        {
            var noteDoc = CreateNoteDoc(newFileInfo.UID, email.NoteID);
            emailActivityMaint.Caches[typeof(NoteDoc)].Insert(noteDoc);

            PXNoteAttribute.SetFileNotes(emailActivityMaint.Caches[typeof(CRSMEmail)], email, newFileInfo.UID.Value);
        }


MichaelShirk
Captain II
Forum|alt.badge.img+5
  • Author
  • Captain II
  • Answer
  • September 16, 2025

Here’s what was happening. 

The PXNoteAttribute.AttachFile() method correctly renames the file with the key fields of the record the files are being attached to. In this case, since it was an email (CRActivity in the db), the key field was a GUID. This then meant that the original file attached to the Sales Order was also renamed, and when sales order revision documents were uploaded, they didn’t upload as a new version of the same file, but as a separate file. 

The solution is to instead use the PXNoteAttribute.SetFileNotes() method to link the existing files to the email. This retains the same file names and solves the issue.