I have a site developed in flask framework and it is hosted on a server. It has email sending feature using smtp lib which will send mail to the registered user in a zip format . Problem that i'm facing is that it is working fine in the localhost system , but when i hosted this on server it is giving me 400 request code . I don't know how to slove this problem because it is working totally fine in the localhost system .
Here is code for sending the email:
def send_mail(recipient_mail,filename):
with app.app_context():
if(filename.endswith('.mp3') ) or filename.endswith('.wav'):
temp_dir ="temp_files"
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
audio_srt_path="/home/mechatt.tk/converted/SrtFiles/audio/" + filename[:-4]+".srt"
audio_text_path="/home/mechatt.tk/converted/TxtFiles/" + filename[:-4]+".txt"
file_paths = [audio_srt_path, audio_text_path]
for file_path in file_paths:
file_name = os.path.basename(file_path)
dest_path = os.path.join(temp_dir, file_name)
shutil.copy(file_path, dest_path)
zip_file_path = 'files.zip'
with zipfile.ZipFile(zip_file_path, 'w') as zip_file:
for file_path in file_paths:
file_name = os.path.basename(file_path)
zip_file.write(os.path.join(temp_dir, file_name))
# Attach the zip file to the email message'
message = MIMEMultipart()
message['From'] = Constants.SENDER_EMAIL
message['To'] = recipient_mail
message['Subject'] = 'Zip file containing files'
body = "Please find attached the zip file containing the requested files."
message.attach(MIMEText(body, 'plain'))
with open(zip_file_path, "rb") as zip_file:
part = MIMEBase('application', 'zip')
part.set_payload(zip_file.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(zip_file_path))
message.attach(part)
with smtplib.SMTP(Constants.SMTP_SERVER, Constants.SMTP_PORT) as server:
server.ehlo()
server.starttls()
server.login(Constants.SENDER_EMAIL, Constants.SENDER_PASSWORD)
server.sendmail(Constants.SENDER_EMAIL, recipient_mail, message.as_string())
server.quit()
os.remove(zip_file_path)
shutil.rmtree(temp_dir)
elif filename.endswith(".mp4") or filename.endswith(".avi"):
temp_dir ="temp_files"
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
audio_srt_path="/home/mechatt.tk/converted/SrtFiles/video/" + filename[:-4]+".srt"
audio_text_path="/home/mechatt.tk/converted/TxtFiles/" + filename[:-4]+".txt"
file_paths = [audio_srt_path, audio_text_path]
for file_path in file_paths:
file_name = os.path.basename(file_path)
dest_path = os.path.join(temp_dir, file_name)
shutil.copy(file_path, dest_path)
zip_file_path = 'files.zip'
with zipfile.ZipFile(zip_file_path, 'w') as zip_file:
for file_path in file_paths:
file_name = os.path.basename(file_path)
zip_file.write(os.path.join(temp_dir, file_name))
message = MIMEMultipart()
message['From'] = Constants.SENDER_EMAIL
message['To'] = recipient_mail
message['Subject'] = 'Zip file containing files'
body = "Please find attached the zip file containing the requested files."
message.attach(MIMEText(body, 'plain'))
with open(zip_file_path, "rb") as zip_file:
part = MIMEBase('application', 'zip')
part.set_payload(zip_file.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(zip_file_path))
message.attach(part)
with smtplib.SMTP(Constants.SMTP_SERVER, Constants.SMTP_PORT) as server:
server.ehlo()
server.starttls()
server.login(Constants.SENDER_EMAIL, Constants.SENDER_PASSWORD)
server.sendmail(Constants.SENDER_EMAIL, recipient_mail, message.as_string())
server.quit()
# Remove the temporary directory and the zip file
os.remove(zip_file_path)
shutil.rmtree(temp_dir)
@app.route('/send-as-mail-background', methods=['POST'])
def send_as_email_background():
global filename
recipient_mail=session['Email']
thread =
threading.Thread(target=send_mail(recipient_mail,filename))
thread.start()
return redirect(url_for('home',sent=True))`
Here is SMTP config code: class Constants:
SENDER_EMAIL="singharushi316@gmail.com"
SENDER_PASSWORD="******************"
SMTP_SERVER="smtp.gmail.com"
SMTP_PORT=587`
It is working fine in the localhost , but not in the hosted server . Any suggestion would be a great help for me . Thanks in advance