Multiple Email Send Issues

Hi,

to = ‘to@gmail.com’
cc = context[testnotification] # == Pick from the custom notification profile model firstemail@gmail.com,secondemail@gmail.com
from_email = request.POST.get(‘fromemail’)
msg = EmailMultiAlternatives(subject, text_content,from_email,[to],[cc])
msg.attach_alternative(html_content, “text/html”)
msg.send() # Mail send code

Errors:

Exception Value: Invalid address; only firstemail@gmail.com could be parsed from “firstemail@gmail.com,secondemail@gmail.com

I am getting the above error while using in To or CC .

Can you edit your original post by surrounding your code in ```, please? We also may need more information regarding what you’re trying to do so if you can, please post the whole view :slight_smile:

Your recipient list needs to be a list of email addresses, not a string of comma-separated addresses.
e.g.

cc = ['first.email@gmail.com', 'second.email@gmail.com']

Thanks sir, let me try same if any issues let you know.

Hi Sir,

If I used hardcoded as suggested format it is working fine. But when I take data from the model to send email same errors are coming.

Here I am capturing the email id’s.

//Get dynamic values from model based on filter
leavnotifications = leavenotification.objects.filter(company_id=context[‘business_unit_id’])

    for lnotification in leavnotifications:
        context['staffnotification'] = lnotification.staff
        context['workernotification'] = lnotification.worker

//End Here*****

Email Send Code:

subject = 'Request has been approved by ’ + str(request.user.profile.full_name) + ’ at '+str(formatedDate)
to = context[‘workernotification’] # Values coming here from profile
print("To Emai **: "+to)
from_email = request.POST.get(‘level3’)
text_content = ‘This is an important message.’
html_content = '

Hi,
try:
msg = EmailMultiAlternatives(subject, text_content,from_email,[to])
msg.attach_alternative(html_content, “text/html”)
msg.send() # Mail send code
except BadHeaderError:
return HttpResponse(‘Invalid header found.’)
Errors:

Exception Value: Invalid address; only first@gmail.com’ could be parsed from “first@gmail.com’,'second@gmail.com”

Hi ,

If I used hardcoded as suggested format it is working fine. But when I take data from the model to send email same errors are coming.

Here I am capturing the email id’s.

//Get dynamic values from model based on filter
leavnotifications = leavenotification.objects.filter(company_id=context[‘business_unit_id’])

    for lnotification in leavnotifications:
        context['staffnotification'] = lnotification.staff
        context['workernotification'] = lnotification.worker

//End Here*****

Email Send Code:

subject = 'Request has been approved by ’ + str(request.user.profile.full_name) + ’ at '+str(formatedDate)
to = context[‘workernotification’] # Values coming here from profile
print("To Emai **: "+to)
from_email = request.POST.get(‘level3’)
text_content = ‘This is an important message.’
html_content = '

Hi,
try:
msg = EmailMultiAlternatives(subject, text_content,from_email,[to])
msg.attach_alternative(html_content, “text/html”)
msg.send() # Mail send code
except BadHeaderError:
return HttpResponse(‘Invalid header found.’)
Errors:

Exception Value: Invalid address; only first@gmail.com’ could be parsed from “first@gmail.com’,'second@gmail.com”

I would need to see the complete model you’re using to be sure I’m giving you the right advice.

If you’re using a text field, then it is up to you to convert that text into a list.
If you’re not, then it would also be helpful to see the complete view in which you’re trying to do this in addition to seeing the model.

(See the Python split function for some ideas.)

Hi Sir,

Please find the below model details.

class leavenotification(models.Model):

company = models.OneToOneField(business_unit, on_delete=models.DO_NOTHING)

staff=models.TextField(max_length=1000)

worker = models.TextField(max_length=1000)

def __str__(self):

    return str(self.company)+" - "+str(self.worker)

verbose_name_plural = "Leave Notification"

So yes, you are using a TextField for this, and so my advice from above applies.

Thank you very much sir, now I am using to.split(",") and working fine.

Once again thanks to you.