Forums

How can I change the from header in an e-mail?

I'm hoping to use a gmail address I have to send e-mails through a web2py app.

Currently I can only send e-mails to other gmail addresses and the "From" gets re-written to "fromaddress@gmail.com" while I want it to be "From Address Name" which is what I have from my gmail settings.

Also, if I set the headers through web2py it doesn't work, it just gets rewritten. So to summarise the problems are

1) I can only send emails to @gmail.com 2) I can't get the correct from header.

Here is my code:

# E-mail
gmail = Mail()
gmail.settings.server = 'smtp.gmail.com:587'
gmail.settings.sender = 'from@gmail.com'
gmail.settings.login  = 'from@gmail.com:password'
# gmail.settings.tls = True

gmail.send( to=['to@gmail.com'] , subject='Hello1' , message='world' , headers={ 'From':'me' } )

Regarding the from address, I think this may depend on the email client of the receiver. ie. If I am me@gmail.com, and I make the from header you@gmail.com and send it to someone, it is not necessarily a good thing to display you@gmail.com, so some email clients just display me@gmail.com.

What sort of error do you get when sending to a non-gmail address fails? If instead you meant that you are trying to send from a non-gmail address, then it is not going to work with a free account.

No error (or don't know where to find the error)...

I'm trying to send from a gmail address ie it sounds like it should work. Also, I remember reading something about spam filters and the sort so if they see it coming from a smtp server that isn't consistent with the name it filters it. Now I don't understand what fields I need to change in order to be consistent.

For example changing the from to be: From : me via from@gmail.com still doesn't change anything...

I'm pretty sure gmail won't let you change the From to anything other than the email address of the gmail account that you're sending from. You may be able to add a long name to the from by specifying your from address as:

From Address Name<fromaddress@gmail.com>

but that may not work either, if Google just ignore it in favour of using the address that they know.

As for the issue of only being able to send to gmail accounts. That could be anything from gmail blocking it to the recipient blocking it and you'd have to check with the services involved to try to work out what's going on.

hm... maybe someone else has experience on this?

I'm guessing there might be something I'm doing that isn't standard practice. What would standard practice be to send e-mails?

Are you using a particular python module/package to send emails? Or is the standard library smtp module

I'm using the web2py framework and that has the gluon.tools

http://www.web2py.com/books/default/chapter/29/08/emails-and-sms#Setting-up-email

Hi, I had the same problem this is the solution in your email config settings write something like this:

mail.settings.sender = 'From Address Name <fromaddress@gmail.com>'

Thanks for posting that!