Django Email backend (keeps sending email from incorrect "sender") - django

I have several instances in my project where I attempt to send an email within a Django view.
I want to be able to hardcode the email sender within the view. When I try to do so, though, it continues to send the emails from the default account specified in my settings file.
Here is an example:
if testform.is_valid():
beta=testform.save()
subject="Hi Beta Tester"
sender="correct#email.com"
recipient=[testform.cleaned_data['email']]
text=loader.get_template('registration/beta_email.txt')
html=loader.get_template('registration/beta_email.html')
site_name='mysite'
d=Context({'site_name':site_name})
text_content=text.render(d)
html_content=html.render(d)
#This sends two mail versions, plain text and html
msg=EmailMultiAlternatives(subject, text_content, sender, recipient)
msg.attach_alternative(html_content, "text/html")
msg.send()
return HttpResponseRedirect('/splash/')
I thought that I could send specify the sender argument explicitly here. And, yet, when I test it, the email is being sent from the address listed in my settings file, configured as the following:
EMAIL_USE_TLS=True
EMAIL_HOST='smtp.gmail.com'
EMAIL_HOST_USER='wrong#email.com'
EMAIL_HOST_PASSWORD='private'
DEFAULT_FROM_EMAIL='wrong#email.com'
Do I just have to remove the DEFAULT_FROM_EMAIL constant to make it work? I tried doing so and it seems to be working but I'm confused. In the Django documentation, it suggests that setting sender in the view should override the DEFAULT.

I've finally figured out the issue here. Unfortunately, gmail rewrites the from and the
envelope on authenticated smtp.
If you want to get around that, you have to use a third party mail server (which doesn't act like such a prissy) and then send mail to gmail users.

For the sender e-mail try putting it in < > and you can add a name:
sender = "Formal Name <correct#email.com>"
that is exactly the syntax I have in my e-mail sending view and it works.
There really shouldn't be a reason that adding the name to it would change how it's sending, but it may be worth trying and perhaps you want an easily readable name anyway.

Related

How can I work with cfmail failto attribute?

I've checked with the cfmail tag. But, if we use failto attribute. It doesn't work to me. So, what can I do? Could you please provide an exact answer to me?
There are many variables at play here but we rely on failto to function for a high volume email system so I can confirm it works. The underlying thing that is happening is that it lets you specify the envelope FROM for the message (what the email server sees) separately from the header FROM (what the receiving user sees). Envelope from can be considered like the reply address on a physical envelope and the header FROM like an address on the letter inside the envelope. If failto is not included in the cfmail tag then they are one in the same.
If the failto is being ignored for some reason or a mail server chooses to ignore it then then the header From address (cfmail from attribute) should be getting the bounce.
If the From address is not getting a bounce then your email is likely just not going out. The message has to reach the mail server for the bounce to occur. If ColdFusion is just failing to reach the mail server then mail will end up in the undelivr folder in your CF instance Mail directory. If that is happening then go into your CF admin interface and ensure the mail server can connect. To retry a message you can just move from undelivr to spool directory.
None of the above an issue? Make sure that you can send an email through your mail server to the failto address you are trying to use, otherwise there is no point in putting that address in the failto attribute.
If you're not getting a return email for failures, it's probably because the target email's server isn't set up to return a notification of failed delivery.
See here: https://community.adobe.com/t5/ColdFusion/CFMail-How-do-I-know-if-email-is-bounced/td-p/2001466

Check if mail account is from sharedbox

I write code in Outlook plugin and I would like to identify when a mail is sent, if it's from a sharedbox mail or a userbox mail.
So I have the object of mail 'MailItem'
https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa210946(v=office.11)
And I get the linked account with method 'SendUsingAccount' and I get object 'Account'
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook._account?view=outlook-pia
But here i can't fine any method/property fto check if account is sharedbox or userbox
Has anyone an idea to achieve this?
That is a perfectly valid email address of type EX (as opposed to SMTP). Check the MailItem.SenderEmailType property. If it is "SMTP", use the SenderEmailAddress property. If it is "EX", use MailItem.Sender.GetExchangeUser.PrimarySmtpAddress. Be prepared to handle nulls/exceptions.

Postfix header_check for multiple recipients

I have some development and test servers, that are only used for internal testing and development, but they often use setup from live solutions, including email addresses of customers and customers customers.
I would like to avoid the dev-solutions sending emails to customers, but allow them to send emails to developers, so that they can test the email part of the solution. I have tried a header_checks rule like this:
/^To:.*#(myowndomaion.com|myotherdomain.com|athirddomain.net)/ DUNNO
/^To:.*#/ REDIRECT mytestemail#myowndomain.com
And this works fine. If I send an email to test#gmail.com it will be forwarded to mytestemail#myowndomain.com and not be sent to test#gmail.com. But if I however send an email to myuser#myowndomain.comit will be send straight to the correct user. That is all fine.
The problem arises if I send emails to several email addresses!
So, if I send an email to test#gmail.com and myuser#myowndomain.com in the same email, they will both get the email (which they should not). The order does not matter, both will get emails in both cases.
So, does anyone have a better regex or some other better way to solve this?
Note: We use Postfix on Debian. (pretty standard Debian 6 and Debian 8)

How to pipe an email into Django?

I'm part of a two-man business selling LED glow toys and one aspect of this is handling support requests. I have a server running exim4 and DJango and have email working so that if a user sends an email to support#myhost.com I'm able to pick up the email and respond.
I'd like to develop something a bit tidier to keep track of the email chain for a particular support request.
To do this, I was thinking of piping the output of my email using a rule in my support email's filter:
pipe /usr/bin/email_to_django_script
What I'm unsure of is how best to go about the last step to actually turning the email content into something DJango can process.
What's the best method to do this? Would a script using curl -d be a sensible option or are there better / less convoluted ways?
You can pipe the output of the email server into a management command. As an example, I have a file /inquiries/management/commands/proc_email.py. I have a single Command class, and the handle() method gets most of the email from the environment, and the body of the email from STDIN:
from_email = strip_tags(os.environ.get('SENDER', None))
to_email = strip_tags(os.environ.get('RECIPIENT', None))
emailMessage = email.message_from_string(''.join(sys.stdin.readlines()))
There is other code in there, but that is how I get the important bits out of it. You can then pipe this into your ORM objects, and access it from the website at some later time.
This is then accessed through /path/to/project/manage.py proc_email.
Depending on your email server, you can also use plus addressing to insure replies come back to the same address. For example, I have my Reply-To headers set to inquiry+12345#whatever.com. The mail server (postfix) then dumps this into the environment under EXTENSION. If no number is supplied, I simply create a new Inquiry, instead of attaching to an existing one.
Not exactly a pure Django solution but I would recommend taking a look at Lamson Project. It's an email server written in Python that you can use to build email applications like what you are describing. I can also integrate with the Django ORM. http://lamsonproject.org/docs/hooking_into_django.html

CFMail with catchall email addresses

I can't believe I've never noticed this before, but it seems that CFMail won't send to an email address that isn't explicitly set up on the destination mailserver.
This means that if I'm using 'info#somedomainorother.com' and have that set up to catch all email on the domain, CFMail won't send to 'test#somedomainorother.com'.
This causes a massive amount of problems for me, as I'm using CFMail to send out order confirmations, member activations and all manner of other bits and pieces.
Whatever your views on using catchall addresses, it can't be denied that people do use them So, in any case that a user enters a made-up address into one of my sites, they won't receive their email.
There must, simply MUST be a way around this - can anyone help?
For refernece, the message that appears in the logs when sending to a catchall address is 'Invalid Addresses'.
EDIT: Here's the CFMail syntax I'm using -
<cfmail to="#Arguments.sEmailAddress#" from="#Application.sAppEmailAddress#" subject="Stock reminder confirmation: #Local.qGetProductDetails.sProductName# - #Application.sCompanyName#" type="HTML" server="#Application.sAppEmailServer#" username="#Application.sAppEmailAddress#" password="#Application.sAppEmailPassword#">
Translates into:
<cfmail to="thisisatest#somedomainorother.com" from="application#mydomainname.com" subject="Stock reminder confirmation: Some product - My Company" type="HTML" server="mail.mydomainname.com" username="application#mydomainname.com" password="XXXXXX">
All works fine for info#somedomainorother.com but not for randombunchofcharacters#somedomainorother.com.
Important to note of course, that the catch-all is working correctly in all other respects, test emails from mail clients work perfectly.
Its not ColdFusion that cares about email validity, its the SMTP server. CF only cares about well formed email addresses.
If you initiated a telnet session to your mail server and tried to use the same address, I'm sure it would have the same result.
Debugging tips for SMTP Connectivity:
http://www.talkingtree.com/blog/index.cfm/2004/11/22/debug-smtp
Can I see your CFMAIL tag setup? CFMAIL doesn't care as long as the email address is properly formatted.
Urgh!
Turns out it was an issue with the server. For some reason, catchall email accounts serverwide had stopped working properly. After an email to my hosting provider, it's all working fine with no code changes.
They're somewhat cagey as to what caused the issue, and I was still able to use an email client to send mail out to the addresses...
Thanks for the help in any case. ;)