How to Configure SMTP to Send Emails from Multiple Addresses in Django
Sending emails is a fundamental feature for many web applications, whether it's for user notifications, password resets, or marketing campaigns. However, businesses often need the flexibility to send emails from multiple addresses, such as sales@yourdomain.com for inquiries or core@yourdomain.com for customer assistance. Configuring SMTP in Django to handle multiple email addresses can seem daunting at first, but with the right setup, it's a straightforward process. In this guide, we’ll walk you through step-by-step instructions on how to configure Django’s email settings to send emails seamlessly from various addresses while ensuring deliverability and compliance with email server requirements.

To send emails from sales@yourdomain.com or core@yourdomain.com using Django, you can follow these steps:
1. Update settings.py
Ensure your email configuration is set up correctly for the email backend in your Django project's settings.py file:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.yourdomain.com'
EMAIL_HOST_USER = 'info@yourdomain.com' # This is your SMTP account
EMAIL_HOST_PASSWORD = 'ldkfjslkgj' # Password for the SMTP account
EMAIL_PORT = 465
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
DEFAULT_FROM_EMAIL = 'info@yourdomain.com'2. Use from_email in Email Sending
In Django, when sending an email, you can override the default from_email by specifying it explicitly. For example:
from django.core.mail import send_mail
# Sending an email from sales@yourdomain.com
send_mail(
subject="Subject of the Email",
message="This is the body of the email.",
from_email="sales@yourdomain.com", # Override the default sender
recipient_list=["recipient@example.com"],
fail_silently=False,
)
3. Add Sales and Core as Verified Senders
For the sales@yourdomain.com and core@yourdomain.com addresses to work, they must be recognized as valid senders by your SMTP server. You might need to:
Add these email addresses as aliases or allowed senders in the SMTP server configuration.
Contact your mail server admin if you don’t have direct access.
4. Send Email from Multiple Addresses Programmatically
If you frequently send emails from different addresses, you can pass from_email dynamically, depending on the context:
def send_custom_email(subject, message, recipient_list, sender):
send_mail(
subject=subject,
message=message,
from_email=sender, # Pass the desired sender dynamically
recipient_list=recipient_list,
fail_silently=False,
)
# Example usage:
send_custom_email(
subject="Welcome Email",
message="Hello, welcome to Your Comapany!",
recipient_list=["recipient@example.com"],
sender="core@yourdomain.com"
)
5. Testing
Run the Django shell to test email functionality:
python manage.py shell
Execute:
from django.core.mail import send_mail
send_mail(
subject="Test Email",
message="Testing email functionality.",
from_email="sales@yourdomain.com",
recipient_list=["your-email@example.com"],
fail_silently=False,
)
If you encounter errors, ensure the SMTP credentials are correct, and the server allows sending emails from the specified addresses.
Configuring email aliases or allowed senders on an SMTP server depends on the specific mail server software being used (e.g., Postfix, Exim, Microsoft Exchange, etc.). Below are general guidelines for popular mail servers. You may need administrative access to the server to make these changes.
1. For Postfix
a. Configure Aliases
Edit the Alias File
Open the Postfix aliases file (usually /etc/aliases or similar):
sudo nano /etc/aliasesAdd the Aliases
Add entries for sales@yourdomain.com and core@yourdomain.com, pointing them to info@yourdomain.com:
sales: info@yourdomain.com
core: info@yourdomain.comRebuild the Aliases Database
Run the following command to apply changes:
sudo newaliasesRestart Postfix
Restart the Postfix service to apply changes:
sudo systemctl restart postfixb. Configure Sender Dependent Relay
If you want to explicitly allow these addresses for sending, configure a sender-dependent relay. Update the sender_dependent_relayhost_maps file:
Edit the File:
sudo nano /etc/postfix/sender_dependent_relayhost_mapsAdd Entries:
sales@yourdomain.com [mail.yourdomain.com]:465
core@yourdomain.com [mail.yourdomain.com]:465Postmap the File:
sudo postmap /etc/postfix/sender_dependent_relayhost_mapsReload Postfix:
sudo systemctl reload postfix2. For Exim
a. Configure Allowed Senders
Edit the Configuration File
Open the Exim configuration file:
sudo nano /etc/exim4/exim4.conf.templateAdd the Senders
Add a trusted_users or trusted_senders section:
trusted_senders = info@yourdomain.com:sales@yourdomain.com:core@yourdomain.comRestart Exim
Apply the changes:
sudo systemctl restart exim43. For Microsoft Exchange
Add SMTP Send As Permissions
Login to Exchange Admin Center.
Navigate to Mailboxes:
Go to Recipients > Mailboxes.
Select the Routing Account:
Click on info@yourdomain.com and choose Manage Send As Permissions.
Add Users/Addresses:
Add sales@yourdomain.com and core@yourdomain.com to the list of allowed senders.
Save Changes.
4. For cPanel/WHM
a. Add Email Aliases
Log in to cPanel/WHM.
Go to Email > Aliases or Forwarders.
Add Forwarding Rules:
Forward sales@yourdomain.com and core@yourdomain.com to info@yourdomain.com.
b. Ensure SPF, DKIM, and DMARC Records
Make sure your DNS records (SPF, DKIM, DMARC) are set to allow emails to be sent from the mail.yourdomain.com server for yourdomain.com domain.
5. Testing Configuration
After adding aliases or allowed senders:
Test by sending an email from sales@yourdomain.com and core@yourdomain.com using your Django app.
Check mail server logs for errors if emails fail to send.
If you don't have access to the SMTP server, contact your email service provider or system administrator and provide them with the required aliases or sender permissions for configuration.
Configuring SMTP to send emails from multiple addresses in Django not only enhances your application’s functionality but also helps maintain a professional and organized communication strategy. By setting up your email server correctly and leveraging Django’s flexibility, you can handle diverse business needs—whether it’s sales inquiries, customer support, or transactional emails. Following the steps outlined in this guide ensures smooth email delivery, better branding, and improved user experience. With this setup in place, you’re now equipped to manage emails effectively and scale your Django project with confidence.
📤 Share this article
Sign in to saveadmin
Writer at Bitsfolio. Passionate about Python, Data Analytics, and making complex tech topics accessible.
View all articles →Related Articles
Comments (0)
No comments yet. Be the first!