tags:

views:

80

answers:

6

when I send a user an email verification message, how do I construct it to look something like this:

alt text

How do I add links? using tags? and newlines?

$message = 'Thanks for signing up!' - a newline afterwards...
then blablablah *link to confirmation*

I would really appreciate any kind of help in this.

A: 

Just construct the link and add it to the email body. The mail client figures out what is a link. Alternatively you can go down the full html email route.

feihtthief
How do I add newlines though? And what is 'full html email route'? Can I construct the entire mail in HTML and the mail client would interpret that, say, as a browser would?
sombe
A: 

You have several options for this.

1.You can use plain text as the mime type, then setup your $message var like:

$message = "Thank you... \n\r";
$message.= "next line... \n\r";

or use heredoc

$message = <<<BLOCK
Thank you...
next line...
BLOCK;

Most email programs should convert the link, I think. But if you need to do that explicitly,

2.then you could always use a html mime type for your email and just provide it basic HTML as the message body.

nowk
+3  A: 

It depends on the type of email you are sending. Modern email clients supports 2 types of email:

  • Text - in this case you just add link to your message like raw text. No special formating is required, but it'll depend on email client to handle the link. Most email clients will interpret string starting from http:// as link.
  • Html - in this case just add the link as usual html code <a href="...">...</a>. Most email agents undestand basic html well.

By default mail() function sends email in raw text. Here is small example:

$lines = array(
    "Hello!",
    "Your password is ...",
    "Click following link to unsubscribe:",
    "http://your_site/unsubscribe?..."
);
$body = implode("\r\n", $lines); ## join lines
mail($to, $subject, $body);      ## and send text email
Ivan Nevostruev
using \r\n makes some problem for YahooMail as it can't read headers properly in such messages, \n should be enough to satisfy all
tomaszsobczak
HTML is not interpreted with emails I send with HTML tags in them.
sombe
@tomaszsobczak Citation? `\r\n` is the established standard.
ceejayoz
@Gal For HTML to be interpreted, you need to send a MIME type header that indicates to the client that it is HTML. The default type is `text/plain`, which doesn't allow HTML to be interpreted.
ceejayoz
@ceejayoz - well i know that it is standard, Iam just nade a comment to tell some solution that i spent few hours to find, sending \r\n as line brakes to Yahoo crashes headers when reading in webmail (same thing for gmx.net as i remember)
tomaszsobczak
+1  A: 

If you send it with plain-text email programs nearly always will convert a URL into a clickable link. Otherwise you can send an HTML email.

How to organize your code:

Smarty templates are also great for this. You can have a .tpl template that you pass in variables to, so your email templates are in their own files that have only the tags/fields needed. Then the PHP can pull in this content, parse it, and send the body as the email.

philfreo
A: 

Like the others said, just send the eMail and let the client figure out what is clickable.

Just create a simple textfile template in a folder accessible by your mail script, e.g.

Thanks for signing up

This is your login data
------------
u: [username]
p: [password]
-------------

Active your account by clicking [activation-link]

Then in your mailing script, you load this file and str_replace() the placeholders. This way, you dont have to garble your mail script with the email message and can easily maintain any changes without having to touch your script. You also dont have to bother coding linebreaks into it.

I strongly discourage using HTML mail. There is a huge uncertainty on the receiving end. Users may access their mail with clients unable or disabled to render HTML, so you will have to send the mail with alternate text (multipart). In addition, rendering of HTML in clients is a major pain.

Gordon
A: 

To specifically address your question regarding new lines, you have a few ways you can do it:

1

$message = 'Thanks for signing up!
blablablah *link to confirmation*'

Use single quotes and a simple <enter> at the end of the line.

2

$message = "Thanks for signing up!\n\r
blablablah *link to confirmation*"

Use double quotes, \n\r (this creates the new line - actually a line feed and a carriage return) and a simple <enter> at the end of the line (this is purely for your own readability.

3

Use heredoc as nowk suggests.

Blair McMillan