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

409 views Asked by At

I am trying to send email following the docs with attachment on https://msdn.microsoft.com/office/office365/APi/mail-rest-operations#SendMessageOnTheFly with PHP. So far I can send a plain email with no attachments. But how do I do it with attachments?

According to docs this is what the response should look like:

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": "[email protected]"
        }
      }
    ],
    "Attachments": [
      {
        "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
        "Name": "menu.txt",
        "ContentBytes": "bWFjIGFuZCBjaGVlc2UgdG9kYXk="
      }
    ]
  },
  "SaveToSentItems": "false"
}

What I have is this:

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);
}

and then I call the method outside:

var_dump(OutlookService::sendMessage($tokens['access_token'], $_SESSION['user_email'], 'subject', 'body', '[email protected]'));

I am clueless about going with this. This is not working and giving me a 400 error. I am aware of the 400 error but don't know what could be causing it.

What should I be doing here? What's the right format?

1

There are 1 answers

2
Rares Hideg On BEST ANSWER

Working with your example, the first thing that poped in my eyes was that you did not set you attachments as in the example

also the documentation specifies SavetoSentItems is required, so that should be added also

Here is the modified version that should print out the json as requested in the sample :

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);
}

edit with only changed lines :

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