Python 在使用extract_msg时,如何保持电子邮件中的换行符?

Python 在使用extract_msg时,如何保持电子邮件中的换行符?,python,Python,我正在使用Matthew Walker的extract msg模块(我很喜欢);但是,在发布到Microsoft TFS(TFSAPI模块)时,msg.body删除换行符时出现问题。到目前为止,电子邮件的主题带有签名,以及之前的回复,这使得它有时很难阅读。每封邮件都是不同的,所以我不确定如何将不需要的内容拼接出来 我不确定是TFSAPI还是extract msg模块导致了问题 是否可以保留原始电子邮件中的换行符和格式,或将其靠近原始电子邮件?下面是TFS中显示内容的示例 下面是powershe

我正在使用Matthew Walker的extract msg模块(我很喜欢);但是,在发布到Microsoft TFS(TFSAPI模块)时,msg.body删除换行符时出现问题。到目前为止,电子邮件的主题带有签名,以及之前的回复,这使得它有时很难阅读。每封邮件都是不同的,所以我不确定如何将不需要的内容拼接出来

我不确定是TFSAPI还是extract msg模块导致了问题

是否可以保留原始电子邮件中的换行符和格式,或将其靠近原始电子邮件?下面是TFS中显示内容的示例


下面是powershell脚本中使用

您也可以在页面中参考其他编程语言中的其他示例

$url=”https://dev.azure.com/{org}/{project}{u/api/wit/workitems/{workitem id}?api版本=5.1“
$body=[
{
“op”:“添加”,
“路径”:“/字段/系统说明”,
“值”:“这是H1标题
这是H2标题

” } ]' $connectionToken=“” $base64AuthInfo=[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(“:$($connectionToken)”) $result7=Invoke RestMethod-Uri$url-Headers@{authorization=“Basic$base64AuthInfo”}-Method patch-ContentType“application/json patch+json”-Body$Body
您确定邮件断线了吗?通常在一行中发送一整段的电子邮件,并依靠邮件客户端将其包装。@Barmar除非我遗漏了什么,否则我已经通过创建一封电子邮件并键入文本,点击几次enter按钮并添加更多文本来测试自己;然而,当我看到完成的结果时,它只是一行很长的文本。有什么想法吗?如果你能将电子邮件正文转换为html格式并更新到azure devops。我试过了。它可以识别htmlformat@LeviLu-MSFT收到的电子邮件是html格式的。我想这更像是一个回车的问题。仍在努力寻找最佳解决方案。您是否有使用workitem update api的示例?我对编码有点陌生。提前谢谢!嗨@MichaelJackson你看过下面的剧本了吗?怎么样?
def e2tfs():
    '''Extract details from emails and assign those fields to TFS Support Ticket'''
    associate = form.getvalue('associate')
    i=1
    for file in os.listdir():
        src = file
        msg = extract_msg.Message(src)
        msg_sender = msg.sender
        msg_date = msg.date
        msg_subj = msg.subject
        msg_message = msg.body
        i+=1

        #Associate the emails details to the TFS fields
        fields = {'System.Title' : 'E2TFS: {}'.format(msg_subj),
                  'Microsoft.VSTS.CMMI.Symptom': 'body: {}' .format(msg_message),
                  'Microsoft.VSTS.TCM.ReproSteps': 'TBD',
                  'Regions.Custom.DocumentationArea': 'Unknown',
                  'Regions.Custom.Application': 'nCino',
                  'Regions.Custom.Channel': 'Email',
                  'Microsoft.VSTS.CMMI.FoundInEnvironment': 'Production',
                  'Regions.Custom.ImpactedAssociate': 'Sender: {}'.format(msg_sender),
                  'Regions.Custom.Associate_Role': 'ALL USERS',
                  'Regions.Custom.BusinessGroupsImpacted2': 'All Business Groups',
                  'AFS.phase.dev': 'ALL USERS',
                  'Regions.Custom.PriorityCustomField': 'High',
                  'Regions.Custom.CaseOwner': associate,
                  'History': 'Ticket created from email sent directly to CBG Level II Support on  {}'.format(msg_date)
                  }      


        client.create_workitem('Support Ticket', fields=fields)

        query_tfs = "SELECT [System.Id], [System.Title] FROM workitems WHERE [System.CreatedDate] = @today AND [System.CreatedBy] = @me AND [System.WorkItemType] = 'Support Ticket'"


        wiql = client.run_wiql(query_tfs)


        # Get all found workitems
        workitems = wiql.workitems
        tfs_number = workitems[-1]['Id']
$url= "https://dev.azure.com/{org}/{project}_/apis/wit/workitems/{workitem id}?api-version=5.1"

$body = '[
    {
    "op": "add",
    "path": "/fields/System.Description",
    "value": "<p>This is a H1 header<br/>This is a H2 header</p>"
    }
]'

$connectionToken="<PAT>"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$result7 = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method patch -ContentType "application/json-patch+json" -Body $body