Django - Sending email with sendgrid templates - problem with a from address - django

I am using Sendgrid Dynamic Transactional Templates to send emails from my django project. The code can be seen below.
sg = SendGridAPIClient(djangoSettings.SENDGRID_KEY)
message = Mail(
from_email = some#email,
to_emails=some#email,
)
message.dynamic_template_data = {...
}
message.template_id = '....'
response = sg.send(message)
The email sends fine, but I need to use a from_email to send. I don't want to use an email they can reply to. I want some generic email associated with my sendgrid account, lets say mydomain.com. How can I go about solving that?

If you have authorized the domain in your Sendgrid account you can add any address you want so long as it's part of the authorized domain.
To do this go to, https://app.sendgrid.com/settings/sender_auth, press Authenticate your domain and follow the steps to add your domain. This includes configuring your DNS, DKIM and SPF. For more details on adding a domain visit here
Now in your code, you could add noreply#yourdomain.com, or any variation of that, as the from address so long as you authenticated yourdomain.com.

Related

Unsure of how to manage email configurations

I am attempting to create Reset Password functionality using Djoser. I am successfully hitting my API's auth/users/reset_password/ endpoint, which is then sending an email as expected. But the problem is occurring in the content of the email. It is sending a redirection link to my api, rather than to my frontend.
Please note, any <> is simply hiding a variable and is not actually displayed like that
Here is an example of what the email looks like:
You're receiving this email because you requested a password reset for your user account at <api>.
Please go to the following page and choose a new password: <api>/reset-password/confirm/<uid>/<token>
Your username, in case you've forgotten: <username>
Thanks for using our site!
The <api> team
The goal with this email is to send the user to the /reset-password/confirm/ url on my frontend, not on my api, which is currently occurring.
Here are my DJOSER settings:
DJOSER = {
'DOMAIN': '<frontend>',
'SITE_NAME': '<site-name>',
'PASSWORD_RESET_CONFIRM_URL': 'reset-password/confirm/{uid}/{token}',
}
The expected behavior is for the DOMAIN setting to alter the link that is being placed in the email, but it is not. I can't seem to find reference to this particular problem within the docs.
Any help here would be greatly appreciated, thanks.
I figured it out:
Due to Djoser extending the package django-templated-mail, the variables DOMAIN and SITE_NAME have to override django-templated-mail setting rather than Djoser's setting. So, you have to pull variables specific to django-templated-mail out of the Djoser variable.
The working setup actually looks like:
DOMAIN = '<frontend>',
SITE_NAME = '<site-name>',
DJOSER = {
'PASSWORD_RESET_CONFIRM_URL': 'reset-password/confirm/{uid}/{token}',
}

How to change reply_to email address in Vtiger 6.4 for all the outgoing mails via Mail Manager?

When any user sends any email via Mail Manager, the default reply_to email address is set as the Help_desk_emailid by Vtiger. I want to change that reply_to email address and want to use some custom dynamic email address for all the outgoing emails via Mail manager only; not for any other outgoing emails such as events, reminders etc.
It would be really helpful even if i need to do that directly in the code in core files.
All the emails through Mail Manager are sent via different module and not using /modules/Emails/mail.php
All the emails are sent from /modules/Emails/models/Record.php via send() function.
So to change the reply_to email address we need to modify below line in send() function:
$replyTo = $currentUserModel->get('email1');
You can change reply to email address in /modules/Emails/mail.php
if(isUserInitiated()) {
$replyToEmail = $from_email;
} else {
$replyToEmail = $from_email_field;
}
Just change the logic of getting from email address or else you can add static email address.

Mailgun domain not found: example.com

I am trying to setup emails with my own website. Let's say the domain name is example.com.
The name server in use is digital ocean and I also have a gmail account linked to the same (say using contact#example.com).
While setting up things with mailgun, I used mg.example.com (as they said it would also let me email using the root domain). The verification step is done and I can send email using contact#mg.example.com.
However, trying to use the root domain (contact#example.com) gives the following error:
AnymailRequestsAPIError: Sending a message to me#gmail.com from contact#example.com
ESP API response 404:
{
"message": "Domain not found: example.com"
}
How do I resolve this issue?
I got the same error when I copy-pasted the curl example from Mailgun help page.
My domain was set to EU region, and I had to set the api domain to api.eu.mailgun.net instead of api.mailgun.net.
Boom! Working! :)
I am using the EU region with Mailgun and have run into this problem myself. My implementation is a Node.js application with the mailgun-js NPM package.
EU Region Implementation:
const mailgun = require("mailgun-js");
const API_KEY = "MY_API_KEY"; // Add your API key here
const DOMAIN = "my-domain.example"; // Add your domain here
const mg = mailgun({
apiKey: API_KEY,
domain: DOMAIN,
host: "api.eu.mailgun.net" // -> Add this line for EU region domains
});
const data = {
from: "Support <support#my-domain.example>",
to: "recipient#example.com",
subject: "Hello",
text: "Testing some Mailgun awesomness!"
};
mg.messages().send(data, function(error, body) {
if (error) {
console.log(error);
} else {
console.log(body);
}
});
Further options for the mailgun() constructor can be found here.
Thought I'd share a full answer for anybody that's still confused. Additionally, Mailgun Support was kind enough to supply the following table as a reference guide:
IF:
your domain is an EU domain AND
you're using django-anymail as in Rob's answer above
THEN the ANYMAIL setting (in your Django project settings) should specify the API_URL to be the EU one, example:
ANYMAIL = {
'MAILGUN_API_KEY': '<MAILGUN_API_KEY>',
'MAILGUN_SENDER_DOMAIN': 'example.eu',
'MAILGUN_API_URL': 'https://api.eu.mailgun.net/v3' # this line saved me!
}
Before adding the MAILGUN_API_URL I was getting this error:
AnymailRequestsAPIError: Sending a message to xxx#example.com from noreply#example.eu <noreply#example.eu>
Mailgun API response 404 (NOT FOUND):
{
"message": "Domain not found: mailgun.example.eu"
}
Update 8/22/16:
Anymail has been updated to take a new MAILGUN_SENDER_DOMAIN in settings.py. See version .5+ docs.
--
Original Answer
You did not post your code for how you're sending your email, but you are probably trying to send using the simple send_mail() function:
from django.core.mail import send_mail
send_mail("Subject", "text body", "contact#abc.example",
["to#example.com"],)
When you use this method, Anymail pulls the domain out of your From address and tries to use this with Mailgun. Since your From address (abc.example) doesn't include the subdomain mg., Mailgun is confused.
Instead, you need to send the email using the EmailMultiAlternatives object and specify the Email Sender Domain like so:
from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives("Subject", "text body",
"contact#abc.example", ["to#example.com"])
msg.esp_extra = {"sender_domain": "mg.abc.example"}
msg.send()
Don't forget the brackets in your To field, as this needs to be a tuple or list even if you're only sending it to one recipient.
For more information, see Anymail docs on esp_extra.
Struggled for days with correct DNS settings and finally found as #wiktor said, i needed to add "eu" to api endpoint to make it work. Its actually also documented here: https://documentation.mailgun.com/en/latest/api-intro.html#mailgun-regions
Sorry for replying as an answer, dont have enough rep to add comment :(
I had the same problem: 404 error, domain not found.
The cause
The EU region selection for the domain on Mailgun
The solution
Change the region from EU back to the default of US.
Since I had not used the domain at all up to this point, I simply deleted it, re-added it, then changed my TXT, MX and CNAME records (for example, mailgun.org instead of eu.mailgun.org) at the domain registrar (which was GoDaddy in my case).
I found my fix with this change:
ANYMAIL = {
...
'MAILGUN_SENDER_DOMAIN': 'example.com', # Using the sending domain in Mailgun
}

Sending email not working on heroku

I have this function in forms.py. There is currently no email specifications in my settings.py.
def send_email(FROM_NAME,FROM,TO,SUB,MSG,EXISTING_EMAIL,EXISTING_PASSWORD):
FROMADDR = "%s <%s>" % (FROM_NAME, FROM)
LOGIN = EXISTING_EMAIL
PASSWORD = EXISTING_PASSWORD
TOADDRS = [TO]
SUBJECT = SUB
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += MSG+"\r\n"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
I call it my views.py like so
send_email('my_name','from_me#gmail.com','to_som1#gmail.com','my subject','mymessage','my_existing_email#gmail.com','password_to_existing_email')
This works locally. I have tested it with yahoomail and gmail. But when I upload to heroku it gives the error "(535, '5.7.1 Please log in with your web browser and then try again. Learn more at\n5.7.1 support.google.com/mail/bin/answer.py?answer=78754 et6sm2577249qab.8')"
Can anyone help?
You want to use this:
FROMADDR = "%s <%s>" % (your_name, your_email)
You shouldn't be building emails with string interpolation, that's a good way to get your site used to send spam via header injections. See my answer here for details on how to construct emails securely.
Generally speaking, when formatting from addresses, you should use the format Display Name <email#example.com>. See RFC 5322 for details.
Have you read the page linked to in the error message?
If you're repeatedly prompted for your username and password, or if
you're getting an 'invalid credentials' or 'web login required' error,
make sure your password is correct. Keep in mind that password are
case-sensitive.
If you’re sure your password is correct, sign in to your account from
the web version of Gmail instead at http://mail.google.com
In most cases signing in from the web should resolve the issue
Here is what worked for me. After getting the error Please log in with your web browser and then try again. Learn more etc. when trying to send email from my web application, I logged in to the email via browser from my local computer.
After I logged in, there was a yellow notification bar on top which asking me if I want to allow external application access my mail. I confirmed this and Google asked me to log in to the account from the application within the next 10 mins. This will white-list the application.

customize amazons SES - remove amazonses.com

I finally got SES to work, but now im confronted with another problem
The email sent leaves amazonses.com in the from and mailed-by fields like thisI am currently on sandbox access
from: name#myDomain.com via amazonses.com
to: john#aol.com
date: Sat, Jun 2, 2012 at 3:30 PM
subject: Subject message
mailed-by: amazonses.com
question: how can i remove amazonses.com from these 2 fileds and replace the 'mailed-by' field with my companies email address?
heres my php
function sendMail($to, $subject, $message, $from ) {
require_once('sdk-1.5.6.2/sdk.class.php');
$AWS_KEY = "********************";
$AWS_SECRET_KEY = "****************************************";
$amazonSes = new AmazonSES(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY ));
$response = $amazonSes->send_email(
$from,
array('ToAddresses' => array($to)),
array(
'Subject.Data' => $subject,
'Body.Html.Data' => $message,
)
);
if (!$response->isOK()) {
return false;
}else {
return true;
}
return false;
}
sendMail('john#aol.com', "Body message", "Subject message", "name#myDomain.com" );
Since july 2012 it is easy to sign outgoing mail via SES itself.
Instructions are easy and straightforward.
p.s.: sorry for nekroposting, but this question is the one that popped up when I tried to do the same thing.
The reason you're getting this is by default you're encouraged to use Amazon's own DKIM setup.
In order to remove the 'via amazonses.com' text you'll need to follow instructions similar to these in order to set up your own DKIM keys.
Note that I haven't yet tested this myself.
Set the MX and SPF record obtained form amazon ses into your subdomain through which your sending your emails.
eg:
Suppose verified domain on amazon ses is: domain.com
then add the MX and SPF record obtained when you click on verfied domain to the subdomain- abc.domain.com
It will change your mailed by to abc.domain.com
I don't believe you can. SES allows only to send from your domain (or other verified address), and it always has the ses stamp.
Even if your php mail has From address other than the verified email from SES it will go through the SES but via amazonses will show. But if the From address is the verified email with SES then the via address will not show up. So not to show the via address its better to change the php script of sending mail.
You can replace mailed-by with your companie's email address. To do that, you must create a custom MAIL FROM domain which is the domain with subdomain like sub.example.com.
The below is how you create the custom MAIL FROM domain sub.example.com.
Click on the domain example.com.
Then, the detail is shown.
Then, click on MAIL FROM Domain to show Set MAIL FROM Domain button.
Then, clicking on Set MAIL FROM Domain button, the window is open.
Then, put the subdomain "sub" to the field and click on Set MAIL FROM Domain button.
Then, the MX Record and SPF Record are shown. You must add the MX Record and SPF Record to Route 53 in addition to the records of the domain example.com to verify the custom MAIL FROM domain sub.example.com.
Finally, check if the custom MAIL FROM domain sub.example.com is verified under MAIL FROM domain section.
If MAIL FROM domain status is still pending verification, click on retry in blue.
Then, it will be verified.
After the custom MAIL FROM domain is verified, mailed-by is sub.example.com whether or not you send emails using example.com or sub.example.com and whether or not your account is in the sandbox.