How to handle special chars in email subject? - email

I am using the Swiftmailer in my Symfony2 webapp.
// Subject and body dynamically come from database
$subject = "This is my subject with an apostroph ' in it.";
$bodytext = "Test text, also with an ' apostrophe in it.";
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom('me#mail.com')
->setTo('you#mail.com');
$message->setBody($bodytext);
$this->get('mailer')->send($message);
Now when there is a special char, e.g. the apostrophe (') in my subject, the email has a strange subject line in my email client:
This is my subject with an apostroph ' in it
Funny thing: The body text is displayed correctly, it's only the subject that's wrongly formatted.
Now how can I handle special chars like this - and even better, is there a function I can call before passing the subject that handles special chars in general?

Sorry, I know this is late but it might help anyone still having these similar issues.
I was having issues with apostrophe ' and ampersand & on SwiftMailer 6.2 so combining both htmlspecialchars_decode and html_entity_decode to convert the subject resolved it for me:
->setSubject(htmlspecialchars_decode(html_entity_decode($subject), ENT_QUOTES))
See https://www.php.net/manual/en/function.html-entity-decode.php and https://www.php.net/manual/en/function.htmlspecialchars-decode.php for further explanations.

Try to escape the subject with the htmlentities PHP function:
$subject = htmlentities("This is my subject with an apostroph ' in it.");

Related

How to Remove a section of text from a string using powershell?

I a building an email and have this section of content in that email which I need to remove at times so I am tying to do a replace from #HOUSESTART through #HOUSEEND using the -replace but it is not working.
$body contains this section along with much more of the entire html email:
"<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><b><u><span style='mso-ascii-font-family:Calibri;mso-fareast-font-family:
"Times New Roman";mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'>#HOUSESTART<o:p></o:p></span></u></b></p>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><b><u><span style='mso-ascii-font-family:Calibri;mso-fareast-font-family:
"Times New Roman";mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'>PLEASE
NOTE</span></u></b><span style='mso-ascii-font-family:Calibri;mso-fareast-font-family:
"Times New Roman";mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'>:
As a house manager, you have two email addresses. Your secondary email
address is #EMAIL. The only place you will need to use this email address
is when you are enrolling any device in the Targeted Threat Protection.<span
style='mso-spacerun:yes'>  </span><b><span style='background:yellow;mso-highlight:
yellow'>(Only used in manager welcome emails.)</span><o:p></o:p></b></span></p>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><b><span style='mso-ascii-font-family:Calibri;mso-fareast-font-family:
"Times New Roman";mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'>#HOUSEEND</span></b><span
style='mso-ascii-font-family:Calibri;mso-fareast-font-family:"Times New Roman";
mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'><o:p></o:p></span></p>
I am using this command to try and remove everything between #HOUSESTART and #HOUSEEND but it is not removing it.
$body = $body -replace "#HOUSESTART.*#HOUSEEND"," "
Any help would be greatly appreciated.
By default, metacharacter . in .NET regexes matches any character except newlines.
Therefore, if you want .* to match across multiple lines, i.e, to match newlines too, you must use inline regex option s ((?s) at the very start of the regex):
$body = $body -replace '(?s)#HOUSESTART.*#HOUSEEND', ' '
Note:
* I'm using '...' (single quotes, i.e. verbatim strings) rather than "..." (expandable (interpolating) string), to avoid confusion between what PowerShell may interpret up front, and what the regex engine will see.
* .* matches greedily, so that everything to the input's last instance of #HOUSEEND is matched; if there can be multiple instances, and you want to match only through the next one, use the non-greedy .*?
Note that $body must be a single, multi-line string for this to work.
For instance, if you use something like $body = Get-Content file.txt to set $body, you'll end up with an array of strings, each of which the -replace operation is applied to, which won't work. In that case, use the -Raw switch to ensure that the file is read as a single, multi-line string: $body = Get-Content -Raw file.txt.

mailto: with multiple addresses and real names

I have backend admin tool that manages a number of groups of people working in different sections. From time to time I need to email all the people in one group, so I created a button in my admin tool which does a simple mailto: for all the users in that section. For example:
Mail All
And this works fine. However, I wanted to add their real names to the mailto link so when I'm sending the mail I can quickly see who's in the group. So I tried formatting the link like this:
Mail All
But that seemed to only pick up the first email address and list the 'real name' as one long name with commas.
Searching the web, documentation is scant on multiple addresses with real names (only found info when sending one). So wondering whether this is (a) not possible, (b) possible, but I've got the syntax wrong or c) only possible if I use a workaround like copying all the email address data onto the clipboard and paste it into the mail.
Any email gurus out there?
I had the same question. I fooled around and got it to work using UTF-8 encoding.
"First Last" <firstlastname#example.com>
becomes
Send Email
I was also able to add a bcc field with multiple addresses by following the example above and separated them with commas.
This launched in both Outlook and in Gmail Compose by replacing "mailto:" with "https://mail.google.com/mail/?view=cm&fs=1&tf=1&to="
I say "not possible", at least the way I interpret RFC 6068:
The syntax of a 'mailto' URI is described using the ABNF of [STD68],
non-terminal definitions from [RFC5322] (dot-atom-text, quoted-
string), and non-terminal definitions from [STD66] (unreserved, pct-
encoded):
mailtoURI = "mailto:" [ to ] [ hfields ]
**to = addr-spec *("," addr-spec )
hfields = "?" hfield *( "&" hfield )
hfield = hfname "=" hfvalue
hfname = *qchar
hfvalue = *qchar
**addr-spec = local-part "#" domain
**local-part = dot-atom-text / quoted-string
domain = dot-atom-text / "[" *dtext-no-obs "]"
dtext-no-obs = %d33-90 / ; Printable US-ASCII
%d94-126 ; characters not including
; "[", "]", or "\"
qchar = unreserved / pct-encoded / some-delims
some-delims = "!" / "$" / "'" / "(" / ")" / "*"
/ "+" / "," / ";" / ":" / "#"
(I've marked the interesting rules with **)
Specifically:
<addr-spec> is a mail address as specified in [RFC5322], but excluding
<comment> from [RFC5322].
The address format you are trying to use is called name-addr in RFC5322. addr-spec is just the name#domain part.

What is this Perl string encoded in?

I'm using use Mail::IMAPClient to retrieve mail headers from an imap server. It works great. But when the header contains any character other that [a-z|A-Z|0-9] I'm served with strings that look like this :
Subject : Un message en =?UTF-8?B?ZnJhbsOnYWlzIMOgIGxhIGNvbg==?= (original string : "Un message en français à la con")
Body :
=C3=A9aeio=C3=B9=C3=A8=C3=A8 (original string : éaeioùèè)
What is this strange format ? Is that the famous "perl string
internal" format ?
what is the safest way of handling human idioms
coming from IMAP servers ?
The body encoding is Quoted-Printable; the header (subject) encoding is MIME "encoded-word" encoding ("B" type for base64). The best way to deal with both of them is to pass the email into a module that's capable of dealing with MIME, such as Email::MIME or the older and buggier MIME::Lite.
For example:
# $message was retrieved from IMAP
my $mime = Email::MIME->new($message);
my $subject = $mime->header('Subject'); # automatically decoded
my $body = $mime->body_str; # also automatically decoded
However if you need to deal with them outside of the context of an entire message, there are also modules like Encode::MIME::Header and MIME::QuotedPrint.
It is quoted-printable coded. It is a standard encoding used in email. It has nothing to do with Perl's internal string format.

MIME::Entity headers encoding correct?

I use MIME::Entity module in Perl to create a MIME message. Some of the headers seem to be encoded OK, while other seem to have issues with folding.
Code:
use strict;
use Encode;
use MIME::Entity;
my %build_params = (
'Charset' => 'UTF-8',
'From' => encode('MIME-Header', 'Fantasy Email <vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh#gmail.com>'),
'Subject' => encode('MIME-Header', "A very long subject that will span on multiple lines in the headers, with a leading sp\
ace at the beginning of each new line."),
'Type' => 'multipart/alternative',
);
my $top = MIME::Entity->build(%build_params);
$top->print_header();
Output:
Content-Type: multipart/alternative;
boundary="----------=_1312196104-11708-0";
charset="UTF-8"
Content-Transfer-Encoding: binary
MIME-Version: 1.0
X-Mailer: MIME-tools 5.427 (Entity 5.427)
Subject: A very long subject that will span on multiple lines in the
headers, with a leading space at the beginning of each new line.
From: Fantasy Email
<vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh#gmail .com>
The Subject seems to be correctly split into multiple lines. The From doesn't, leaving a space before the com, but the newline is gone.
Is this standard behavior or have I found a bug in MIME::Entity?
Encode::MIME::Header (called as encode('MIME-Header', ...)) does some line splitting (called folding in the RFC 822).
Unfortunately, MIME::Entity does some line splitting too, probably in a different way. It also gets rid of the newline generated by Encode::MIME::Header. It leaves the spaces though.
I would be happy to leave MIME::Entity deal with the encoding of my headers, but it looks like it just does the line splitting part. So I guess I still have to encode them myself.
As a workaround, I removed the line splitting markers from my encoded headers with
my $encoded_from = encode('MIME-Header', 'Fantasy Email <vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh#gmail.com>');
$encoded_from =~ s/\r?\n\s//g;
(And same thing for the subject.)
Now the output looks like this:
Subject: A very long subject that will span on multiple lines in the
headers, with a leading space at the beginning of each new line.
From: Fantasy Email
<vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh#gmail.com>
I'm wondering if there's a more elegant solution, like Encode::MIME::Header featuring a MIME::Entity compatibility mode or something like that.

Problem encoding with utf-8

I referred to serhio for the utf-8 encoding problem and hv been trying for the whole day different methods searched from net :( I want to show the chinese characters in subject lines but when received in gmail it shows rubbish characters. I had tried to put
header('Content-Type: text/html; charset=utf-8');
on top of page but not working
i tried to add "\r\n" also not working
My code as below
$mail->charset = 'utf-8';
$mail->body('',$strInv);
$mail->subject('"=?UTF-8?B?".base64_encode(我的问题)."?=" #'.$inquiry_no);
when I received in gmail subject looks like this :
"=?UTF-8?B?".base64_encode(è®¢å •ç¡®è®¤)."?=" #00016
I really appreciate anyone can help me with this. Thank you.
when you fix it your subject string should look like this:
=?UTF-8?B?RUSSIANNNN?=
use the echo function to debug your subject string before you call
$mail->subject
or just do
$ssubject = '=?UTF-8?B?' . base64_encode('RUSSIAN') . '?=';
$ssubject = $ssubject . $inquiry_no;
$mail->subject($ssubject);
good luck newbie