Unintended Indents with Plain Text Email - email

I have a function that will send plain text and HTML emails. When coding, I indent code to get proper nesting. Note the two code strings below.
$plain_text = '
*Hi ' . show_user_name($user_id) . '. Thanks for signing up!*
';
$plain_text = '
*Hi ' . show_user_name($user_id) . '. Thanks for signing up!*
';
When the second one is sent as plain text email, the line "*Hi...." is indented.
How can I prevent this from happening?

In short, it's indented because you indented it!
In plain-text email messages (unlike HTML), white space is preserved and significant, so if you don't want leading spaces, strip them from your content:
$plain_text = preg_replace('/^ +/m', '', $plain_text);
You need the m modifier to apply the leading space stripping to each line in the body.
Alternatively, don't indent in the first place - the indenting of your code doesn't have to be related to the format of the text that you generate:
$plain_text =
'*Hi ' . show_user_name($user_id) . '. Thanks for signing up!*';
If you're using tabs for indents, you could filter those out too:
$plain_text = preg_replace('/^[ \t]+/m', '', $plain_text);

Related

How can I get the substrings with spaces as end delimiter

String text ="65:234567
69:456789678
74:45678";
How do I get the text following 65: .I should be able to retrieve 234567.
I would not be able to get the text using str.substring(firstIdx + secondIdx) as the no of digits post ":" might change.
Please advice .

Remove Text between two Strings

I have a disclaimer message in an email which I want to remove using Perl.
The code is below:
my $stval = 'hii This is a test Email*************** CAUTION - Disclaimer *****************
This e-mail contains PRIVILEGED AND CONFIDENTIAL INFORMATION intended solely
for the use of the addressee(s). If you are not the intended recipient, please
notify the sender by e-mail and delete the original message.
******MAILEND***** End of Disclaimer ******MAILEND*****';
$stval =~ s/[*]//g; # this removes all * Characters
print "$stval\n\n";
The output I am expecting should be as below:
hii This is a test Email
That scalar string has several embedded \n, as if it is a here document. You can remove everything from the first '*' to the end of the string with:
$stval =~ s/[*\n]+.+//g; # this removes all * Characters
Use s modifier to include newline in the deletion:
$stval =~ s/\*{10}.*//s;

How to handle special chars in email subject?

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.");

Why won't my extension render umlauts?

I am working on an extension to display downloads on a website. You can view the full, current source over on GitHub.
Given this piece of code in my controller:
$linkName = Tx_Downloads_Utility_Filename::construct( $download );
$download->setLinkText( $linkName );
This is where I want to set the label for a download. Sadly, when it is later rendered, the result will be blank if $linkName contained an umlaut (umlauts were just my test subject, the actual scope is unknown).
For debugging purposes, I have extended that section to look like this:
$linkName = Tx_Downloads_Utility_Filename::construct( $download );
$download->setLinkText( $linkName );
$this->flashMessages->add( "'" . strlen( $linkName ) . "'" );
$this->flashMessages->add( urlencode( $linkName ) );
$this->flashMessages->add( $linkName );
The resulting output of that is:
Please note that no third flash message is rendered.
But it's not like no umlauts would be rendered. For example, this is the record I am debugging with:
The link field (between the image icon and the 31.06KB) is blank but should say Text_File_Sömething.jpg. The string Sömething is rendered perfectly fine in another place of the template.
Is the problem with my Fluid template?
Sorry, that was not really clear. Next try:
you call Tx_Downloads_Utility_Filename::construct($linkName) which (by default) calls Tx_Downloads_Utility_Filename::clean($linkName) which again removes all the special characters by replacing anything that doesn't match the regex pattern /([[:alnum:]_\.-]*)/ by underscores.
There seems to be a problem with encoding (maybe your db is not set to UTF-8 encoding) so Text_File_Sömething is actually turned into Text_File_Sömething and the clean() method turns that into an invalid string. try using utf8_encode() on the $filename first.

How to use tab in disp()?

disp(['counter ' num2str(blk) (here I need a tab!!!) 'adc ' num2str(adc_nr)])
Try
disp(['counter ' num2str(blk) 9 'adc ' num2str(adc_nr)])
Explanation: Usually if you want to insert a tab, you put a '\t' in the string. This works well for sprintf, but the disp command doesn't interpret it properly. So one solution is to put the ASCII value of the tab directly, which is '9'.