Sender email address cut short (SMTP python 2.7.9) - python-2.7

I tried sending an email using the SMTP (Python 2.7.9).
The sender I provide is 'kmuthukumar#example.com', however in the receiver's inbox, the sender is cut short into 'ukumar#example.com', which is another existing user in our system.
Any idea what is causing this?
More info:
This issue only happens when emails are generated by code using 'kmuthukumar' as sender. Results are normal when kmuthukumar sends an email through email applications. The original code is working fine for all other users.
import time
from smtplib import SMTP
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
server='internal-smtp.example.com'
sender="kmuthukumar#example.com"
recipients = ['user#example.com']
cc=[]
msg = MIMEMultipart()
msg["From"] = sender
msg["To"]=", ".join(recipients) if isinstance(recipients, list) else recipients
msg["Subject"] = "test"
msg['Date'] = time.ctime(time.time())
subtype='plain'
charset='utf-8'
message = "hello"
text = MIMEText(message, _subtype=subtype, _charset=charset)
msg.attach(text)
smtp = SMTP(server)
smtp.sendmail(sender, list(set(recipients+cc)), msg.as_string())
smtp.quit()

Related

How to schedule an email using twilio sendgrid in django?

I'm currently building an app which contains email sending to multiple users which i'm able to do but i want to add a functionality which schedule's an email, for instance I'm using sent_at method as you can see below:-
settings.py
EMAIL_FROM = 'EMAIL'
EMAIL_API_CLIENT ='XXXXXXXX'
views.py
import json
from sendgrid import SendGridAPIClient
from django.conf import settings
message = Mail(from_email=settings.EMAIL_FROM,
to_emails=selectedphone,
subject=subject,
html_content=editor)
message.extra_headers = {'X-SMTPAPI': json.dumps({'send_at':
FinalScheduleTime})}
sg = SendGridAPIClient(settings.EMAIL_API_CLIENT)
response = sg.send(message)
if response.status_code == 202:
emailstatus = "Accepted"
elif .....
else.....
I've also tried message.extra_headers = {'SendAt':FinalScheduleTime} but it's not working either.
Here the FinalScheduleTime is of the datetime object. The sendgrip api accepts the UNIX timestamp according to the documentation. You can check it here
Hence to convert your datetime object into unix time stamp, you can use the time module of python.
scheduleTime = int(time.mktime(FinalScheduleTime.timetuple()))
Also, replace the message.extra_headers with message.send_at.
Hence, your final code will look like:
import json
import time
from sendgrid import SendGridAPIClient
from django.conf import settings
message = Mail(from_email=settings.EMAIL_FROM,
to_emails=selectedphone,
subject=subject,
html_content=editor)
scheduleTime = int(time.mktime(FinalScheduleTime.timetuple()))
message.send_at = scheduleTime
sg = SendGridAPIClient(settings.EMAIL_API_CLIENT)
response = sg.send(message)
if response.status_code == 202:
emailstatus = "Accepted"
elif .....
else.....
This is an official blog by Twilio on Using Twilio SendGrid To Send Emails from Python Django Applications - https://www.twilio.com/blog/scheduled-emails-python-flask-twilio-sendgrid
Also here are, official docs

Python smtplib email doesn't send to External Addresses

I'm able to automate some emails internally using mime and smtplib, but for some reason, these emails fail to send to external addresses (outside of company's domain)
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import Encoders
import smtplib
SERVER = 'mailrelay'
FROM = 'myemail#internaldomain.com'
TO = ['myemail#internaldomain.com','someemail#externaldomain.com']
body = 'This is a test'
msg = MIMEMultipart()
msg["To"] = ','.join(TO)
msg["From"] = FROM
msg["Subject"] = 'Automated Test Email'
msgText = MIMEText(body, 'html')
msg.attach(msgText)
message = msg.as_string()
server = smtplib.SMTP(SERVER)
server.sendmail(FROM,TO,message)
server.quit()
Produces this error:
SMTPRecipientsRefused: {'someemail#externaldomain.com': (550, '5.7.1 Unable to relay')}
Admin insists that relaying is enabled, and told me that sending emails to external domains using Powershell works, so it can't be a relay issue.
So now I'm stuck. If it's not the problem python is telling me it is, is my admin wrong or is something else going on?
Any ideas?
Looks like I just needed to authenticate in this case:
server.ehlo()
server.starttls()
server.ehlo
server.login('user', 'pass')

Is it possible to save the sent email into the sent items folder using python?

I want to sends an email but sent mail is empty.
how to send an email, and then put a copy of it in the "Sent" mail folder.
what can i do?
Yes, its possible.
Basicaly you need to create an MIME email and then send it throug smptlib and than save it on Sent with imaplib.
The official imaplib documentation.
More detailed examples of using imaplib.
Here is an example:
import time
import ssl
import imaplib
import smtplib
import email
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class Mail:
def __init__(self):
# considering the same user and pass for smtp an imap
self.mail_user = 'youruser#yourdomain.com'
self.mail_pass = 'pass'
self.mail_host = 'mail.yourdomain'
def send_email(self, to, subject, body, path, attach):
message = MIMEMultipart()
message["From"] = self.mail_user
message["To"] = to
message["Subject"] = subject
message.attach(MIMEText(body, "plain"))
with open(path + attach, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
"attachment; filename= \"" + attach + "\"",
)
message.attach(part)
text = message.as_string()
context = ssl.create_default_context()
with smtplib.SMTP_SSL(self.mail_host, 465, context=context) as server:
result = server.login(self.mail_user, self.mail_pass)
server.sendmail(self.mail_user, to, text)
imap = imaplib.IMAP4_SSL(self.mail_host, 993)
imap.login(self.mail_user, self.mail_pass)
imap.append('INBOX.Sent', '\\Seen', imaplib.Time2Internaldate(time.time()), text.encode('utf8'))
imap.logout()
if __name__ == '__main__':
m = Mail()
m.send_email('someone#somewhere.com', 'Hello', 'Its just a test!', 'c:\\', 'test.pdf')

Email Notification for test results by using Python in Robot Framework

We are using ROBOT framework to execute automation test cases.
Could anyone please guide me to write script for e-mail notification of test results.
Note:
I have e-mail server details.
Regards,
-kranti
You can create a custom library for send email.
More details in official documentation
I did something similar, I based on this article to create the library.
an example of the function in python file:
import smtplib
from io import StringIO
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os
def send_mail_no_attachment(server, from_user, from_password, to, subject, text):
msg = MIMEMultipart()
msg['From'] = from_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
mailServer = smtplib.SMTP(server)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(from_user, from_password)
mailServer.sendmail(from_user, to, msg.as_string())
mailServer.close()
call the function in robot file:
*** Test Cases ***
example mail
send mail no attachment ${SMTP_SERVER} ${USER} ${PASS} ${mail} ${subject} ${text}
If you are not very expert with the robot framework, define only the function, do not define the class, and you can call this function within your algorithm as a keyword.
I use smtplib and MIMEText
import smtplib
from email.mime.text import MIMEText
class EmailClient():
def __init__(self, my_address):
self.my_address = my_address
def send(self, message, subject, user, email):
header = "Hello " + str(user) + ",\n\n"
footer = "\n\n-Your Boss"
msg = MIMEText(header + message + footer)
msg['Subject'] = subject
msg['From'] = self.my_address
msg['To'] = email
s = smtplib.SMTP('localhost')
s.sendmail(self.my_address, [email], msg.as_string())
s.quit()
EClient = EmailClient("MyEmail#University.edu")
EClient.send("This is a test Email", "Test Subject", "John Doe", "jdoe#University.edu")
you can use jenkins to run your Robot Framework Testcases. There is a auto-generated mail option in jenkins to send mail with Test results.
I have similar requirement so using python file I have achieved requirement (need to execute .py file after robot execution)
Project link and Readme.md
How to send email after test execution
Copy robotemail.py to file
Create a bat file (which executes robot command and .py file in sequence)
robot test.robot &&
python robotemail.py
Execute bat file
Email will be sent to recipients

Get all the mail with body using imap in python

I am new to python and want to get all the mails with the body in the gmail account , but i am able to get only the oldest email.
My code is given below , thanks for help in advance.
#!/usr/bin/env python
import getpass
import imaplib
import email
from email.parser import HeaderParser
M = imaplib.IMAP4_SSL("imap.gmail.com", 993)
add = raw_input("Email address: ")
password = getpass.getpass()
M.login(add, password)
M.select()
resp, data = M.FETCH(1, '(RFC822)')
mail = email.message_from_string(data[0][1])
for part in mail.walk():
# multipart are just containers, so we skip them
if part.get_content_maintype() == 'multipart':
continue
# we are interested only in the simple text messages
if part.get_content_subtype() != 'plain':
continue
payload = part.get_payload()
print payload
i prefer to use uids for iterating, because they are unique, so that's my code:
imap.select('INBOX',False)
typ, ids = imap.uid('search',None,'ALL')
ids = ids[0].decode().split()
for id in ids:
typ, messageRaw = imap.uid('fetch',id,'(RFC822)')
message = email.message_from_bytes(messageRaw[0][1])
for part in message.walk():
You can try high level library - https://github.com/ikvk/imap_tools
# get list of email body from INBOX folder
with MailBox('imap.mail.com').login('test#mail.com', 'password') as mailbox:
body_set = [msg.body for msg in mailbox.fetch()]