I am trying to send an email to a specific recipient using python, but my code keeps setting the recipient as a BCC. How do I change this to set it as a normal To?
Thanks!
Here is my code
import smtplib
def send_email(subject, msg):
login = "My_email"
password = "password"
reciever = "recipient#gmail.com"
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(login, password)
message = 'Subject: {}\n\n{}'.format(subject, msg)
server.sendmail(login, reciever, message)
server.quit()
print("Success: Email sent!")
except:
print("Email failed to send.")
subject = "Test email"
Sensor_Message = "This has worked"
send_email(subject, Sensor_Message)
I don't know python, but here is what I think the problem is: your message does not contain a To: or Cc: email header containing the recipient's email address.
Because the recipient's email address is in the list of RCPT TO commands sent over SMTP, but not in the email headers, the recipient's MUA (e.g. Gmail, Outlook, Thunderbird, etc.) treats it as a Bcc.
To fix this problem, make sure that the message contains a To: or Cc: header for the recipient.
Unrelated nitpick: the correct spelling is "receiver"
Good luck!
Related
I was working on my discord bot trying to implement a email feature where you imbed a file and then the discord bot downloads it and sends it back out to the server. I came across the issue where I have no idea how I would begin to save the file. You can find my code at https://github.com/Omar-Alabdalla/DiscordBot. the specific files that have the emailing feature are mailFunctions(discord commands part) and basicMail(email commands part).
I looked through the docs of nextcord.py and couldn't find any simple way that I could understand. I probably just missed what I was supposed to find though.
discord command Code:
#commands.command()
async def mailFile(self, ctx, *stuff):
# received if else statement from stackoverflow: https://stackoverflow.com/questions/65169339/download-csv-file-sent-by-user-discord-py
if str(ctx.attachments) == "[]": # This checks if there is an attachment on the message
return "You didn't include a file"
else:
await save("mailFile
The mailing class code:
def sendFileMail(rmail, message):
mail_content = '''Hello,
This is a test mail.
In this mail we are sending some attachments.
The mail is sent using Python SMTP library.
Thank You
'''
# Setup the MIME
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = rmail
message['Subject'] = 'A test mail sent by Python. It has an attachment.'
# The subject line
# The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'TP_python_prev.pdf'
attach_file = open(attach_file_name, 'rb') # Open the file as binary mode
payload = MIMEBase('application', 'octate-stream')
payload.set_payload(attach_file.read())
encoders.encode_base64(payload) # encode the attachment
# add payload header with filename
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)
# Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) # use gmail with port
session.starttls() # enable security
session.login(sender_email, password) # login with mail_id and password
text = message.as_string()
session.sendmail(sender_email, rmail, text)
session.quit()
print('Mail Sent')
Apologies for not including code prior First time posting on stack overflow
See Attachment.save: https://nextcord.readthedocs.io/en/latest/api.html?highlight=attachment#nextcord.Attachment.save
for attachment in ctx.message.attachments:
await attachment.save(attachment.filename)
I want to send an email without login to server in Python. I am using Python 3.6.
I tried some code but received an error. Here is my Code :
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='from#Address.com'
toAddr='to#Address.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer)
server.set_debuglevel(1)
server.sendmail(fromAddr, toAddr, text)
server.quit()
I expect the mail should be sent without asking user id and password but getting an error :
"smtplib.SMTPSenderRefused: (530, b'5.7.1 Client was not authenticated', 'from#Address.com')"
I am using like this. It's work to me in my private SMTP server.
import smtplib
host = "server.smtp.com"
server = smtplib.SMTP(host)
FROM = "testpython#test.com"
TO = "bla#test.com"
MSG = "Subject: Test email python\n\nBody of your message!"
server.sendmail(FROM, TO, MSG)
server.quit()
print ("Email Send")
import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
mail.To='To address'
mail.Subject='Message subject'
mail.Body='Message body'
mail.HTMLBody='<h2>HTML Message body</h2>' #this field is optional
# To attach a file to the email (optional):
attachment="Path to the attachment"
mail.Attachments.Add(attachment)
mail.Send()
The code below worked for me.
First, I opened/enabled Port 25 through Network Team and used it in the program.
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='from#Address.com'
toAddr='to#Address.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer,25)
server.ehlo()
server.starttls()
server.sendmail(fromAddr, toAddr, text)
server.quit()
First, you have to have a SMTP server to send an email. When you don't have one, usually outlook's server is used. But outlook only accepts authenticated users, so if you don't want to login into the server, you have to pick a server that doesn't need authentication.
A second approach is to setup an internal SMTP server. After you setup the internal SMTP server, you can use the "localhost" as the server to send the email. Like this:
import smtplib
receiver = 'someonesEmail#hisDomain.com'
sender = 'yourEmail#yourDomain.com'
smtp = smtplib.SMTP('localhost')
subject = 'test'
body = 'testing plain text message'
msg = 'subject: ' + subject + ' \n\n' + body
smtp.sendmail('sender', receiver, msg)
I'm creating an app that automatically sends emails to different users of a Google Group, where the email sender is the Google Group address.
The app is written in Python and is using Mandrill to send the emails. The distribution of the emails is working properly, but I need to sender email to be the Google Group. I have it setup as an alias on my Gmail, which allows me to manually select the alias and send emails from the Google Groups address. I am looking for a way to automatically send the emails from the alias without me having to manually send it from Gmail.
Here is code example how to send email via SMTP by using python. You can configure "From" field so it will be used as sender. Please pay attention there are python libraries being used: smtplib, os and email.
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart('alternative')
msg['Subject'] = "Hello from Mandrill, Python style!"
msg['From'] = "John Doe <john#doe.com>" # Your from name and email address
msg['To'] = "recipient#example.com"
text = "Mandrill speaks plaintext"
part1 = MIMEText(text, 'plain')
html = "<em>Mandrill speaks <strong>HTML</strong></em>"
part2 = MIMEText(html, 'html')
username = os.environ['MANDRILL_USERNAME']
password = os.environ['MANDRILL_PASSWORD']
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP('smtp.mandrillapp.com', 587)
s.login(username, password)
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()
For more information check this link How to Send via SMTP with Popular Programming Languages?
You can try using the Users.settings.sendAs resource.
Settings associated with a send-as alias, which can be either the
primary login address associated with the account or a custom "from"
address. Send-as aliases correspond to the "Send Mail As" feature in
the web interface.
{
"sendAsEmail": string,
"displayName": string,
"replyToAddress": string,
"signature": string,
"isPrimary": boolean,
"isDefault": boolean,
"treatAsAlias": boolean,
"smtpMsa": {
"host": string,
"port": integer,
"username": string,
"password": string,
"securityMode": string
},
"verificationStatus": string
}
The sendAsEmail property of this resource stands for the email address that appears in the "From:" header for mail sent using this alias. This is read-only for all operations except create.
For other information about managing aliases, you can check out this documentation.
I know there are a lots of how to send mail questions before, but does sending mail from python scripts not work anymore? I've tried to use the SMTP lib with the following script:
import smtplib
fromaddr = 'mymail#gmail.com'
toaddr = 'someones#gmail.com'
msg = "\r\n".join([
"From: mymail#gmail.com",
"To: someonesmail#gmail.com",
"Subject: Just a message",
"",
'Why, oh why?'
])
username = 'mymail#gmail.com'
pwd = 'mypassword'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username, pwd)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
But it says to login via a web browser, and this post says that google now uses an api to send mails. So, having confusion about the possibility to send mails via scripts without using any of google's api, Is it possible?
I am not able to send the mail without password authentication in python.Its printing the last message but mail is not sent.Below is the code that i am using.Anyone please help i am stuck because of this. Thanks in advance.
import smtplib
fromaddr = 'xyz#gmail.com'
toaddrs = ['abc#gmail.com']
msg = '''
From: {fromaddr}
To: {toaddr}
Subject: 'testing'
This is a test
.
'''
msg = msg.format(fromaddr =fromaddr, toaddr = toaddrs[0])
server = smtplib.SMTP("gmail-smtp-in.l.google.com:25")
server.starttls()
server.ehlo("example.com")
server.mail(fromaddr)
server.rcpt(toaddrs[0])
server.data(msg)
server.quit/close()
print("mail sent")