Tony Blair was told by his government's most senior legal adviser that an invasion of Iraq to topple Saddam Hussein would be a serious breach of international law and the UN charter.

Lord Goldsmith, then attorney general, issued the warning in an uncompromising letter in July 2002, eight months before the invasion. It was becoming clear in government circles that Blair had had secret meetings with George Bush at which the US president was pressing Britain hard to join him in a war to change the regime in Baghdad.

The hitherto unpublished letter has been given to the Chilcot inquiry on the Iraq war. Goldsmith is due to be questioned about it early in the new year with other senior government lawyers who questioned the legality of an invasion at the time. Goldsmith warned Blair that "as things stand you obviously cannot do it [invade Iraq]", a source familiar with the dispute told the Guardian.

Increasingly concerned that Blair was ignoring his earlier advice that regime change was "not a legal basis for military action", on 29 July 2002 Goldsmith wrote to Blair on what the Mail on Sunday described as "a single side of A4 headed notepaper".

The typed letter was addressed by hand, "Dear Tony", and signed by hand, "Yours, Peter". In the letter, whose existence was confirmed by other sources, Goldsmith warned that the UN charter permitted "military intervention on the basis of self-defence", but it did not apply here because Britain was not under threat from Iraq; that the UN allowed "humanitarian intervention" in some circumstances, but such was not the case in Iraq; and that it would be very difficult to rely on earlier UN resolutions approving the use of force against Saddam.

Goldsmith is reported to have ended his letter saying "the situation might change".

Blair not only ignored Goldsmith's letter, he banned the attorney from attending cabinet meetings. Goldsmith was so angry that he threatened to resign – and lost three stone in weight as Blair and his closest advisers gagged him, according to the Mail on Sunday. A spokesman for Goldsmith told the paper: "His focus is on the legality of the war, its morality is for others."

Goldsmith issued his private warning to Blair before a new UN security council resolution – number 1441, in November 2002 – held Iraq in "material breach" of its disarmament obligations and gave Saddam "a final opportunity" to comply with them. He subsequently allowed UN weapons inspectors into the country. The inspectors were withdrawn once Bush made it clear he was about to bomb Iraq and invade the country.

On 7 March 2003, Goldsmith warned the government that although Saddam could be said to be in breach of his international obligations, British forces could still face legal action if they participated in an invasion. Ten days later, he issued a brief statement saying invasion would be lawful. The Butler inquiry into the use of intelligence to justify war revealed that Goldsmith changed his advice after a meeting with two of Blair's close advisers, Lady (Sally) Morgan and Lord Falconer.

It also revealed that a worried Lord Boyce, then chief of the defence staff, needed "unequivocal" advice that the invasion was lawful. He got it after Goldsmith's office contacted 10 Downing Street. Downing Street advised the attorney to say that an invasion would, indeed, be lawful, and unequivocally so. Boyce is due to give evidence to the Chilcot inquiry this week.

Lord Falconer told the Mail on Sunday that the version of events it described was "totally false".