Link to home
Get AccessLog in
Avatar of badwolfff
badwolfffFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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=""></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:
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']);

	public function notifyFriend($friend, $email, $name) {
		$customer_id = $this->customer->getId();
		$encoded_string = base64_encode($customer_id);
		$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')). "".$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 = "";        // sets Gmail as the SMTP server
		$mail->Port = 587;                     // set the SMTP port for the GMAIL

		$mail->Username = "";  // Gmail username
		$mail->Password = "xdgsgdsgsdg";      // Gmail password

		$mail->CharSet = 'windows-1250';
		$mail->SetFrom ('');
		$mail->AddBCC ('');
		$mail->Subject = $subject;
		$mail->ContentType = 'text/plain';

		$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);

		        $error_message = "Mailer Error: " . $mail->ErrorInfo;
		} else 
		        $error_message = "Successfully sent!";

	public function notifyAdmin($data) {
		$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->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));

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?

Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

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.
Avatar of badwolfff


yes, thanks, I got that already. What I don't know is HOW to do it :)

Can you help?
Do I just change that line?
$mail->CharSet = 'windows-1250';
if so,
what do I change it to?
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
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.