2

I am having issues in adding a contact page to a webpage. I am using Flask to do this. The section I dont understand is this:

app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = '''example@gmail.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");
l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';
r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;
s+=String.fromCharCode(c);}s=document.createTextNode(s);
l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>]'''

app.config["MAIL_PASSWORD"] = 'password'

and

msg = Message(form.subject.data, sender='''contact@gmail.com<script
type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");
l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';
r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;
s+=String.fromCharCode(c);}s=document.createTextNode(s);
l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>', recipients=['example@gmail.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");
l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';
r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;
s+=String.fromCharCode(c);}s=document.createTextNode(s);
l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>]''')

Please explain which email address should I put at the following places in the code, where should i add my email id? and what should the other one be. I do not have my own domain.

app.config["MAIL_USERNAME"] = '''example@gmail.com
app.config["MAIL_PASSWORD"] = 'password'

msg = Message(form.subject.data, sender='''contact@gmail.com
recipients=['example@gmail.com . . 
codegeek
  • 32,236
  • 12
  • 63
  • 63
user3182194
  • 1,729
  • 2
  • 13
  • 9
  • I am referring this tutorial http://code.tutsplus.com/tutorials/intro-to-flask-adding-a-contact-page--net-28982 – user3182194 Mar 06 '14 at 16:33

3 Answers3

3

That tutorial seems a little overcomplicated. Not sure why you would need all that Javascript. Here is what works for me which I have tested:

Config settings for gmail should be something like:

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'sender@gmail.com'
app.config['MAIL_PASSWORD'] = 'password'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True

In code above, replace the app.config['MAIL_USERNAME'] with your actual gmail address. Same for password. Using this settings, the email will be sent from your gmail account to the receiver. All this information above is for the sender i.e. your gmail account.

Then, to send the email, just do the following

msg = Message(subject,sender="sender@gmail.com",recipients=['user1@gmail.com','user2@gmail.com'])
msg.body = "test email"
mail.send(msg)

In the above code, replace recipients with whoever you want to send the email to. recipients is a list. So you can enter 1 or more email address as needed.

Finally, using the above 2, flask-mail will send an email from sender@gmail.com (i.e. you) to user1@gmail.com,user2@gmail.com etc. (i.e. the receiver)

codegeek
  • 32,236
  • 12
  • 63
  • 63
2

Also make sure "Access for less secure apps has been turned on" for your google account. Else that can cause SMTP Authentication problems. You can turn on/off from google settings: https://www.google.com/settings/security/lesssecureapps

FILVIN
  • 21
  • 2
0

make sure in security section in your gmail account access to less secure apps has been turned on