How do I get PHP to send strings as UTF-8 and parse html code too, via email?

This is the code that is being sent via email:
$_['email_text_1']      = 'NAME OF COMPANY: <a href="http://www.mydomain.org">http://www.mydomain.org</a>';
$_['email_text_2']      = 'offre prodotti e servizi a condizione vantaggiose per le famiglie sostenedno il mondo delle associazioni e del volontariato secondo formule innovative. L\'iscrizione è completamente GRATUITA e non comporta nessun impegno da parte tua.';

Open in new window


This is the php code that is sending the email:
<?php
class ModelCatalogTellafriend extends Model {
	public function sendMail($data) {
		
		$data['email'] = $this->customer->getEmail();
		
        $data['name'] = $this->customer->getFirstName();
		
		$customer_id = $this->customer->getId();
		
         $friend = $data['sendto'];
		 $this->notifyFriend($friend, $data['email'], $data['name']);
		 $this->notifyAdmin($data);   
	}

	public function notifyFriend($friend, $email, $name) {
		
		require_once(DIR_SYSTEM.'smtp/class.phpmailer.php');
		
		$customer_id = $this->customer->getId();
		$encoded_string = base64_encode($customer_id);
		
		$this->load->language('information/tellafriend');
		$subject = sprintf($this->language->get('email_subject'), $name);
		$message = $this->language->get('email_greeting') . "\n\n";
		$message .= sprintf($this->language->get('email_text_1'), $name, $email) . "\n\n";
		$message .= sprintf($this->language->get('email_text_2'), $name) . "\n\n";
		$message .= $this->config->get('config_name') . ': ' . $this->config->get('config_url'). "\n\n";
		$message .= sprintf($this->language->get('email_invite'))."\n\n";
		$message .= sprintf($this->language->get('email_signup')). "http://missionfamily.org/index.php?route=account/register&invite_id=".$encoded_string. "\n\n";
		
		$mail = new PHPMailer();

		$mail->IsSMTP();                       // telling the class to use SMTP

		$mail->SMTPDebug = 1;                  
		// 0 = no output, 1 = errors and messages, 2 = messages only.

		$mail->SMTPAuth = true;                // enable SMTP authentication
		$mail->SMTPSecure = "tls";              // sets the prefix to the servier
		$mail->Host = "smtp.gmail.com";        // sets Gmail as the SMTP server
		$mail->Port = 587;                     // set the SMTP port for the GMAIL

		$mail->Username = "sdgsdg@sdgsdg.com";  // Gmail username
		$mail->Password = "xdgsgdsgsdg";      // Gmail password

		$mail->CharSet = 'windows-1250';
		$mail->SetFrom ('administrator@mydomain.org');
		$mail->AddBCC ('administrator@mydomain.org');
		$mail->Subject = $subject;
		$mail->ContentType = 'text/plain';
		$mail->IsHTML(false);

		$mail->Body = $message; 
		// you may also use $mail->Body = file_get_contents('your_mail_template.html');

		$mail->AddAddress ($friend, $name);     
		// you may also use this format $mail->AddAddress ($recipient);

		if(!$mail->Send())
		{
		        $error_message = "Mailer Error: " . $mail->ErrorInfo;
		} else 
		{
		        $error_message = "Successfully sent!";
		}
		
	}

	public function notifyAdmin($data) {
		$this->load->language('information/tellafriend');
		$subject = $this->language->get('email_admin');
		$message = sprintf($this->language->get('email_sender'), $data['name'], $data['email']) . "\n\n";
		$message .= $this->language->get('email_recommend') . "\n";
		$message .= $data['sendto'] . "\n";
		$mail = new Mail();
		$mail->protocol = $this->config->get('config_mail_protocol');
		$mail->hostname = $this->config->get('config_smtp_host');
		$mail->username = $this->config->get('config_smtp_username');
		$mail->password = $this->config->get('config_smtp_password');
		$mail->port = $this->config->get('config_smtp_port');
		$mail->timeout = $this->config->get('config_smtp_timeout');
		$mail->setTo($this->config->get('config_email'));
		$mail->setFrom($this->config->get('config_email'));
		$mail->setSender($this->config->get('config_name'));
		$mail->setSubject($subject);
		$mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
		$mail->send();
	}
}
?>

Open in new window



I need those two strings to come via email with the html code parsed and also the accented letters (like "è") showing correctly.

How can I achieve this? Which lines should I edit?

thanks
badwolfffAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marcus BointonCommented:
You're sending your email having told it to expect content using a windows-1250 charset; if you're not providing your data in that charset (either by it being submitted originally in that charset or via conversion), then it will appear corrupt. I would suggest you do everything in UTF-8, it's just much simpler that way.
badwolfffAuthor Commented:
yes, thanks, I got that already. What I don't know is HOW to do it :)

Can you help?
badwolfffAuthor Commented:
Do I just change that line?
$mail->CharSet = 'windows-1250';
if so,
what do I change it to?
Marcus BointonCommented:
Just:

    $mail->CharSet = 'utf-8';

But you need to know that the data you're giving it is also in UTF-8 - it doesn't convert it for you.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Consistency from beginning to end is important in any application that uses UTF-8 (and all of them will use it soon).  Some of the background you need to understand is in this article.  It's an increasingly common issue for PHP software, since PHP changed its posture toward UTF-8 recently.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.