如何附加位于我的 Web 服务器上的文件以发送电子邮件?

How do I attach files which are located on my web server to send email?

我正在尝试按照 https://msdn.microsoft.com/office/office365/APi/mail-rest-operations#SendMessageOnTheFly 和 PHP 上带有附件的文档发送电子邮件。到目前为止,我可以发送一封没有附件的普通电子邮件。但是我该如何处理附件呢?

根据文档,响应应如下所示:

POST https://outlook.office.com/api/v2.0/me/sendmail
{
  "Message": {
    "Subject": "Meet for lunch?",
    "Body": {
      "ContentType": "Text",
      "Content": "The new cafeteria is open."
    },
    "ToRecipients": [
      {
        "EmailAddress": {
          "Address": "garthf@a830edad9050849NDA1.onmicrosoft.com"
        }
      }
    ],
    "Attachments": [
      {
        "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
        "Name": "menu.txt",
        "ContentBytes": "bWFjIGFuZCBjaGVlc2UgdG9kYXk="
      }
    ]
  },
  "SaveToSentItems": "false"
}

我的是这样的:

public static function sendMessage($access_token, $user_email, $subject, $Content, $email)
{
    $url = "https://example.com/upload.txt";
    $base64 = base64_encode(file_get_contents($url));
    $arr= array(
        "Message" =>array(
            'Subject' => $subject,
            "Body"=>array(
                "ContentType"=>"HTML",
                "Content"=>$Content,
            ),
        "ToRecipients"=>array(
            array(
                "EmailAddress"=>array(
                    "Address"=>$email,
                )
            ),
        ),
        "Attachments"=> array(
            array(
                "@odata.type"=> "#Microsoft.OutlookServices.FileAttachment",
                "Name" => "upload.txt",
                "ContentLocation"=> $url,
                "ContentBytes"=> $base64,
                "ContentType" => "text/plain"
            )
        )
    ));

    $json=json_encode($arr, true);
    $getMessagesUrl = self::$outlookApiUrl."/me/sendmail";

    return self::makeApiCall($access_token, $user_email, "POST",$getMessagesUrl,$json);
}

然后我在外面调用方法:

var_dump(OutlookService::sendMessage($tokens['access_token'], $_SESSION['user_email'], 'subject', 'body', 'abc@gmail.com'));

我对此一无所知。这不起作用并给我一个 400 错误。我知道 400 错误,但不知道是什么原因造成的。

我应该在这里做什么?什么是正确的格式?

使用您的示例,我首先想到的是您没有像示例中那样设置附件

文档还指定 SavetoSentItems 是必需的,因此也应该添加

这是修改后的版本,应该按照示例中的要求打印出 json:

public static function sendMessage($access_token, $user_email, $subject, $Content, $email)
{
    $url = "https://example.com/upload.txt";
    $base64 = base64_encode(file_get_contents($url));
    $arr= array(
        "Message" =>array(
            'Subject' => $subject,
            "Body"=>array(
                "ContentType"=>"HTML",
                "Content"=>$Content,
            ),
        "ToRecipients"=>array(
            array(
                "EmailAddress"=>array(
                    "Address"=>$email,
                )
            ),
        ),
        "Attachments"=> array(
            array(
                "@odata.type"=> "#Microsoft.OutlookServices.FileAttachment",
                "Name" => "upload.txt",
                "ContentLocation"=> $url,
                "ContentBytes"=> $base64,
                "ContentType" => "text/plain"
            )
        )
    ),
    "SaveToSentItems" => "false"
    );

    $json=json_encode($arr, true);
    $getMessagesUrl = self::$outlookApiUrl."/me/sendmail";

    return self::makeApiCall($access_token, $user_email, "POST",$getMessagesUrl,$json);
}

仅更改行进行编辑:

    "Attachments"=> array(
        array(
            "@odata.type"=> "#Microsoft.OutlookServices.FileAttachment",
            "Name" => "upload.txt",
            "ContentLocation"=> $url,
            "ContentBytes"=> $base64,
            "ContentType" => "text/plain"
        )
    )