A Coleraine man who bought heroin over the internet was caught when the package was delivered to a neighbour by mistake.
Ryan Duggan (31), of Laurel Hill Gardens, appeared yesterday at Coleraine Magistrates Court, sitting in Ballymena, for attempting to possess heroin.
A prosecutor said a package addressed to the defendant containing around five grammes of the drug had been delivered to the wrong address by Royal Mail.
The prosecution lawyer said a householder opened the package thinking it was mail for them and discovered brown powder and suspected it was drugs.
The next day Duggan went to the address in an attempt to retrieve the package. A postal operator was present and police were contacted.
The defendant admitted the offence to officers.
A defence solicitor said the defendant had received treatment for addiction and was now "clean" and on methadone.
He said the drugs had been ordered on the internet and after being delivered to a neighbour Duggan had gone looking for them.
The offences came to light a day after Duggan was given a suspended sentence for another matter.
The solicitor said the drugs had been ordered before the defendant received the suspended sentence.
District Judge Peter King said Duggan had 50 previous convictions and said "the easy thing to do would be to send you to prison", but he wanted to break the cycle of offending and deferred sentencing to September.
He told the defendant if he remained drug-free and did not reoffend he would avoid prison when the case returns to court in the autumn.
The judge said Duggan was "doing good work, don't spoil it".