PHPMailer: Subject name in arabic (non-English) characters - php

I am using PHPMailer API for sending emails. I was wondering how can I send the subject in Arabic (non-English) language
$mail->CharSet = 'utf-8';
$array= FetchTable('cos');
$subject = $_POST['subject'];
$body = $_POST['body'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "host";
$mail->SMTPAuth = true;
$mail->Username = "yaz#enfaltourism.com";
$mail->Password = "*******";
$mail->Port = "587";
$mail->From = "yaz#enfaltourism.com";
$mail->FromName = "Enfaltourism";
$mail->Subject = $subject;
$mail->AddAddress($email);
$mail->Send();`
The email is being successfully sent, but the problem is in sending subject in Arabic language. The email message body is displayed properly in Arabic after I set the char encoding but the the subject is being displayed in weird characters
Update
include("../mail/class.phpmailer.php");
$array= FetchTable('cos');
$subject = $_POST['subject'];
$body = $_POST['body'];
$mail = new PHPMailer();
$mail->CharSet = 'utf-8';
$mail->IsSMTP();
$mail->Host = "mail.enfaltourism.com";
$mail->SMTPAuth = true;
$mail->Username = "yaz#enfaltourism.com";
$mail->Password = "*****";
$mail->Port = "587";
$mail->From = "yaz#enfaltourism.com";
$mail->FromName = "Enfaltourism";
$mail->Subject = $subject;
i fixed as u told but the subject still weird character like this

I had this same problem with Japanese characters, both in the body and in the subject line:
I resolved it (email looks outstanding now) by doing:
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
There really isn't any need to encode the subject separately so long as the object has the CharSet property set to UTF-8.

you can try using this code. Let me know if it helps
$phpmailer->Subject = "=?UTF-8?B?".base64_encode($subject)."?=";

Try encoding subject with base64_encode
$subject = '=?UTF-8?B?'.base64_encode('سلام علیکم').'?=';
However with setting the issue must be solved.
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';

Try this:
$mail->CharSet = 'UTF-8';
$mail->Subject = html_entity_decode("Recuperación de contraseña");
I did use this in my code and it worked!
Results

Related

PHPMailer Sending emails to two users with different body

Good day. I need to send two different messages to two different people (user and admin). Tell me how to do this?
My mail php
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$name = $_POST['name'];
$email = $_POST['email'];
$mail->isSMTP();
$mail->Host = 'smtp.mail.ru';
$mail->SMTPAuth = true;
$mail->Username = 'pmewilberries#mail.ru';
$mail->Password = '123456789456';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('pmewilberries#mail.ru');
$mail->addAddress($email);
$mail->Subject = 'Заявка на участие в интенсиве Wildberries';
// $mail->Body($body);
// $mail->isHTML(true);
$body = 'Hello'
$mail->msgHTML($body);
$mail->send()
?>
Send one message, change the properties that are different, then send the second one:
$mail->addAddress($email);
$body = 'Hello';
$mail->msgHTML($body);
$mail->send();
$mail->clearAddresses();
$mail->addAddress($email2);
$body = 'Hello2';
$mail->msgHTML($body);
$mail->send();
It will be slightly quicker if you set keepalive, which will make it re-use the existing connection for the second message:
$mail->SMTPKeepAlive = true;
Try this,
$mail->addAddress($email);
$body = 'Hello';
$mail->msgHTML($body);
$mail->send();
$mail->ClearAllRecipients();
$mail->addAddress($email2);
$body = 'Hello2';
$mail->msgHTML($body);
$mail->send();
Look at the code for clearAllRecipients;

phpmailer str_replace multiple times

I have the following setup:
$message = file_get_contents('./site/resources/external/template.html');
$message = str_replace('[name]', $email['name'], $message);
$message = str_replace('[password]', $password, $message);
require './site/resources/external/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPSecure = 'tls';
$mail->Username = "mymail";
$mail->Password = "mypassword";
$mail->AddAddress($email);
$mail->FromName = "Marc";
$mail->CharSet = 'UTF-8';
$mail->Subject = "Din nye adgangskode";
$mail->msgHTML($message, dirname(__FILE__));
$mail->Host = "smtp.live.com";
$mail->Port = 587;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
$mail->Send();
However when i try to send it, it does not send.
However when i do like this:
However when i do the following:
$mail->msgHTML(str_replace('[password]', $password, file_get_contents('./site/resources/external/template.html')), dirname(__FILE__));
it sends without a problem.
So how can i add multiple variables to my template ?
Although I don't see how the variable substitution of $message could be breaking your code, there is a multiple replacement form of str_replace that uses array arguments:
$mail->msgHTML(
str_replace(
array('[password]','[name]'),
array( $password, $email['name'] ),
file_get_contents('./site/resources/external/template.html')
),
dirname(__FILE__)
);
I suspect something else is wrong with your code, but this might be useful.

PhpMailer Authentication Error

I'm trying to configure email by using phpmailer and smtp but Authentication error appears.
Code below
include_once("phpmailer.php");
$mail = new PHPMailer();
$tarih = date("d.m.Y - H:i:s");
$mail->IsSMTP();
$mail->Host = "mail.golcukdh.gov.tr";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = "username";
$mail->Password = "password";
$mail->IsHTML(true);
$mail->From = "email";
$mail->AddAddress("email");
$mail->Subject = "subject";
$mail->Body = "Body Part";
$mail->Send()
?>
I tried to configure this by using asp.net with these authentication information and it was successfull. But I want to use php, what is wrong ?

PHP Object of class PHPMailer could not be converted

I have read similar threads but this problem is different. Problem is that 100% same code with same class is working on another page. But it is not working on index page for some odd reason. Here is error I get:
The only difference is that the working file is located in subdirectory on root that is /xyz but the index file is located in root that is / but the action to the form points to file in sub directory that is /xyz/file.php so I don't think that could be a problem.
PHP Catchable fatal error: Object of class PHPMailer could not be converted to string in /home4/elliot/public_html/web.com/class.phpmailer.php on line 764
Here is what PHP Mailer Shows on line 764:
$address = trim($address);
Here is What working code looks like:
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "removed";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "removed";
$mail->Password = "removed";
//Set who the message is to be sent from
$mail->setFrom('xx#xx.com', 'John Doe');
$mail->addReplyTo('xx#xx.com', 'John Doe');
//Set who the message is to be sent to
$mail->addAddress($email,$full);
$mail->Subject = 'Subject';
$mail->Body = $body;
$mail->IsHTML(true);
$mail->send();
Here is what non working code looks like:
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "removed";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "removed";
$mail->Password = "removed";
//Set who the message is to be sent from
$mail->setFrom('xx#xx.com', 'John Doe');
$mail->addReplyTo('xx#xx.com', 'John Doe');
//Set who the message is to be sent to
$mail->addAddress($email,$full);
$mail->Subject = 'Subject';
$mail->Body = $body;
$mail->IsHTML(true);
$mail->send();
I got it. IT was such a stupid error. I was using $mail variable for both object and also email address.

PHPMailer tweek

Hello i need help on PHPMailer here is my code :
$message = 'main message';
$bccmessage = 'BCC Message';
include '../inc/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$mail->Host = "smtphost";
$mail->SMTPAuth = true;
$mail->Username = "email#domain.com";
$mail->Password = "xxxxxx";
$mail->From = "email#domain.com";
$mail->FromName = "foo.com";
$mail->AddAddress($mainemail);
$mail->AddBCC($bccemail);
$mail->AddBCC($bccemail);
$mail->Subject = "Subject";
$mail->Body = "$message";
if(!$mail->Send())
{
echo '<pre>Error: '.$mail->ErrorInfo.'</pre>';
exit;
} else {
//Display result
echo '<div class="success">message Sent</div>';
}
My question is how can i manage that the "AddAddress" get $message and the "AddBCC" get the $bccmessage message.
You will have to send 2 separate emails to accomplish that.
You could do something like:
$oMail->Body = $sToMessage;
$oMail->addAddress($sToEmail);
$oMail->send();
// clear
$oMail->ClearAddresses();
$oMail->Body = $sBccMessage;
$oMail->addAddress($sBccEmail);
$oMail->send();
But I'd have to advice against it. You would do better to wrap the email sending in an function - and calling it twice with different parameters.

Categories