When using the smtplib library to send emails to phone numbers (ex. number#tmomail.net), my email gets blocked with the message 550 permanent failure for one or more recipients.
I can successfully send emails using the smtplib library to normal emails. I have used my script to send emails to my personal email.
I can also send emails to the phone number successfully using the manual gmail client. If I log into google manually, fill out the form, the email sends to the number perfectly fine.
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(self.email, self.password)
subject = 'test'
body = 'hello world'
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail(self.email, validNumber#carrier.com, msg)
Can someone tell me how to make the text go through unblocked the same way it does when it emails normal gmails?
I believe this issue is specific to T-Mobile numbers. Refer to the answer here:
https://support.t-mobile.com/thread/116618
"T-Mobile is now blocking traffic where the sending Domain is altered from the true sending address: IE support#customerservice.com, when the domain is actually is support#gmail.com. senders will receive a 550 bounceback error . To correct this issue, Senders will need to revert their sending address to their actual email address."
Related
For my use case, I would like to manually set the displayed email addresses in the "to" and "from" field headers of the email, separate from the actual email recipient and sender. I am currently using the smtplib library in python and have managed to accomplish the desired effect with the "to" field and was looking to replicate it for the "from" field as well.
What I have so far:
EMAIL_ADDRESS_G = 'ayush.warikoo77#gmail.com'
from email.message import EmailMessage
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS_G, EMAIL_PASSWORD_G)
# What I would like to be displayed in the email
msg = EmailMessage()
msg["Subject"] = "Test"
msg["To"] = 'test#gmail.com' # shows up
msg['From'] = 'test#gmail.com' # does not show up
msg.set_content("Test body")
# Where I would like to be setting the actual email sender and recipient
smtp.send_message(msg, from_addr=EMAIL_ADDRESS_G, to_addrs=EMAIL_ADDRESS_G)
The above code produces the following:
As shown, the "to" field displays the desired set address, while the "from" field displays my actual email instead of "test#gmail.com". I believe it is being set when I call login with the account, but I am unsure if I can override it. Also happy to use another python email library, if it is not possible with smtplib.
Current --> Desired
To: test#gmail.com
From: ayush.warikoo77#gmail.com --> test#gmail.com
Actual Sender: ayush.warikoo77#gmail.com
Actual Reciever: ayush.warikoo77#gmail.com
Note that this would be used for archiving purposes, where a designated email client might actually be sending the emails, however, I would like the email to use the to and from fields of the message it is trying to document. So the desired displayed "from" field is separate from the actual sender.
Authenticated Gmail SMTP prevents you from spoofing the From header, presumably to prevent abuse.
For archiving purposes, using IMAP’s APPEND command will allow you to place whatever you like in your own mailbox (as it doesn’t count as sending email) and may be a better solution. (You will need to use an App Specific Password or OAUTH to login though).
When I send mail with header like this message['Reply-To'] = '' (Python), it work fine on localhost. When I click Reply in Outlook at that received mail, To field is empty. When I send the same mail from production via company SMTP server, the mail also contains empty Reply-To header, however If I click Reply in Outlook, the address from that the mail had been received is prefilled in To field.
Is there a bug in company SMTP or why does it work only in localhost?
Thank you.
In Reply-To empty, Outlook would default to the sender address. IMHO that is how it is supposed to work.
I have a python script that sends out emails. The emails are being sent out with no issues.
However, when I get the mails and open them up, their "to:" field do not show the recipients. It's just blank. I want to show all the recipients. And since I'm not using any "bcc" configuration in my script, I'm befuddled as to how this could be.
Here's the code I'm using:
#!/usr/bin/env python
you = [ "MyEmail1#MyEmail.com", "MyEmail2#MyEmail.com" ]
for eachrecord in fformatErrMessage:
# Preparing all variables #
msg = MIMEMultipart()
msg['Subject'] = subjectMsg
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()
I have tried replacing the you with:
you = "MyEmail1#MyEmail.com"
thinking maybe the list is the problem - but that did not work.
When sending emails, servers do not look at the To:-Field. Instead, email servers have a separate way of transmitting recipient addresses, sometimes called the "envelope", which they use to route and deliver mail. The client never receives that envelope, it instead uses the To:-field in the message headers.
The sendmail() call simply sets the addresses of the SMTP envelope. If you want the To:-field to show up in the mail itself, you must set the appropriate header:
msg['To'] = "foo#bar.com"
This is, btw, how BCC works: The addresses on the envelope is simply not repeated in the message headers.
This question already has answers here:
How to send an email with Gmail as provider using Python?
(16 answers)
SMTPAuthenticationError 5.7.14 Please log\n5.7.14 in via your web browser
(10 answers)
Closed 7 years ago.
when i try to send mail using gmail and python error occurred this type of question are already in this site but doesn't help to me
gmail_user = "me#gmail.com"
gmail_pwd = "password"
TO = 'friend#gmail.com'
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_user,
'Subject: %s' % SUBJECT,
'', TEXT])
server.sendmail(gmail_user, [TO], BODY)
print ('email sent')
error:
server.login(gmail_user, gmail_pwd)
File "/usr/lib/python3.4/smtplib.py", line 639, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14
<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtl1\n5.7.14 Li2yir27TqbRfvc02CzPqZoCqope_OQbulDzFqL-msIfsxObCTQ7TpWnbxIoAaQoPuL9ge\n5.7.14 BUgbiOqhTEPqJfb02d_L6rrdduHSxv26s_Ztg_JYYavkrqgs85IT1xZYwtbWIRE8OIvQKf\n5.7.14 xxtT7ENlZTS0Xyqnc1u4_MOrBVW8pgyNyeEgKKnKNyxce76JrsdnE1JgSQzr3pr47bL-kC\n5.7.14 XifnWXg> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 fl15sm17237099pdb.92 - gsmtp')
Your code looks correct. Try logging in through your browser and if you are able to access your account come back and try your code again.
Just make sure that you have typed your username and password correct
EDIT:
Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:
Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps
Your code looks correct but sometimes google blocks an IP when you try to send a email from an unusual location. You can try to unblock it by visiting https://accounts.google.com/DisplayUnlockCaptcha from the IP and following the prompts.
Reference: https://support.google.com/accounts/answer/6009563
I have just sent an email with gmail through Python.
Try to use smtplib.SMTP_SSL to make the connection. Also, you may try to change the gmail domain and port.
So, you may get a chance with:
server = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
server.login(gmail_user, password)
server.sendmail(gmail_user, TO, BODY)
As a plus, you could check the email builtin module. In this way, you can improve the readability of you your code and handle emails headers easily.
I am working on a piece of code that regularly sends emails from 4 different accounts, 2 of them are gmail accounts and the other 2 are yahoo accounts. When I started writing the code I was able to send all the emails from both the gmail accounts using the following piece of code:
def sendGmail(self, fromaddr, toaddr, username, password,
email_body, email_subject
):
# Build the email
msg = MIMEText(email_body)
msg['Subject'] = email_subject
msg['From'] = fromaddr
msg['To'] = toaddr
try:
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddr, msg.as_string())
server.quit()
print "email sent: %s" % fromaddr
except Exception as e:
print "Something went wrong when sending the email %s" % fromaddr
print e
As I said, this piece of code worked perfectly, now that I add the sendYahoomail(), which is a different method, I always get (530, 'Access denied') as an exception for using sendGmail(). I'm pretty sure it's got nothing to do with my Yahoo method, and I can login from the browser with the gmail credentials correctly.
What can possibly be wrong, or just Gmail doesn't want me to send through code ?
I tried your code above with my Gmail account (actually it's a Google Apps account, but that should be identical) and it works fine for me. I even tried an incorrect "From" header and also using the incorrect source address at the SMTP level - both times Gmail allowed me to send the email, but seemed to quietly fix up the header on egress. Still, you might like to double-check your from address matches your username. Also try using the full email address as the username if you're currently just using the part before the #.
I also tried an incorrect password and received error 535, which isn't the same as what you're seeing. Since I have 2-factor authentication enabled I also tried my real password instead of an application-specific one and that still gave a 535 error (but with the message "application-specific password required").
Is it possible that your ISP has set up something that's intercepting SMTP connections to Gmail? Seems unlikely, although my ISP once blocked access to Gmail on port 587 although port 465 still worked. Perhaps you could try using smtplib.SMTP_SSL on port 465 just in case and see if that gives you any more joy.
You could also try sending to addresses at different providers in case Gmail is rejecting the send for that reason (e.g. if the other provider has been got on to a spam blacklist, for example). Also, if it's possible your email looks like spam, try you code with a message subject and body that's close to an authentic email and see if that helps.
You might find this helpful: https://support.google.com/mail/answer/14257
Basically, Google has detected an attempt to sign in from a server that it considers unfamiliar, and has blocked it. The link above lets you attempt to unblock it.
Sometimes it has worked for me, and sometimes not.