Outgoing Email Issue

While sending mail getting error.

Traceback (most recent call last):
File “/home/vivek/ERPNEXT_LOCAL/frappe-bench/apps/frappe/frappe/email/queue.py”, line 443, in send_one
smtpserver.sess.sendmail(email.sender, recipient.recipient, encode(message))
File “/home/vivek/ERPNEXT_LOCAL/frappe-bench/apps/frappe/frappe/email/smtp.py”, line 282, in sess
raise frappe.OutgoingEmailError(ret[1])
frappe.exceptions.OutgoingEmailError: b’5.5.1 MAIL first. j22sm18718214pji.16 - gsmtp’

Where and how did you specify what 3rd party service will handle outgoing emails?

Thanks for reply @MartinHBramwell,

I am trying to send email through our company email id which dont have access of third party apps.
And problem is i cant enable the third party access.
So i was trying to set token based access.
But i really stuck how to do.

If you know please help me.

Sir any suggestion on this?

I am totally stuck how to send email using private email id which dont have access to enable third party access.
Sir i tried using normal python code to send email using private email…its working fine using following code.


import base64
import imaplib
import json
import smtplib
import urllib.parse
import urllib.request
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import lxml.html

GOOGLE_ACCOUNTS_BASE_URL = ‘https://accounts.google.com
REDIRECT_URI = ‘urn:ietf:wg:oauth:2.0:oob’

GOOGLE_CLIENT_ID = ‘1012971819541-ufm8kc9hgmio223krjppmiv8lsc7t5vl.apps.googleusercontent.com
GOOGLE_REFRESH_TOKEN = ‘1//0gyNiODB_NrHqCgYIARAAGBASNwF-L9Ir52nse8zlNmmXDQc6otohl07i3-Qt0HiZZo5sYFRYTXDMyQpmZrA7fHU6PCJikb1Qd0k’

‘USER’: ‘*@.in’,
‘PASS’: ‘********’,
‘CLIENT_ID’: ‘1012971819541-ufm8kc9hgmio223krjppmiv8lsc7t5vl.apps.googleusercontent.com’,
‘CLIENT_SECRET’: ‘C9hOXwCDMvVEmwLyg0Zjjl1s’,
‘REFRESH_TOKEN’: ‘1//0gyNiODB_NrHqCgYIARAAGBASNwF-L9Ir52nse8zlNmmXDQc6otohl07i3-Qt0HiZZo5sYFRYTXDMyQpmZrA7fHU6PCJikb1Qd0k’,

def command_to_url(command):
return ‘%s/%s’ % (GOOGLE_ACCOUNTS_BASE_URL, command)

def url_escape(text):
return urllib.parse.quote(text, safe=’~-._’)

def url_unescape(text):
return urllib.parse.unquote(text)

def url_format_params(params):
param_fragments = []
for param in sorted(params.items(), key=lambda x: x[0]):
param_fragments.append(’%s=%s’ % (param[0], url_escape(param[1])))
return ‘&’.join(param_fragments)

def call_refresh_token(client_id, client_secret, refresh_token):
params = {}
params[‘client_id’] = client_id
params[‘client_secret’] = client_secret
params[‘refresh_token’] = refresh_token
params[‘grant_type’] = ‘refresh_token’
request_url = command_to_url(‘o/oauth2/token’)
response = urllib.request.urlopen(request_url, urllib.parse.urlencode(params).encode(‘UTF-8’)).read().decode(‘UTF-8’)
return json.loads(response)

def generate_oauth2_string(username, access_token, as_base64=False):
auth_string = ‘user=%s\1auth=Bearer %s\1\1’ % (username, access_token)
if as_base64:
auth_string = base64.b64encode(auth_string.encode(‘ascii’)).decode(‘ascii’)
return auth_string

def refresh_authorization(google_client_id, google_client_secret, refresh_token):
response = call_refresh_token(google_client_id, google_client_secret, refresh_token)
return response[‘access_token’], response[‘expires_in’]

def send_mail(fromaddr, toaddr, subject, message):
access_token, expires_in = refresh_authorization(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_REFRESH_TOKEN)
auth_string = generate_oauth2_string(fromaddr, access_token, as_base64=True)

 msg = MIMEMultipart('related')
 msg['Subject'] = subject
 msg['From'] = fromaddr
 msg['To'] = toaddr
 msg.preamble = 'This is a multi-part message in MIME format.'
 msg_alternative = MIMEMultipart('alternative')
 part_text = MIMEText(lxml.html.fromstring(message).text_content().encode('utf-8'), 'plain', _charset='utf-8')
 part_html = MIMEText(message.encode('utf-8'), 'html', _charset='utf-8')
 server = smtplib.SMTP('smtp.gmail.com:587')
 server.docmd('AUTH', 'XOAUTH2 ' + auth_string)
 server.sendmail(fromaddr, toaddr, msg.as_string())

if name == ‘main’:
print(‘No refresh token found, obtaining one’)
refresh_token, access_token, expires_in = get_authorization(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)
print(‘Set the following as your GOOGLE_REFRESH_TOKEN:’, refresh_token)

 send_mail('****@***.in', '****@***.in', 'A mail from you from Python', '<bA mail from you from Python</b<br<br' + 'So happy to hear from you!')


but i am not able to achieve how to do in erpnext. Please help me with your valuable suggestion

I see from your code dump that you are using a “3rd party service”. It is 'smtp.gmail.com:587

Do your Email Domain settings look anything like the following?

I have used pop.gmail.com instead of imap.gmail.com.


That’s great. Glad I was able to help.

problem is not solved.
Should i use imap?

imap works for me. pop is very old. I doubt Google even allows it any more for support and security reasons.