For and by are completely different words and can hardly be compared. I think in Spanish, 'por' is used both as 'for' and 'by' and hence the doubt.
e.g.
The book was written by him : He is the author of the book
the book was written for him : Somebody wrote the book and dedicated it (for example) to him
******************************************************************************************
Otherwise,
for : preposition : means
1. with the object or purpose of:
e.g. to run for exercise.
2. intended to belong to, or be used in connection with:
e.g. equipment for the army; a closet for dishes.
3. suiting the purposes or needs of:
e.g. medicine for the aged.
4. in order to obtain, gain, or acquire:
e.g. a suit for alimony; to work for wages.
5. (used to express a wish, as of something to be experienced or obtained): e.g. O, for a cold drink!
******************************************************************************************
By has many more meanings. Some are as follows
1. near to or next to: a home by a lake.
2.over the surface of, through the medium of, along, or using as a route: He came by the highway. She arrived by air.
3.on, as a means of conveyance: They arrived by ship.
4.to and beyond the vicinity of; past: He went by the church.
5.within the extent or period of; during: by day; by night.