I have a code using http.client library in python that sends email to others.I tested this in jupyter notebook and am able to send an email.
But when I tried the same inside lambda AWS,I get an error.Below is my code:
import http.client
import mimetypes
def lambda_handler(event, context):
conn = http.client.HTTPSConnection("mail.us-east-1.aws.cloud.xxx")
payload = "{\n \"from\": \"[email protected]\",\n \"to\": \"[email protected]\",\n \"subject\": \"Test mail\",\n \"textbody\": \"Test body\",\n \"htmlbody\": \"<h3>Test body!</h3>\"\n}"
headers = {
'Content-Type': 'application/json'
}
conn.request("POST", "", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
print("done")
Below is my error message:
Response:
{
"errorMessage": "[Errno -2] Name or service not known",
"errorType": "gaierror",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 15, in lambda_handler\n conn.request(\"POST\", \"\", payload, headers)\n",
" File \"/var/lang/lib/python3.8/http/client.py\", line 1230, in request\n self._send_request(method, url, body, headers, encode_chunked)\n",
" File \"/var/lang/lib/python3.8/http/client.py\", line 1276, in _send_request\n self.endheaders(body, encode_chunked=encode_chunked)\n",
" File \"/var/lang/lib/python3.8/http/client.py\", line 1225, in endheaders\n self._send_output(message_body, encode_chunked=encode_chunked)\n",
" File \"/var/lang/lib/python3.8/http/client.py\", line 1004, in _send_output\n self.send(msg)\n",
" File \"/var/lang/lib/python3.8/http/client.py\", line 944, in send\n self.connect()\n",
" File \"/var/lang/lib/python3.8/http/client.py\", line 1392, in connect\n
super().connect()\n",
" File \"/var/lang/lib/python3.8/http/client.py\", line 915, in connect\n self.sock = self._create_connection(\n
" File \"/var/lang/lib/python3.8/socket.py\", line 787, in create_connection\n for res in getaddrinfo(host, port, 0, SOCK_STREAM):\n",
" File \"/var/lang/lib/python3.8/socket.py\", line 918, in getaddrinfo\n for res in _socket.getaddrinfo(host, port, family, type, proto, flags):\n"
]
}
Here the line number 15 points to :
data=res.read()
I am new to lambda and am not sure if I am missing out something.