How do I prevent duplicate messages in custom order confirmations?

I am trying to send custom email confirmations based on tags and titles. The issue I’m getting is that if someone buys multiple courses, then they get duplicate messages in the confirmation. I’m assuming it’s because the if statements are inside of the loop, but I tried doing it outside of the loop and it didn’t work.

{% for line_item in line_items %}

    {% if line_item.product.tags contains 'passport' %}
        

Message 1

    {% elsif line_item.product.title == "CPTM Alumni Professional Development Subscription" %}
        

Message 2

        
    {% elsif line_item.product.title == "Certified Professional in Training %}
        

Message 3

    {% elsif line_item.product.tags contains 'Courses' %}
        

Message 4

    {% else %}
        

Message 5

    {% endif %}

{% endfor %}