In my settings.py i have a mail congiguration Like :
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = os.getenv('EMAIL_HOST')
EMAIL_PORT = os.getenv('EMAIL_PORT') //465
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS')
And in my code i am using like :
connection = get_connection(
host=settings.EMAIL_HOST,
port=settings.EMAIL_PORT,
username=settings.EMAIL_HOST_USER,
password=settings.EMAIL_HOST_PASSWORD,
use_tls=settings.EMAIL_USE_TLS,
)
print('11111111')
print(connection)
print('222222222')
mail = send_mail('diditwork?', 'test message', settings.EMAIL_HOST_USER, [userObj.email], connection=connection)
But in Result i am getting the error Like :
File "/var/www/html/gelmeko/myenv/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 125, in _send
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "/usr/lib/python3.6/smtplib.py", line 867, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (501, b'Invalid MAIL FROM address provided', 'AKI**************')
Can any one please help me related this ?? what i am doing wrong here
i am sending the mail through AWS SES credentials.
Try :-
Use EMAIL_PORT = 587` instead of `465`
Related
I made a contact form, I tested with the mailtrap service and it works I receive the messages well.
But when I put the smpt parameters for a real mail account I have this error message
SMTPRecipientsRefused at /contact/
{'info#mysite.net': (550, b'relay not permitted: you must be authenticated to send messages')}
the smtp server and the mail account is on the host alwasdata.net, but I tested with an outloock account it's the same thing always this same error. it seems to come from the line in the contact method:
message,
'info#mysite.net',
['info#othersite.net'],
fail_silently=False,
in settings.py i have this config
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = "info#mysite.net"
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST = 'smtp-blablabla.net'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
in a views.py
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
subject = "Message d'un visiteur sur votre site"
body = {
'Nom': form.cleaned_data['first_name'],
'Tel': form.cleaned_data['tel'],
'Email': form.cleaned_data['email'],
'Message':form.cleaned_data['message'],
}
message = "\n".join(body.values())
try:
send_mail(
subject,
message,
'info#mysite.net',
['info#othersite.net'],
fail_silently=False,
)
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('/')
form = ContactForm()
return render(request,'pages/email_form.html',{'form':form})
the forms.py
from django import forms
class ContactForm(forms.Form):
first_name = forms.CharField(max_length=100, required=True)
tel = forms.CharField(max_length=15, required=True)
email = forms.EmailField(required=False)
message = forms.CharField(widget=forms.Textarea, required=True)
I changed a few parameters:
I put all the email addresses on the servers where my site is hosted,
try:
send_mail(
subject,
message,
'info#mysite.net',
['info#mysite.net'],
fail_silently=False,
)
and at my host I changed the parameters of the mail server so that it redirects to the email address that I was targeting. Now it's work
I tried sending email using python and put attachment, but when it sent the attachment became noname without any extension.
message = MIMEMultipart()
message ['From'] = sender_email
message ['To'] = ', '.join(receiver_email)
message ['Subject'] = 'Python Email'
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'Final Project - Basic Python.pdf'
attach_file = open('Final Project - Basic Python.pdf', 'rb')
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload)
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)
I have been struggling to solve this for the last 3 hours but couldn't find it. My problem is that Django is not sending email and even if I enter the wrong username and password it shows successful .It never sends mail. Please tell me what I am doing wrong.Here is my settings.py file
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '******************'
EMAIL_HOST_PASSWORD = '********'
My urls.py file:
path('reset_password_complete',auth_views.PasswordResetCompleteView.as_view(),name='password_reset_complete'), path('reset/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'), path('reset_password_sent/',auth_views.PasswordResetDoneView.as_view(),name='password_reset_done'), path('reset_password/',auth_views.PasswordResetView.as_view(),name="reset_password"),
I am using the default view of Django but even if my password is wrong or username is wrong it shows me email has been sent but it has not.Please help me.
Make sure you have followed the instructions mentioned in the following two answers:
Django sending email with google SMTP
Sending emails on Django using SMTP and Gmail
If you are still facing problems and you're on a tight timeline, I would recommend using SendGrid's free plan and then add this to your settings.py file:
# settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
SENDGRID_API_KEY = 'YourAPIKey'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = 'YourHostPassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'example#mai.com' # the email that you used on SendGrid
Can somebody help me?
i want to attach a excel file for an email sender in python but gives everytime an error. I looked online and everywhere it said it like it is in my code. can somebody tell me whats wrong
thank you verry much
message = MIMEMultipart()
message['Subject'] = 'Maandelijks rapport'
message['To'] = receiver_email
message['From'] = sender_email
message.attach(MIMEText('In de bijlage vind je het maandrapport terug!'))
part = MIMEBase('application', "octet-stream")
part.set_payload(open('test.xls', "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Desposition', 'attachment; filename="test.xls"')
message.attach(part)
#send the email
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
smtp.login(sender_email, password)
print('connecting with smtp server...')
smtp.login(sender_email, password)
smtp.sendmail(sender_email, receiver_email, message.as_string())
print('connection succeed!')
print('email has been sent!')
smtp.quit()
I'm sending a mail using a python script on a development server which don't have internet connection but i check the connectivity with the SMTP server by pinging the server and it is connected. But i'm getting error (11004, getaddrinfo failed) while connecting to it using smtplib.
Please help me in this issue!
Below is the code im using
msg = MIMEMultipart()
msg["From"] = "sender"
msg["To"] = 'recipient'
msg["Subject"] = "Testing Email"
message = 'Hi,\n \nThis is testing mail.'
ctype, encoding = mimetypes.guess_type(file)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
fp = open(file, "rb")
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename='report.xlsx')
msg.attach(MIMEText(message, 'plain'))
msg.attach(attachment)
try:
server = smtplib.SMTP(server_name)
server.set_debuglevel(0)
server.sendmail(sender, receiver, msg.as_string())
server.quit()
except Exception as e:
print(e.args)
Try this:
server = smtplib.SMTP("smtp.google.com", 587)
server.starttls()
server.login(email, password)
server.sendmail(email, email, message)
server.quit()