Solution 1 :

The code is perfect. The mistake was that the email request.post was getting was an invalid email.

Thanks guys for helping. It works perfectly now.

Problem :

Please can anyone help me with this issue. I am trying to allow the users of my website to send out review request to customers by filling out a form on their profile page. They only have to provide the email address of the recipient then the backend would use this to configure a HTML message then send to the recipient. Currently, the whole system works just fine if I hard code the recipient email address. But once I try to get the email from request.POST[‘receiver’] it seems not to be passing the argument to the function.

Here is the view function:

def request_review_api(request):
receiver_email = request.POST['receiver']
print(receiver_email)
request_review(request, receiver_email)

return redirect('profile_company')


@login_required(login_url='loginpage_company')
@allowed_users_company(allowed_roles=['company'])
def request_review(request, receiver_email):
    company = get_object_or_404(Company, user=request.user)
    company_id = company.pk
    print(company)
    html_tpl_path = 'companyusers/request_review.html'
    context_data = {'company': company, 'company_id': company_id,}
    email_html_template = get_template(html_tpl_path).render(context_data)
    receiver = receiver_email 
    
    email_msg = EmailMessage('Review Request',
                                email_html_template,
                                settings.EMAIL_HOST_USER,
                                [receiver],
                                reply_to=['[email protected]'],

                                )
    # this part allows the message to be send as html instead of plain text
    email_msg.content_subtype = 'html'
    email_msg.send(fail_silently=False)

This is what I have in my Template:

<p class="tm-request-review-display card-text">
                               
                                <form class="tm-request-review-display" action="{%url 

'request_review' %}" method="POST"> {% csrf_token %}
                                    
                                    <div class="form-group">
                                        <div class="col-md">
                                            <input type="email" class="form-control" name="receiver" 

id="receiver" placeholder="Enter Reviewer's Email">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md">
                                                <input class="btn btn-primary btn-sm" type="submit" 

value="Send Request">
                                                </div>
                                            </div>
                                            </form>
                                        </p>

The email gets sent successfully once I hard code the recipient email like this:

def request_review_api(request):
    request_review(request)
    
    return redirect('profile_company')


@login_required(login_url='loginpage_company')
@allowed_users_company(allowed_roles=['company'])
def request_review(request):
    company = get_object_or_404(Company, user=request.user)
    company_id = company.pk
    print(company)
    html_tpl_path = 'companyusers/request_review.html'
    context_data = {'company': company, 'company_id': company_id,}
    email_html_template = get_template(html_tpl_path).render(context_data)
    receiver_email = '[email protected]' 
    
    email_msg = EmailMessage(' Review Request',
                                email_html_template,
                                settings.EMAIL_HOST_USER,
                                [receiver_email],
                                reply_to=['[email protected]'],

                                )
    # this part allows the message to be send as html instead of plain text
    email_msg.content_subtype = 'html'
    email_msg.send(fail_silently=False)

Comments

Comment posted by Branko Radojevic

Why you use square brackets while calling EmailMessage for receiver? And why don’t you just use receiver_email in your example that does not work? Have you tried to print out value of receiver_message inside request_review function?

Comment posted by Moha369

Can you show your

Comment posted by prynet

The square bracket is because that a list. that means it can take more than one receiver separated by a comma. If you remove the square bracket you get this error: “to” argument must be a list or tuple

Comment posted by prynet

This is the url path: path(‘request_review’, views.request_review_api, name=’request_review’),

Comment posted by prynet

Yes Sir, I have printed the value of receiver_email in the request_review function and it prints just well. Just that is doesnt get passed in to email_msg = EmailMessage(‘Review Request’, email_html_template, settings.EMAIL_HOST_USER, [receiver_email], reply_to=[‘[email protected]’], )

By