0

I've been trying to let users send email to my personal account via form. At first it was showing me

socket.gaierror: [Errno 11004] getaddrinfo failed

error which I solved by installing and configuring hMailServer in my device. Now when I try to send mail by my friend's email it send email by my own account instead. When I removed MAIL_HOST_USER = 'my_email' from settings file, it shows

smtplib.SMTPSenderRefused

error as mentioned in title. Any suggestions or related links will be appreciated.

My code is:

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_PORT = 25
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my_email'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_USE_TLS = True

views.py

class contact(View):
def post(self, request):
    form = ContactForm(request.POST)
    if form.is_valid():
        if send_mail(subject = form.cleaned_data['title'], message = form.cleaned_data['content'], from_email = form.cleaned_data['contact_email'], recipient_list = ['my_email'], fail_silently=False):
            return render(request, 'index.html', {'form': form, 'message': 'Message delivered successfully', 'error':'', 'email_sent': 1 })
    else:   
        return render(request, 'index.html', {'form': ContactForm(), 'error': form.errors})
LundinCast
  • 9,412
  • 4
  • 36
  • 48
Aayush Dhakal
  • 73
  • 2
  • 9
  • I don't think you will be able to send email from your user's account because it sounds so wrong. Like had it been true, any "bad" site can implement it for purpose like spamming that too in the name of their user. What sounds right is that you as site owner should be able to send mails to your users, not other way around. – aquaman Jan 05 '19 at 15:15
  • @aquaman I was making a site for a company so the clients can interact with them directly through their website and many IT companies have implemented it in their site, and I'll try to make it secure once the email works. – Aayush Dhakal Jan 05 '19 at 15:32
  • please checkout this [link](https://stackoverflow.com/questions/20293986/how-do-i-send-email-from-a-users-email-address-with-django). – aquaman Jan 05 '19 at 15:37
  • What i suggest is that you take your user's email and then send mail from your account to your destination on their behalf. Since you have control over your destination ie. your client, that should not be a problem. – aquaman Jan 05 '19 at 15:40
  • See my answer below, if you are sending mail from yourself, the issue might be your port settings. – 2ps Jan 05 '19 at 16:36
  • @aquaman Thanks for the link. I've taken your suggestion. – Aayush Dhakal Jan 07 '19 at 08:28

2 Answers2

1

Gmail, like more or less any other email provider or correctly configured mail server, does not allow you to set an arbitrary From address. An email server that allows this would be abused by spammers and quickly find itself on a spam blacklist.

What you can do is set my_email as sender and add a Reply-To header with the email address provided through the form.

You have to create an EmailMessage instance and call send():

from django.core.mail import EmailMessage
...
email = EmailMessage(
    subject=form.cleaned_data['title'],
    message=form.cleaned_data['content'],
    from_email='my_email',
    recipient_list=['my_email'],
    reply_to=form.cleaned_data['contact_email']
)
sent = email.send(fail_silently=False)
if sent:
    return render(request, 'index.html', {'form': form, 'message': 'Message delivered successfully', 'error':'', 'email_sent': 1 })

Note that Gmail has some limits on the number of emails you can send this way. Make sure you don't accidentally block your account.

Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
0

N.B., you can configure gmail to relay messages on behalf of any sending address in a google apps organization (powered by gmail), but as Daniel Hepper noted, this is not the default for a personal gmail address. That said, I would wager that the OP's problem is likely that the port setting is incorrect. In gmail, the port setting should be 465 or 587 for TLS-using SMTP.

2ps
  • 15,099
  • 2
  • 27
  • 47