i am trying to send email with attachment using senditblue api, Code works fine from local machine.
It gives error when triggered from pythonanywhere Task.
code sample below :
configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key'] = 'API Key'
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
subject = "This weeks Volume Scanner"
html_content = "<html><body><h1>Find attached weekly volume breakout scripts</h1></body></html>"
sender = {"name":"Rutvij","email":"myemail@gmail.com"}
to = [{"email":"abc@gmail.com","name":"abc"},{"email":"pqr@gmail.com","name":"pqr"}]
reply_to = {"email":"myemail@gmail.com","name":"my email"}
headers = {"Some-Custom-Name":"unique-id-1234"}
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(THIS_FOLDER,'HigherTradeQuantityThisWeek.csv')
data = open(filename, "r").read().encode('utf-8')
attachment = [{"content":base64.b64encode(data).decode('utf-8'), "name":"HigherTradeQuantityThisWeek.csv"}]
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(to=to,reply_to=reply_to, headers=headers, html_content=html_content, sender=sender, subject=subject,attachment=attachment)
try:
api_response = api_instance.send_transac_email(send_smtp_email)
pprint(api_response)
except ApiException as e:
print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)
Error details below :
2021-10-23 06:40:29,564 WARNING Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f947f768940>: Failed to establish a new connection: [Errno 101] Network is unreachable')': /v3/smtp/email
2021-10-23 06:40:31,612 WARNING Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f947f768d60>: Failed to establish a new connection: [Errno 101] Network is unreachable')': /v3/smtp/email
2021-10-23 06:40:33,660 WARNING Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f947f768ee0>: Failed to establish a new connection: [Errno 101] Network is unreachable')': /v3/smtp/email
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/urllib3/connection.py", line 169, in _new_conn
conn = connection.create_connection(
File "/usr/local/lib/python3.8/dist-packages/urllib3/util/connection.py", line 96, in create_connection
raise err
File "/usr/local/lib/python3.8/dist-packages/urllib3/util/connection.py", line 86, in create_connection
sock.connect(sa)
OSError: [Errno 101] Network is unreachable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/urllib3/connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "/usr/local/lib/python3.8/dist-packages/urllib3/connectionpool.py", line 382, in _make_request
self._validate_conn(conn)
File "/usr/local/lib/python3.8/dist-packages/urllib3/connectionpool.py", line 1010, in _validate_conn
conn.connect()
File "/usr/local/lib/python3.8/dist-packages/urllib3/connection.py", line 353, in connect
conn = self._new_conn()
File "/usr/local/lib/python3.8/dist-packages/urllib3/connection.py", line 181, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7f947f7490a0>: Failed to establish a new connection: [Errno 101] Network is unreachable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/rutvij/VolumeScanner/WeeklyVolumeScanner.py", line 312, in <module>
EmailFile()
File "/home/rutvij/VolumeScanner/WeeklyVolumeScanner.py", line 296, in EmailFile
api_response = api_instance.send_transac_email(send_smtp_email)
File "/home/rutvij/.local/lib/python3.8/site-packages/sib_api_v3_sdk/api/transactional_emails_api.py", line 1735, in send_transac_email
(data) = self.send_transac_email_with_http_info(send_smtp_email, **kwargs) # noqa: E501
File "/home/rutvij/.local/lib/python3.8/site-packages/sib_api_v3_sdk/api/transactional_emails_api.py", line 1798, in send_transac_email_with_http_info
return self.api_client.call_api(
File "/home/rutvij/.local/lib/python3.8/site-packages/sib_api_v3_sdk/api_client.py", line 329, in call_api
return self.__call_api(resource_path, method,
File "/home/rutvij/.local/lib/python3.8/site-packages/sib_api_v3_sdk/api_client.py", line 161, in __call_api
response_data = self.request(
File "/home/rutvij/.local/lib/python3.8/site-packages/sib_api_v3_sdk/api_client.py", line 371, in request
return self.rest_client.POST(url,
File "/home/rutvij/.local/lib/python3.8/site-packages/sib_api_v3_sdk/rest.py", line 269, in POST
return self.request("POST", url,
File "/home/rutvij/.local/lib/python3.8/site-packages/sib_api_v3_sdk/rest.py", line 162, in request
r = self.pool_manager.request(
File "/usr/local/lib/python3.8/dist-packages/urllib3/request.py", line 78, in request
return self.request_encode_body(
File "/usr/local/lib/python3.8/dist-packages/urllib3/request.py", line 170, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/usr/local/lib/python3.8/dist-packages/urllib3/poolmanager.py", line 375, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/local/lib/python3.8/dist-packages/urllib3/connectionpool.py", line 783, in urlopen
return self.urlopen(
File "/usr/local/lib/python3.8/dist-packages/urllib3/connectionpool.py", line 783, in urlopen
return self.urlopen(
File "/usr/local/lib/python3.8/dist-packages/urllib3/connectionpool.py", line 783, in urlopen
return self.urlopen(
File "/usr/local/lib/python3.8/dist-packages/urllib3/connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "/usr/local/lib/python3.8/dist-packages/urllib3/util/retry.py", line 573, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.sendinblue.com', port=443): Max retries exceeded with url: /v3/smtp/email (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f947f7490a0>: Failed to establish a new connection: [Errno 101] Network is unreachable'))
2021-10-23 06:40:36 -- Completed task, took 28.88 seconds, return code was 1.