Forum Moderators: coopster

Message Too Old, No Replies

PHP mail function

formatting and including links in mail

         

naiquevin

11:32 am on May 28, 2009 (gmt 0)

10+ Year Member



I am using an application by which users on my website can invite others by sending them a mail. I am using the PHP mail function for this.

using the following as the msg, I tried sending a message to my own addresss... the mail was sent sucessfully but as expected, it was recieved as it is. ie. along with the HTML tags. I understand that HTML cannot be used here

$msg = 'Hello ' . $mem_name . ',<br/> ' . $invitor . ' has invited you to join '.
'<a href="index.php">My community</a>

What is the other way to format the text and also include a link and add images to the mail?

Thanks

kaidok

11:39 am on May 28, 2009 (gmt 0)

10+ Year Member



You are better downloading and using PHPmailer, in particular its function IsHtml, that will treat the message as html message. I think that the mail function itself cannot send messages as html.

rocknbil

2:25 pm on May 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the other way to format the text and also include a link and add images to the mail?

To send html email, you need to

a) compose text/html mail headers,
b) you should make sure the output is a valid html document to reduce SpamAssassin points (e.g., "claims to be HTML type but no opening HTML element...")

Using your example (may need debugging,)

// If you set company name right here no reason to
// to an "if isset . . ."
$company_name='Your Company';
$url = 'http://www.example.com/';
$mail_subj = 'Join Our Community';
$from_email = 'no-reply@example.com'; //Small anti-spam precaution

$from_email = (isset($company_name))?'"'.$company_name.'" <'.$from_email.'>':$from_email;

$msg = '
<html>
<head>
<title>'
.$mail_subj.
'</title></head><body><p>Hello '
. $mem_name .
',</p><p> '
. $invitor .
' has invited you to join
'<a href="'.$url.'">My community</a></p></body></html>';

send_mail($to_email,$from_email,$mail_subj,$msg);


function send_mail($to,$from,$email_subj,$emailbody) {
// To send HTML mail, the Content-type header must be set
//Also a FROM field is required or it comes from anonymous.
$headers = "From: $from\r\n"; // Preformat as "company" <email>
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
mail($to, $email_subj, $emailbody, $headers);
}

penders

2:31 pm on May 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The reason you are seeing the HTML tags is because you are sending a plain text email.

You can send HTML emails using PHP's mail() [uk2.php.net] function (you should also include a text/plain section) but you need to specify that it's mime encoded and set the content-type to text/html. There is an example on the mail() manual page (link above).

You can send any kind of email using PHP's mail() function, attachments, embedded images etc. It's just that you need to do it yourself, which can be rather tedious, particularly because of the differences in email clients.

Does PHPmailer use PHP's mail() function in its implementation, or does it go that step further?

penders

3:25 pm on May 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<a href="index.php">My community</a>

Also, any links will need to be an absolute URL (with protocol and fully qualified domain name).

Just to add... certain email clients will parse plain text emails and turn what it thinks are URLs into clickable links - but that is purely down to the email client.

naiquevin

5:33 am on May 29, 2009 (gmt 0)

10+ Year Member



Got it

@rocknbil - Thanks a lot. That works just the way I want

@penders, thanks for the info