I understand your confusion, but the ambiguity there is inherent to using the word "por", which has multiple meanings. You would have to use the context to translate. Also, there would be at least another meaning,
This letter was written because of Juan.
However, I can say that it would be very rare to actually say that sentence with any but the first meaning
This letter was written by Juan. The speaker would try to avoid the ambiguity using different words or adding some explanation. Such as:
Escribí la carta en nombre de Juan (on his behalf)
Escribí la carta por culpa de Juan (negative connotation) or
Escribí la carta gracias a Juan (positive connotation) -> (Because of Juan)