Here is the standard way to prove the result.
First note that there is a non-zero $\mathcal C^\infty$ function $\theta\geq 0$ on $\mathbb R$ which is supported on $[0,1]$.
From this, it follows that for any $\varepsilon >0$, there is a non-negative $\mathcal C^\infty$ function $\phi_\varepsilon$ on $\mathbb R^n$ which is supported on the closed euclidean ball $\overline B(0,\varepsilon)$ and such that $\int_{\mathbb R^n} \phi_\varepsilon (u) du=1$: just put $\phi_\varepsilon(x)= c_\varepsilon\, \theta\left(\frac{\Vert x\Vert^2}{\varepsilon^2}\right)$ for some suitably chosen constant $c_\varepsilon$.
Now, choose $\varepsilon>0$ such that $D+\overline B(0,2\varepsilon)\subset U$, and define $f$ to be the convolution $\mathbf 1_{D_\varepsilon}*\phi_\varepsilon$, where $D_\varepsilon=D+\overline B(0,\varepsilon)$:
$$f(x)=\int_{\mathbb R^n} \mathbf 1_{D_\varepsilon}(y)\phi_\varepsilon(x-y)\, dy\, . $$
Then, by the standard theorem on differentiation under the integral sign, $f$ is $\mathcal C^\infty$; and by a well known property of the support of a convolution, $${\rm supp}(f)\subset D_\varepsilon+{\rm supp}(\phi_\varepsilon)\subset D+\overline B(0, 2\varepsilon)\subset U\, .$$
Finally, $f$ is equal to $1$ on $D$. Indeed, write
$$f(x)=\int_{\mathbb R^n} \mathbf 1_{D_\varepsilon}(x-y) \phi_\varepsilon (y)\, dy=
\int_{\overline B(0,\varepsilon)} \mathbf 1_{D_\varepsilon}(x-y) \phi_\varepsilon (y)\, dy $$
and observe that if $x\in D$, then $x-y\in D_\varepsilon$ for every $y\in\overline B(0,\varepsilon)$, i.e. $\mathbf 1_{D_\varepsilon}(x-y)=1$. It follows that for $x\in D$ we have
$$ f(x)=\int_{\overline B(0,\varepsilon)} \phi_\varepsilon (y)\, dy=\int_{\mathbb R^n} \phi_\varepsilon(y)\, dy=1\, .$$
$\bf Edit.$ If you want to find the function $f$ just by using the existence of a partition of unity as you stated it, you can do this assuming that your partition of unity $(\phi_i)_{i\in I}$ is relative to $U$ and is locally finite, i.e. each point $x\in\mathbb R^n$ has a neighbourhood $V_x$ on which all but finitely many functions $\phi_i$ are $0$.
Assume that the closure of $E$ is contained in $U$. By compactness, you can cover $\overline E$ by finitely open sets $V_{x_1},\dots ,V_{x_N}$ as above; and moreover you may assume that $V_{x_j}\subset U$ for all $j$. For each $j\in\{ 1,\dots ,N\}$, choose a finite set $I_j\subset I$ such that $\phi_i\equiv 0$ on $V_{x_j}$ for all $i\not\in I_j$. Then let $I':=\bigcup_{j=1}^N I_j$ and $f:=\sum_{i\in I'} \phi_i$. The function $f$ is perfectly well-defined and $\mathcal C^\infty$ on $\mathbb R^n$ since this is a finite sum, and you do have $f\equiv 1$ on $E$ because $\phi_i\equiv 0$ on $\overline E$ for all $i\not\in I'$ and hence $f\equiv\sum_{i\in I}\phi_i=1$ on $E$.