2

I am using python 3.4.3 to send e-mail, and at this time I will be needing the e-mail to be sent under an alias. The account is a gmail account, but I need to be able to put whatever I want as the spoof(alias) 'From' e-mail. I have looked quite hard at how to do this and have had very little luck. Given the amount of threads I've looked at and the actuality that I haven't gotten a workable answer shows the lack of discussion about this specific topic. I hope it's not just that this is something so very easy that everyone but me knows how to do it.

I should mention that I am on a windows 10 machine, but have access to a Ubuntu, and Windows 7 machine as well.

import smtplib

fromreal = 'realmail@gmail.com'
fromshow = 'fakemail@gmail.com'
toaddy = ['rec01@gmail.com', 'rec02@gmail.com']
subject = ' test'
body = 'This is the body test'

content = '''\
From: %s
To: %s
Subject: %s
%s
''' % (fromshow, ', '.join(toaddy), subject, body)

server = 'smtp.gmail.com'
port = 587

mail = smtplib.SMTP(server, port)

mail.ehlo()
mail.starttls()
mail.login(fromreal, 'password')
try:
    mail.sendmail(fromshow, toaddy, content)
    print('E-mail sent.')
except:
    print('E-mail not sent.')

mail.close()
dubkoidragon
  • 21
  • 1
  • 5
  • 1
    Gmail server will *not* let you send email with a faked sender. – spectras Oct 21 '15 at 10:04
  • Still, you should never use `except` without specifying which exception you want to catch. Here, I am pretty certain that proper error handling would have given you a hint on what's going wrong. – spectras Oct 21 '15 at 10:09
  • could you help me with some of that proper error handling?? – dubkoidragon Oct 27 '15 at 08:19

2 Answers2

6

You can use yagmail to send an alias (not changing to the fake email, but at least the alias):

import yagmail
# first is "from" arg; using a dictionary you can give an alias as value
yag=yagmail.SMTP({fromreal:'fakealias'}, 'password') 
yag.send(toaddy, subject, body)

How nice it is to have 3 lines instead of 30 ;)

Install using pip install yagmail.

Read more about a lot of other features on the github page.

Among other things, you could use "passwordless" scripts (no need for password in script), really easy to send HTML, inline images and attachments!

Full disclosure: I'm the developer/maintainer of yagmail.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • Can I contact you with some more questions? – dubkoidragon Oct 27 '15 at 08:42
  • @dubkoidragon GitHub provides a way to @ contact me, https://github.com/kootenpv/yagmail. – PascalVKooten Oct 27 '15 at 09:15
  • Has anybody else experienced a very instable performance of sending mail when using this alias feature? It seems that sometimes i recieved a mail, and sometimes I just dont. It silently fails (= with no errors, but mail doesn't appear in inbox). Checked spam folder, and with no luck too... – Marjan Moderc Jul 31 '17 at 11:52
  • @PascalVKooten Was spoofing feature removed form later releases of yagmail? Can't get it to work. – miran80 Sep 27 '21 at 21:20
  • My bad, it seems that gmail is the one that limits it, because I rewrote your yagmail lib to accept spoofed from header and it still didn't work:) – miran80 Sep 27 '21 at 22:39
  • @Valentin (https://stackoverflow.com/users/11221432/valentin) pointed out the usage was unclear - from his proposed edit I gathered `fromreal` was unclear. Basically, if you read the original question, you can see that `fromreal` is the variable name with the value `'realmail@gmail.com'` – PascalVKooten Feb 16 '23 at 21:05
0

Your code is fine,

Google prevents you from setting an alias email that not belongs to you. That's why you need to set the alias in your gmail account. To do this go to https://mail.google.com/ -> settings -> (see all settings) -> Accounts -> Send mail as: -> add another email address.

Validate the email address and then you can set your alias as used in your code.

If you get an SMTPAuthenticationError (534, b'5.7.9 Application-specific password required. ...) you should follow the link to set an app-password instead of your real password.

Easy_Israel
  • 841
  • 6
  • 8