A reliable rule of thumb is to ensure at least 1.5× the screw diameter of thread engagement in the receiving material. For M6, that means at least 9mm of thread engagement; for M8, at least 12mm; for M10, at least 15mm. Add the thickness of all clamped parts to determine your minimum screw length.
Quick inch reference: 8mm ≈ 5/16" · 10mm ≈ 3/8" · 16mm ≈ 5/8" · 20mm ≈ 13/16" · 25mm ≈ 1" · 30mm ≈ 1-3/16" · 40mm ≈ 1-9/16" · 50mm ≈ 2" · 60mm ≈ 2-3/8"
Important: The listed length is the thread shank only and does not include the head height. Factor this in when measuring your assembly stack-up.