send email to localhost from localhost via python - python

I'm building and testing a web service on my local machine before i put it in production. I want to test the mail service. I'm using the standard python email and smtplib libraries.
import smtplib
from email.mime.text import MIMEText
fp = open('textfile', 'rb')
msg = MIMEText(fp.read())
fp.close()
me = 'me_email#localhost'
you = 'me_again_email#localhost'
msg['Subject'] = 'The contents of %s' %fp
msg['From'] = me
msg['To'] = you
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
I have not configured sendmail and hence it throws an error. But since I just want to test my web service I am not concerned if sendmail cant send an email right now. My service is designed to pulls some records off db and send them an email. So i want to know if this connection, python taking inputs from db and pushing an email is working. I want to receive the email on localhost sent via the script.

An SMTP server needs to be configured for sending emails. Sending emails is not possible unless you configure an SMTP server. More information on using python smtplib can be found in pymotw.com, tutorialspoint.com and Python docs.

Related

using docmd to receive queue-id of a mail from smtp server

I have written a code in jython 2.5, which uses the smtplib sendmail to send mail over a smtp server. Below is a simple snippet of the code
mail = MIMEMultipart('mixed')
mail['Subject'] = mail_subject
mail['FROM']=UstrSender
mail['To']=UstrReceivers
mail['Cc']=UstrCC
mail_p2=MIMEText(mail_html, 'html', 'utf-8')
mail.attach(mail_p2)
#Connection to SMTP
#Enter SMTP Server Details, In case your server do require authentication modify authentication parameter below and uncomment
s = smtplib.SMTP(smtpserver)
#s.sendmail(UstrSender, [UstrReceivers, UstrCC], mail.as_string())
sendmail_return = s.sendmail(UstrSender, [UstrReceivers, UstrCC], mail.as_string())
Now, the smtp server mentioned is a cluster of 3 individual servers and there is a lag at times on one of these servers because of a long queue of requests. To identify such issues and the culprit server, need to have a generic script to identify the queue id of the message being sent. Kindly, help on the same, if using docmd we can create such a command to get the queue id of the submitted email.
I tried getting a response from sendmail itself, but since the mail is eventually sent, there is no return from the command.
Thanks,
Dev

sendgrid - how to specify recipient SMTP server?

I am trying to send emails to a private SMTP server and can do so using python smtplib as follows
s = smtplib.SMTP(server, port)
s.sendmail(me, you, msg.as_string())
Trying to convert the same to Sendgrid (as Azure blocks SMTP) but it has no option to specify a private SMTP server.
import sendgrid
import os
from sendgrid.helpers.mail import *
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test#example.com")
to_email = Email("test#example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
Any help would be much appreciated.
Thanks
That's quite normal, the python package for sendgrid does not use the smtp protocol, but actually request an API which use classical HTTPS requests...
So, except if your smtp server can be accessed over an API (custom-made?), there's no chance you can use the sendgrid package.
If you want to customise the python package for sendgrid in order to point toward your own API endpoint which will act as relay for your smtp requests, you are free to modify its source code: https://github.com/sendgrid/sendgrid-python
Alternatively you can use port 2525 since all major smtp vendors support it and its not blocked by Azure as far as I know. But if your smtp server is something not from a major vendor and you do not have access to it to change/add ports you have no option but to go to a service other than Azure for hosting which might be hard since all major vendors now block 25, 465, 587.

Using Python I can't send email using gmail smtp server from within my Corporate LAN. Why

I have searched many python email related post here but I seem not able to send a single email using python. I am working within corporate LAN. But If I use my company's smtp relay server I am able to send email to gmail, or my company ID or other emails.This is very strange or it is suppose to be this way? How can I use gmail smtp server to send email outside?
Here are few scripts I have tried.
====================================1==
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("xyz#gmail.com", "myPasswd")
msg = "YOUR MESSAGE!"
server.sendmail("xyz#gmail.com", "xyz#gmail.com", msg)
server.quit()
===================================2==
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
msg = MIMEMultipart()
msg['From'] = 'sender#gmail.com'
msg['To'] = 'recvr#gmail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('sender#gmail.com', 'pswd')
mailserver.sendmail('sender#gmail.com','rcvr#gmail.com',msg.as_string())
mailserver.quit()
======================================
I am sure it is nothing to do with code but something else I am missing.
I had the same problem
It seems that google find your application is insecure. You can:
Allow less secure applications to connect to the server in google apps settings. see Allowing less secure apps to access your account
Secure your code [whatever it means by google ...]

How to send an email using Python

I made a simple keylogger in C++. I want to send an email with the input the keylogger made using Python. I know you can do it with C++ but I want to use Python.
You I hope that your keylogger is for educational purposes only. Anyway,
here is a simple way to send one e-mail using Python script.
#!/usr/bin/python
import smtplib
sender = 'from#fromdomain.com'
receivers = ['to#todomain.com']
message = """From: From Person <from#fromdomain.com>
To: To Person <to#todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Here, you have placed a basic e-mail in message, using a triple quote, taking care to format the headers correctly. An e-mail requires a From, To, and Subject header, separated from the body of the e-mail with a blank line.
To send the mail you use smtpObj to connect to the SMTP server on the local machine and then use the sendmail method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these aren't always used to route mail).
If you're not running an SMTP server on your local machine, you can use smtplib client to communicate with a remote SMTP server. Unless you're using a webmail service (such as Hotmail or Yahoo! Mail), your e-mail provider will have provided you with outgoing mail server details that you can supply them, as follows:
smtplib.SMTP('mail.your-domain.com', 25)
Source: http://www.tutorialspoint.com/python/python_sending_email.htm

Using proxy on Python SMTP

I want to try hiding my main IP and send email using Hotmail SMTP via Python SMTP Lib. I'm using the following code to send email but its sent from my IP (Originally it sends from Hotmail SMTP but it still shows my IP in the header if can see).
import smtplib
SMTP = "smtp.live.com"
s = smtplib.SMTP(SMTP,587)
s.ehlo()
s.starttls()
s.ehlo()
s.login("username#hotmail.com", "Password")
s.sendmail("username#hotmail.com", "recipient#hotmail.com","Hello World")
Let me know if there's any thing possible from which I can hide my IP and use the proxy IP to send the email. Furthermore, I think playing with socket/socks library might do the trick but still unsure.
Thank you very much.

Categories