Is strip_tags necessary for phpmailer? - phpmailer

I'm using phpmailer with the following settings:
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
...
$mail->Body = $_POST['comments'];
$mail->Body = strip_tags($mail->Body);
I noticed strip_tags() cuts off the text if it hits a single greater than / less than sign (i.e if the user put in one of those characters legitimately).
Given that I have content type = 'text/plain' and ishtml = false, is it even necessary to have strip_tags() in there?

No, it is not neccesary. If you set $mail->isHTML(false) and you write HTML in the email, it is sent like text plain so it doesnt interpret it like HTML.
For example I've just done this:
$mail->ContentType = 'text/plain';
$mail->isHTML(false);
$mail->Subject = 'Your password';
$mail->Body = '<p>Your password is 123</p> Go to google ';
And the mail looks like this:
<p>Your password is 123</p> Go to google

Related

change font size for phpmailer subject

The following function works but I am unable to control the font-size of the "Subject". Is there a way to change the font size of the "Subject". I've tried:
$mail->Subject = "<span style='font-size:3vw'>" .$mailInputs['subject'] ."<\span>";
but that didn't work.
function mailerExpressBlueHost(array $mailInputs){
require_once '../includes/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsMail();
$mail->SetFrom('example#xyz.com');
$mail->IsHTML(true);
$mail->addAddress('abc#example.com');
$mail->AddEmbeddedImage("../public/img/swb.jpg", "swb-image");
$body = '<!DOCTYPE html>
<html><header>
</header>
<body lang=EN-US>
<div style="text-align:center">
<img src="cid:swb-image">' . $mailInputs['body'] ;
$mail->isHTML(true);
$mail->Subject = $mailInputs['subject'] ;
$mail->Body = $body;
if(!$mail->send()) {
return 'Message could not be sent.' . 'Mailer Error: ' . $mail->ErrorInfo;
} else {
return 'Message has been sent';
}
$mail->ClearAddresses();
}
No, you can't do this. The Subject line does not support HTML formatting; it's plain text only - not least because the definition of the Subject email header predates HTML by at least 10 years.
On the upside, you can use unicode, which allows you quite a lot of tricks, but it's highly dependent on the OS and application it's viewed in.

Display inline image attachments with wp_mail

I have a problem.
I would like to attach an image to an email and also display it inline, with some other php-generated content. The problem is I don't have the slightest ideea how to use inline a file attachment array used by wp_mail to attach.
My solution was to encode the images in base64 and put them inline the html like this:
<img alt="The Alt" src="data:image/png;base64,*etc*etc*etc" />
But the problem is that Gmail / Outlook remove the src data from the image. So it lands as
<img alt="The Alt" />
Any clues what to modify (headers to work with base64) or how to use attachment to embed them inline?
Thanks,
Radu.
wp_mail uses the PHPMailer class. This class has all the functionality needed for inline attachments.
To change the phpmailer object before wp_mail() sends the email you could use the filter phpmailer_init.
$body = '
Hello John,
checkout my new cool picture.
<img src="cid:my-cool-picture-uid" width="300" height="400">
Thanks, hope you like it ;)';
That was an example of how to insert the picture in you email body.
$file = '/path/to/file.jpg'; //phpmailer will load this file
$uid = 'my-cool-picture-uid'; //will map it to this UID
$name = 'file.jpg'; //this will be the file name for the attachment
global $phpmailer;
add_action( 'phpmailer_init', function(&$phpmailer)use($file,$uid,$name){
$phpmailer->SMTPKeepAlive = true;
$phpmailer->AddEmbeddedImage($file, $uid, $name);
});
//now just call wp_mail()
wp_mail('test#example.com','Hi John',$body);
That's all.
If you get an unexpected T_FUNCTION error, it is due to the PHP version < 5.3. In that case, create a function to do it in a more traditional way:
function attachInlineImage() {
global $phpmailer;
$file = '/path/to/file.jpg'; //phpmailer will load this file
$uid = 'my-cool-picture-uid'; //will map it to this UID
$name = 'file.jpg'; //this will be the file name for the attachment
if (is_file($file)) {
$phpmailer->AddEmbeddedImage($file, $uid, $name);
}
}
add_action('phpmailer_init','attachInlineImage');
I needed this in a small better way because I'm sending multiple mails in one step and not all mails should have the same embedded images. So I'm using this solution from Constantin but with my Modifications :-)
wp_mail('test#example.org', 'First mail without attachments', 'Test 1');
$phpmailerInitAction = function(&$phpmailer) {
$phpmailer->AddEmbeddedImage(__DIR__ . '/img/header.jpg', 'header');
$phpmailer->AddEmbeddedImage(__DIR__ . '/img/footer.png', 'footer');
};
add_action('phpmailer_init', $phpmailerInitAction);
wp_mail('test#example.org', 'Mail with embedded images', 'Example <img src="cid:header" /><br /><img src="cid:footer" />', [
'Content-Type: text/html; charset=UTF-8'
], [
__DIR__ . '/files/terms.pdf'
]);
remove_action('phpmailer_init', $phpmailerInitAction);
wp_mail('test#example.org', 'Second mail without attachments', 'Test 2');
The first wp_mail will be without attachments.
The second wp_mail will contain embedded images.
The third wp_mail will be without attachments.
It's working fine for now 😎
AddEmbeddedImage only accepts two parameters, so be sure to not include the $name parameter as in the example.

Line break in a message

With Google Apps Script, how to make a line break in a variable to send mail?
If you're not sending an HTML formatted message, use "\n". I personally despise HTML formatted e-mail.
You should use the <br> tag when sending the HTML portion of the email .
Below is a sample on how I compose the same email body, but formatted differently for HTML & plain text. (Not the best code but hopefully it illustrates the point)
function onFormSubmit(e) {
var subject = "Subject";
// Collect user data
var name = e.values[0];
var email = e.values[1]; // Where user enters his/her email address
// Generate content - Replace this with what you're composing
var content = [];
content.push("Hi " + name);
content.push("Thanks for submitting the survey!___LINE_BREAK___");
content.push("Survey Team");
// Combine content into a single string
var preFormatContent = content.join('___LINE_BREAK___');
// Replace text with \n for plain text
var plainTextContent = preFormatContent.replace('___LINE_BREAK___', '\n');
// Replace text with <br /> for HTML
var htmlContent = preFormatContent.replace('___LINE_BREAK___', '<br />');
MailApp.sendEmail(email ,
subject,
plainTextContent ,
{
name: "Survey Team",
html: htmlContent
});
}
I usually use table in my mail but I think <br /> should work
Newline in msgBox:
Browser.msgBox('line 1 \\n line 2');
Please note you need to escape '\n' with additional backslash.

Whenever I use ajax to get data from my textarea and insert it into mysql, line breaks are not inserted

I am using ajax to insert a form using GET. When that form is submitted, it goes into a mysql database. I know that the error is occurring when this data is being submitted into mysql and not when I am retrieving it. My problem is that all line breaks, and when you press the "enter" key are not being submitted into the database. All of the text just goes in as a straight line without breaks or anything of the sort. I would appreciate any help as to figuring out how to get these breaks to actually be inserted into mysql because this is a big problem for my site. Any help is very much appreciated.
Here is the code for the ajax that I am using
$echovar400=
"
<script language='javascript' type='text/javascript'>
function ajaxFunction(){
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try{
ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e){
// Something went wrong
alert('Your browser broke!');
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('pagecomments');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var age = document.getElementById('age').value;
var wpm = document.getElementById('wpm').value;
var queryString = '?age=' + age + '&wpm=' + wpm;
ajaxRequest.open('GET', 'ajaxprofilechat.php' + queryString, true);
ajaxRequest.send(null);
}
</script>
<form name='myForm' method='GET' >
<textarea rows='4' name='message' class='comment' maxlength='250' id='age' wrap='hard'> </textarea><br><h40>
<input type='hidden' id='wpm' value='$profilename'/>
<input type='button' onclick='ajaxFunction()' value='Comment' />
</form>
";
}
?>
I realize that is not the start of the php, but the rest is unimportant.
here is the code for ajaxprofilechat
$age = strip_tags($_GET['age']);
$wpm = $_GET['wpm'];
// Escape User Input to help prevent SQL Injection
$wpm = mysql_real_escape_string($wpm);
$chatname6 = ($_SESSION['username']);
$message6 = $_GET['site_message'];
$month6 = date("F");
$dayofmonth6 = date("d");
$year6 = date("Y");
$date10 = "$month6 $dayofmonth6 $year6";
$hours6 = date("g");
$min6 = date("i");
$sec6 = date("s");
$amorpm6 = date("A");
$time6 = "$hours6:$min6 $amorpm6";
if (strlen($age)>4)
{
mysql_connect("","","") or die($error);
mysql_select_db("") or die($error);
mysql_query("INSERT INTO guestbook VALUES ('','$wpm','$chatname6','$age','$date10','$time6')");
echo " <h80><b>Comment Posted</b></h80<p><p>";
}
else
{
echo " <h80><b>Your comment must be greater than four characters</b></h80><p>";
}
?>
Any help would be great. Thanks!
if you need to see my site to look at the error, here is a link to my profile page http://www.pearlsquirrel.com/profile.php?u=eggo
Guys I have literally tried everything that you have told me, and in every possible way. However, i am still encountering the same problem. Should I try to use the POST method of ajax instead of GET? Do you have any other suggestions? And thank you for the help so far.
Try $message6 = nl2br($_GET['site_message']);, then you don't have to worry about the \n or \r\n in your MySQL record since it will be stored as HTML <br /> and will display as intended during output. If you need to put it back in a textfield for edit, you'd simply use br2nl().
function br2nl($str)
{
return preg_replace('#<br\s*?/?>#i', "\n", $str);
}
try to wrap the POST data from the textarea with the nl2br() function
nl2br

How to verify the ownership of a website?

I want to create a tag to allow user to claim the ownership of his website.
How would I get the tag and the match it against the one I have generated.
The tag format will be <!--tag-->
how will I verify this tag if I am able to get the html using file_Get_contents("url");
it's just a string match.
<?php
$tagCode = "<!-- abc123 -->";
$pageContent = file_get_contents("http://www.somesite.com/");
if (strpos($pageContent, $tagCode) !== false) {
echo "it matched!";
}

Resources