Search This Blog

Sunday, July 7, 2024

Send Email from Django Project with Domain HostMyCode SMTP

 url.py

from django.contrib import admin

from django.urls import path

from my_app import views


urlpatterns = [

    path('admin/', admin.site.urls),

    path('',views.index, name="homepage"),

    path('send-email/', views.send_test_email, name='send_email'),

]


settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.arsenalit.com'  # Replace with your SMTP host
EMAIL_PORT = 587  # Usually 587 for TLS
EMAIL_USE_TLS = True  # Use TLS for secure connection
EMAIL_USE_SSL = False  # Set to False if using TLS
EMAIL_HOST_USER = 'email@arsenalit.com'  # Your SMTP username/email
EMAIL_HOST_PASSWORD = 'password'  # Your SMTP email password
DEFAULT_FROM_EMAIL = 'from@arsenalit.com'  # Default from email
EMAIL_TIMEOUT = 60


views.py

def send_test_email(request):
    #import pdb;
    #pdb.set_trace();
    subject = 'Test Email'
    message = 'This is a test email sent from Django using HostMyCode SMTP server.'
    from_email = 'from@arsenalit.com'
    recipient_list = ['toemail@gmail.com']
    
    try:
        print ( "calling send Email");
        send_mail(subject, message, from_email, recipient_list)
        return HttpResponse('Email sent successfully')
    except Exception as e:
        return HttpResponse(f'Error sending email: {e}')
        

Hit Counter


View My Stats