def mail_to(email_address, name=None, cc=None, bcc=None, subject=None, body=None, replace_at=None, replace_dot=None, encode=None, **html_options): """ Creates a link tag for starting an email to the specified ``email_address``, which is also used as the name of the link unless ``name`` is specified. Additional HTML options, such as class or id, can be passed in the ``html_options`` hash. You can also make it difficult for spiders to harvest email address by obfuscating them. Examples:: >>> mail_to("*****@*****.**", "My email", encode = "javascript") <script type="text/javascript" language="javascript">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script> >>> mail_to("*****@*****.**", "My email", encode = "hex") <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a> You can also specify the cc address, bcc address, subject, and body parts of the message header to create a complex e-mail using the corresponding ``cc``, ``bcc``, ``subject``, and ``body`` keyword arguments. Each of these options are URI escaped and then appended to the ``email_address`` before being output. **Be aware that javascript keywords will not be escaped and may break this feature when encoding with javascript.** Examples:: >>> mail_to("*****@*****.**", "My email", cc="*****@*****.**", bcc="*****@*****.**", subject="This is an examjust ple email", body= "This is the body of the message.") <a href="mailto:[email protected]?cc="*****@*****.**"&bcc="*****@*****.**"&body="This%20is%20the%20body%20of%20the%20message."&subject="This%20is%20an%20example%20email">My email</a> """ extras = {} for key, option in ('cc', cc), ('bcc', bcc), ('subject', subject), ('body', body): if option: extras[key] = option options_query = urllib.urlencode(extras).replace("+", "%20") email_address_obfuscated = email_address if replace_at: email_address_obfuscated = email_address_obfuscated.replace('@', replace_at) if replace_dot: email_address_obfuscated = email_address_obfuscated.replace('.', replace_dot) if encode=='hex': email_address = ''.join(['%%%x' % ord(x) for x in email_address]) url = 'mailto:' + email_address if options_query: url += '?' + options_query html_options['href'] = url tag = tags.content_tag('a', name or email_address_obfuscated, **html_options) if encode =='javascript': tmp = "document.write('%s');" % tag string = ''.join(['%%%x' % ord(x) for x in tmp]) return javascript_tag("eval(unescape('%s'))" % string) else : return tag
def secure_button_to(name, url='', **html_options): """ Create a button (like webhelpers.rails.urls.button_to) including a hidden authentication token field. """ id = authentication_token() button_html = _button_to(name, url, **html_options) return '%s\n%s</form>' % (button_html, content_tag('div', hidden_field(token_key, id), style='display:none;'))
def secure_form_remote_tag(**args): """Create a form tag (like webhelpers.rails.prototype.form_remote_tag) including a hidden authentication token field. """ id = authentication_token() form_html = form_remote_tag(**args) return '%s\n%s' % (form_html, content_tag('div', hidden_field(token_key, id), style='display: none;'))
def secure_button_to(name, url='', **html_options): """ Create a button (like webhelpers.rails.urls.button_to) including a hidden authentication token field. """ id = authentication_token() button_html = _button_to(name, url, **html_options) return '%s\n%s</form>' % ( button_html, content_tag('div', hidden_field(token_key, id), style='display:none;'))
def mail_to(email_address, name=None, cc=None, bcc=None, subject=None, body=None, replace_at=None, replace_dot=None, encode=None, **html_options): """ Creates a link tag for starting an email to the specified ``email_address``, which is also used as the name of the link unless ``name`` is specified. Additional HTML options, such as class or id, can be passed in the ``html_options`` hash. You can also make it difficult for spiders to harvest email address by obfuscating them. Examples:: >>> mail_to("*****@*****.**", "My email", encode = "javascript") <script type="text/javascript" language="javascript">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script> >>> mail_to("*****@*****.**", "My email", encode = "hex") <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a> You can also specify the cc address, bcc address, subject, and body parts of the message header to create a complex e-mail using the corresponding ``cc``, ``bcc``, ``subject``, and ``body`` keyword arguments. Each of these options are URI escaped and then appended to the ``email_address`` before being output. **Be aware that javascript keywords will not be escaped and may break this feature when encoding with javascript.** Examples:: >>> mail_to("*****@*****.**", "My email", cc="*****@*****.**", bcc="*****@*****.**", subject="This is an examjust ple email", body= "This is the body of the message.") <a href="mailto:[email protected]?cc="*****@*****.**"&bcc="*****@*****.**"&body="This%20is%20the%20body%20of%20the%20message."&subject="This%20is%20an%20example%20email">My email</a> """ extras = {} for key, option in ('cc', cc), ('bcc', bcc), ('subject', subject), ('body', body): if option: extras[key] = option options_query = urllib.urlencode(extras).replace("+", "%20") email_address_obfuscated = email_address if replace_at: email_address_obfuscated = email_address_obfuscated.replace( '@', replace_at) if replace_dot: email_address_obfuscated = email_address_obfuscated.replace( '.', replace_dot) if encode == 'hex': email_address = ''.join(['%%%x' % ord(x) for x in email_address]) url = 'mailto:' + email_address if options_query: url += '?' + options_query html_options['href'] = url tag = tags.content_tag('a', name or email_address_obfuscated, **html_options) if encode == 'javascript': tmp = "document.write('%s');" % tag string = ''.join(['%%%x' % ord(x) for x in tmp]) return javascript_tag("eval(unescape('%s'))" % string) else: return tag
def mail_to(email_address, name=None, cc=None, bcc=None, subject=None, body=None, replace_at=None, replace_dot=None, encode=None, **html_options): """ Create a link tag for starting an email to the specified ``email_address``. This ``email_address`` is also used as the name of the link unless ``name`` is specified. Additional HTML options, such as class or id, can be passed in the ``html_options`` hash. You can also make it difficult for spiders to harvest email address by obfuscating them. Examples:: >>> mail_to("*****@*****.**", "My email", encode = "javascript") '<script type="text/javascript">\\n//<![CDATA[\\neval(unescape(\\'%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b\\'))\\n//]]>\\n</script>' >>> mail_to("*****@*****.**", "My email", encode = "hex") '<a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>' You can also specify the cc address, bcc address, subject, and body parts of the message header to create a complex e-mail using the corresponding ``cc``, ``bcc``, ``subject``, and ``body`` keyword arguments. Each of these options are URI escaped and then appended to the ``email_address`` before being output. **Be aware that javascript keywords will not be escaped and may break this feature when encoding with javascript.** Examples:: >>> mail_to("*****@*****.**", "My email", cc="*****@*****.**", bcc="*****@*****.**", subject="This is an example email", body= "This is the body of the message.") '<a href="mailto:[email protected]?cc=ccaddress%40domain.com&bcc=bccaddress%40domain.com&subject=This%20is%20an%20example%20email&body=This%20is%20the%20body%20of%20the%20message.">My email</a>' """ extras = [] for item in ('cc', cc), ('bcc', bcc), ('subject', subject), ('body', body): if item[1]: extras.append(item) options_query = urllib.urlencode(extras).replace("+", "%20") protocol = 'mailto:' email_address_obfuscated = email_address if replace_at: email_address_obfuscated = email_address_obfuscated.replace( '@', replace_at) if replace_dot: email_address_obfuscated = email_address_obfuscated.replace( '.', replace_dot) if encode == 'hex': email_address_obfuscated = ''.join( ['&#%d;' % ord(x) for x in email_address_obfuscated]) protocol = ''.join(['&#%d;' % ord(x) for x in protocol]) word_re = re.compile('\w') encoded_parts = [] for x in email_address: if word_re.match(x): encoded_parts.append('%%%x' % ord(x)) else: encoded_parts.append(x) email_address = ''.join(encoded_parts) url = protocol + email_address if options_query: url += '?' + options_query html_options['href'] = url tag = tags.content_tag('a', name or email_address_obfuscated, **html_options) if encode == 'javascript': tmp = "document.write('%s');" % tag string = ''.join(['%%%x' % ord(x) for x in tmp]) return javascript_tag("eval(unescape('%s'))" % string) else: return tag
def mail_to(email_address, name=None, cc=None, bcc=None, subject=None, body=None, replace_at=None, replace_dot=None, encode=None, **html_options): """ Create a link tag for starting an email to the specified ``email_address``. This ``email_address`` is also used as the name of the link unless ``name`` is specified. Additional HTML options, such as class or id, can be passed in the ``html_options`` hash. You can also make it difficult for spiders to harvest email address by obfuscating them. Examples:: >>> mail_to("*****@*****.**", "My email", encode = "javascript") '<script type="text/javascript">\\n//<![CDATA[\\neval(unescape(\\'%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b\\'))\\n//]]>\\n</script>' >>> mail_to("*****@*****.**", "My email", encode = "hex") '<a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>' You can also specify the cc address, bcc address, subject, and body parts of the message header to create a complex e-mail using the corresponding ``cc``, ``bcc``, ``subject``, and ``body`` keyword arguments. Each of these options are URI escaped and then appended to the ``email_address`` before being output. **Be aware that javascript keywords will not be escaped and may break this feature when encoding with javascript.** Examples:: >>> mail_to("*****@*****.**", "My email", cc="*****@*****.**", bcc="*****@*****.**", subject="This is an example email", body= "This is the body of the message.") '<a href="mailto:[email protected]?cc=ccaddress%40domain.com&bcc=bccaddress%40domain.com&subject=This%20is%20an%20example%20email&body=This%20is%20the%20body%20of%20the%20message.">My email</a>' """ extras = [] for item in ('cc', cc), ('bcc', bcc), ('subject', subject), ('body', body): if item[1]: extras.append(item) options_query = urllib.urlencode(extras).replace("+", "%20") protocol = 'mailto:' email_address_obfuscated = email_address if replace_at: email_address_obfuscated = email_address_obfuscated.replace('@', replace_at) if replace_dot: email_address_obfuscated = email_address_obfuscated.replace('.', replace_dot) if encode == 'hex': email_address_obfuscated = ''.join(['&#%d;' % ord(x) for x in email_address_obfuscated]) protocol = ''.join(['&#%d;' % ord(x) for x in protocol]) word_re = re.compile('\w') encoded_parts = [] for x in email_address: if word_re.match(x): encoded_parts.append('%%%x' % ord(x)) else: encoded_parts.append(x) email_address = ''.join(encoded_parts) url = protocol + email_address if options_query: url += '?' + options_query html_options['href'] = url tag = tags.content_tag('a', name or email_address_obfuscated, **html_options) if encode == 'javascript': tmp = "document.write('%s');" % tag string = ''.join(['%%%x' % ord(x) for x in tmp]) return javascript_tag("eval(unescape('%s'))" % string) else : return tag