开发者

html email issue for a tag value

开发者 https://www.devze.com 2023-04-09 20:22 出处:网络
I am sending html email to the client via php mail function. while & sign shown in bold creating problems in email replacing ! %20 something like characters within my ids such as below(in bold).

I am sending html email to the client via php mail function. while & sign shown in bold creating problems in email replacing ! %20 something like characters within my ids such as below(in bold).

http://test.com/test-page.php?id=abcd**!**1234&cat_id=23

Below is my code. $to = 'test@abc.com';

// subject
$subject = 'test';

//message
 $message.='<html><head><meta charset="UTF-8" /></head><body><p><a href="http://test.com/test-page.php?id=abcd1234&cat_id=23" target="_blank">Wine **&** Dine Offers</a></p></body></html>';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

// Additional headers
$headers .= 'To: test <test@abc.com>开发者_如何学C;' . "\r\n";
$headers .= 'From: test user <no-reply@test.com>' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers); 

After sending mail i am getting ! and %20 like characters in email. I also tried & except & in email but no use still ! adding within my email html.


Try running urldecode() on the incoming parameters. Example:

$id = urldecode( $_GET['id'] );


I think you should encode the URL on the email. Plus, you probable have magic_quotes_gpc "on" that is not recommended.

I always use PHPMailer, it saves a lot of work and helps on these problems


try this:

$toName  = 'test'; 
$toAddress  = 'test@abc.com'; 

$fromName  = 'test user'; 
$fromAddress  = 'no-reply@test.com'; 

// subject
$subject = 'test';

// URL for link
// this should have any URL encoded characters literally in the string
$url = 'http://test.com/test-page.php?id=abcd1234&cat_id=23&msg=URL%20encode%20this%20string';

// HTML for message
// Call htmlspecialchars() on anything that may need it (like the URL)
$messageHTML = '<html><head><meta charset="UTF-8" /></head><body><p><a href="'.htmlspecialchars($url).'" target="_blank">Wine &amp; Dine Offers</a></p></body></html>';

// Text version of message
// Remember, not everyone has an HTML email client!
$messageText = "Wine & Dine Offers: $url";

// Build a multipart MIME message
$boundary = '------'.md5(microtime()).'------';
$body =
"This is a multipart message in MIME format.\r\n"
."$boundary\r\n"
."Content-Type: text/plain\r\n"
."\r\n"
."$messageText\r\n"
."$boundary\r\n"
."Content-Type: text/html; charset=UTF-8\r\n"
."\r\n"
."$messageHTML\r\n"
."--$boundary";

// To send HTML mail, the Content-type header must be set correctly
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/alternative; boundary=\"$boundary\"\r\n";

// Additional headers 
// the To: header will be set by mail() and is not required here
$headers .= "From: $fromName <$fromAddress>\r\n";

// Mail it
mail("$toName <$toAddress>", $subject, $body, $headers); 


I changed the encoding settings on phpmailer to use base64 encoding using

$mail->Encoding=”base64"

This solved my problem.

As there is issue for HTML mail meassage exceeds length 998 or something i got the above solution to it. :)

http://www.jeremytunnell.com/posts/really-hairy-problem-with-seemingly-random-crlf-and-spaces-inserted-in-emails

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号