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="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
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

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
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.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html