drupal_mail() HTML - email

I'm trying to send out a simple email with some HTML, drupal_mail() is sending out the email, but all HTML seems to be removed.
$body = '<div style="width: 600px; margin: 0px auto">';
$body .= '<img align="center" src="' . $url . '" style="max-width: 600px; height: auto; margin: 0px auto">';
$body .= '<p style="margin: 30px 0px 0px 0px; line-height: 18px">' . nl2br($message) . '</p>';
$body .= '</div>';
params = array(
'subject' => 'An e-card from HKUST',
'body' => $body
);
if(drupal_mail('ecard', 'ecard_email', $single_email, language_default(), $params, $yourEmail, TRUE)) {
echo 'Send';
}
function ecard_mail($key, &$message, $params) {
$headers = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
foreach($headers as $key => $value) {
$message['headers'][$key] = $value;
};
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
}
Am I still missing something here?
EDIT: When I check the email in gmail, I can see this:
X-PHP-Originating-Script: 381:system.mail.inc
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8Bit
X-Mailer: Drupal
So I guess the HTML is filtered by drupal before sending

Related

Bootstrap 4 & Phpmailer form upload file

I think it's quite simple to realize but i really dont know where to start. What i would like is an option in the form where people can add an image or other file.
Ive added this in my bootstrap form:
<!--attachments-->
<div class="form-group">
<label for="attachment">Attachment</label>
<input type="file" class="form-control-file" name="attachmentFile" id="attachmentFile">
</div>
So far so good i guess. But it wont send because there has to be something added in my phpmailer / send.php file. This is the script which i use now:
<?php
require 'PHPMailerAutoload.php';
// Controleren of het formulier is verzonden dmv submit
if (isset($_POST['submitted'])) {
$errors = array(); // Initialize error array.
// Geef hier zoveel mogelijk vereiste velden voor validatie
//Add attachment
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']
['tmp_name'],$_FILES['attachmentFile']['name']);
}
// Controleer naam
if (!preg_match("/^[a-zA-Z]+[a-zA-Z[:space:]]{2,}$/", $_POST['naam'])) {
$errors[] = 'Naam';
}
// Controleer adres
if (!preg_match("/^[a-zA-Z[:space:]]{1,}$/", $_POST['straat'])) {
$errors[] = 'Adres';
}
// Controleer huisnummer
if (!preg_match("/^[0-9]{1,4}$/", $_POST['huisnummer'])) {
$errors[] = 'Huisnummer';
}
// Controleer postcode
if (!preg_match("/^[0-9]{4}$/", $_POST['postcode'])) {
$errors[] = 'Postcode (Cijfers)';
}
// Controleer postcode letters
if (!preg_match("/^[a-zA-Z]{2}$/", $_POST['letters'])) {
$errors[] = 'Postcode (Letters)';
}
// Controleer woonplaats
if (!preg_match("/^[a-zA-Z]+[a-zA-Z[:space:]]{2,}$/", $_POST['woonplaats'])) {
$errors[] = 'Woonplaats';
}
// Controleer Kengetal
if (!preg_match("/^[0]{1}[0-9]{1,3}$/", $_POST['kengetal'])) {
$errors[] = 'Kengetal';
}
// Controleer telefoonnummer
if (!preg_match("/^[0-9]{6,8}$/", $_POST['telefoon'])) {
$errors[] = 'Telefoonnummer';
}
// Controleer e-mail adres
if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*#([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,})$/", $_POST['email'])) {
$errors[] = 'E-mail adres';
}
// Controleer tekstvak
if (empty($_POST['bericht'])) {
$errors[] = 'Bericht';
}
if (empty($errors)) {
// Voer hier de gegevens in
$mailFrom = "myEmail";
$mailTo = "myEmail";
$senderNaam = $_POST['naam'];
$senderEmail = $_POST['email'];
$subject = 'mySubject';
$okMessage = '<p>blablabla</p>';
$errorMessage = '<p>blablabla</p>';
$emailText .= "Naam: " . $_POST['titel']." ". $_POST['naam']. "\n";
$emailText .= "Bedrijfsnaam: " . $_POST['bedrijfsnaam']. "\n";
$emailText .= "Adres: " . $_POST['straat']." ". $_POST['huisnummer']." ". $_POST['toevoeging']. "\n";
$emailText .= "Postcode: " . $_POST['postcode']." ". $_POST['letters']. "\n";
$emailText .= "Woonplaats: " . $_POST['woonplaats']. "\n";
$emailText .= "Telefoonnummer: " . $_POST['kengetal']." ". $_POST['telefoon']. "\n";
$emailText .= "E-mailadres: " . $_POST['email']. "\n";
$emailText .= "Bericht: " . $_POST['bericht']. "\n";
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'SMTP SERVER'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'User'; // SMTP username
$mail->Password = 'Pass'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->Sender = $mailFrom;
$mail->SetFrom($senderEmail, $senderNaam);
$mail->AddReplyTo($senderEmail, $senderNaam);
$mail->Subject = $subject;
$mail->MsgHTML($emailText);
$mail->AddAddress($mailTo, $mailTo);
$mail->addAttachment($uploadfile, 'My uploaded file'); **// ADDED THIS HERE**
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = "<table>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Naam:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['titel']." ".$_POST['naam']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Bedrijfsnaam:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['bedrijfsnaam']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Adres:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['straat']." ".$_POST['huisnummer']." ".$_POST['toevoeging']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Postcode:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['postcode']." ".$_POST['letters']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Woonplaats:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['woonplaats']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Telefoonnummer:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['kengetal']." ".$_POST['telefoon']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>E-mailadres:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['email']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Bericht:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['bericht']."</td></tr>
</table>";
$mail->AltBody = $emailText;
if(!$mail->send()) {
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
} else {
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
} else {
echo $responseArray['message'];
}
}
?>
Also added this to <form> enctype="multipart/form-data"
Edit #2: I also have a contact.js file with this:
data: $(this).serialize(),
I changed it in this:
data: new FormData(this),
Because i read somewhere that this could be the problem, but it didnt make sense.
Alright, the form is sending with attachments! There is only one little thing i hope someone can help me with that.. more about this later on. First the code.
In contact.js - I had to change data: $(this).serialize(), into data: new FormData(this),
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "send.php";
$.ajax({
type: "POST",
url: url,
data: new FormData(this),
cache : false,
contentType : false,
processData : false,
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset(); //Remove this line if form should not reset after submit
$('body, html').animate({scrollTop:$('#contact-form').offset().top}, 'fast'); //Remove this line if form should not scroll to top of the form after submit
}
}
});
return false;
}
})
});
Myform.html - 2 things: this enctype="multipart/form-data" in <form>, and this
<!--attachments-->
<div class="form-group">
<label for="attachment">Attachment</label>
<input type="file" class="form-control-file" name="attachmentFile" id="attachmentFile">
</div>
somewhere you want in the form.
Finally put this in your phpmailer file:
//Add attachment
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']);
}
I did it below $mail->AddAddress($mailTo, $mailTo);
So far how to fix this. There is still one problem left i refer to in the beginning.
Without data: new FormData(this), I still get my okMessage (with markup) when i hit send and the form stays at the same page.
With data: new FormData(this), it goes to a new page and just give black on white plain text (without markup).
So its like FormData is overruling something. Hope someone can help me out with this.
EDIT: Seems i have fixed this too. Place this..
cache : false,
contentType : false,
processData : false,
..below data: new FormData(this),

How to remove payment method from woocommerce admin email

Cannot find how to remove payment method from woocommerce admin email.
Searched through the files for Payment Method, but no luck
First of all, if you haven't done so, copy your woocommerce template files to your themes root as described in http://docs.woothemes.com/document/template-structure/.
Then, open a file that is responsible for building the email template. In my case it was (after copying it over) /wp-content/themes/MY_THEME/woocommerce/emails/admin-new-order.php
Find the following lines of code
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
?><tr>
<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
</tr><?php
}
}
?>
</tfoot>
And add a condition to check if one of the labels contains Payment Method, like so
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
if ( $total['label'] != 'Payment Method:' ){
?><tr>
<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
</tr><?php
}
}
}
?>
</tfoot>
You can use this for other fields also

How do I get a current facebook access_token when accessing group events with graph api?

I have this code for accessing and displaying Facebook events from a Facebook Group. If you use a current access_token, this works but the access_tokens are only valid for an hour or so. What code do I need to add to programatically get a current access_token each time the page loads?
I have looked at the facebook advice and it puts my page into a permanant loop.
<?php
header ('Content-type: text/html; charset=utf-8');
$limit = 5;
$group_id = '68663437095';
$url1 = 'https://graph.facebook.com/'.$group_id;
$des = json_decode(file_get_contents($url1));
$url2 = "https://graph.facebook.com/68663437095/events?access_token=CAACEdEose0cBAORQs8KFqo6Fw1ZA78alt8CgH3pyKgEVWdbsxWmBhjwVqOOSqst51fb1ZCHK0Dtz8CmTDCZByqoJMoaXcEU6qjN4IO1JWExSwuHzVRmNU5JEkzpdcSuhPd8StyYSObAhXU4DKngmnHtvF018wZAmsg8GEFXNatcZAZBBMbJmsUOvwSOa7DubIZD";
$data = json_decode(file_get_contents($url2));
?>
<style type="text/css">
.wrapper {
width:200px;
border:1px solid #ccc;
font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
background-color: black;
float:left;
}
.top {
margin:5px;
border-bottom:2px solid #e1e1e1;
float: left;
width:290px;
}
.single {
margin:5px;
border-bottom:1px dashed #e1e1e1;
float:left;
}
.img {
float:left;
width:60px;
text-align:center;
margin:5px 5px 5px 0px;
border-right:1px dashed #e1e1e1;
}
.text {
width:220px;
float:left;
font-size:10px;
}
a {
text-decoration: none;
color: #3b5998;
}
</style>
<div style="width: 250px; padding-bottom:15px;"><br/>
<div style="width: 190x; margin-left:15px;">
<a target="_parent" style="font-size:40px;" href="http://www.facebook.com/groups/fumcbixby/"> <?=$des->name?></a>
<div style="width:100%; margin: 5px">
<?=$des->description?>
</div>
</div><br/>
<?
$counter = 0;
foreach($data->data as $d) {
if($counter==$limit)
break;
?>
<div style="width: auto; background-color:white; margin-left:15px; margin-bottom:15px; padding:5px">
<div style="float: left; width:120px;">
<img src="https://graph.facebook.com/<?= $d->id?>/picture">
</div>
<div>
<h2>Name <?= $d->name?></h2>
<h3>Location <?= $d->location?></h3>
<p>Start<?= date('F j, Y H:i',strtotime($d->start_time))?></p>
<p>End<?= date('F j, Y H:i',strtotime($d->end_time))?></p>
Visit Event
</div>
</div>
<?
$counter++;
}
?>
</div>
Get Extended Access Token which will be valid for 60 days
https://graph.facebook.com/oauth/access_token?
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN
Put APP_ID, APP_SECRET & EXISTING_ACCESS_TOKEN in the above url & make a HTTP request.
HTTP request will return JSON Array. Then you can easily parse Extended Access Token
Have a look at this question....
How to renew/extend facebook access tokens with PHP?

Find value in a html file using HTML::TreeBuilder

Below is my data in html file. I want to find the values in the html file using "HTML::TreeBuilder"
<table id="stats" cellpadding="0" cellspacing="0">
<tbody>
<tr class="row-even">
<td class="stats_left">Main Domain</td>
<td class="stats_right"><b>myabcab.com</b></td>
</tr>
<tr class="row-odd">
<td class="stats_left">Home Directory</td>
<td class="stats_right">/home/abc</td>
</tr>
<tr class="row-even">
<td class="stats_left">Last login from</td>
<td class="stats_right">22.32.232.223 </td>
</tr>
<tr class="row-odd">
<td class="stats_left">Disk Space Usage</td>
<td class="stats_right">30.2 / ∞ MB<br>
<div class="stats_progress_bar">
<div class="cpanel_widget_progress_bar" title="0%"
style="position: relative; width: 100%; height: 100%; padding: 0px; margin: 0px; border: 0px">
</div>
<div class="cpanel_widget_progress_bar_percent" style="display: none">0</div>
</div>
</td>
</tr>
<tr class="row-even">
<td class="stats_left">Monthly Bandwidth Transfer</td>
<td class="stats_right">0 / ∞ MB<br>
<div class="stats_progress_bar">
<div class="cpanel_widget_progress_bar" title="0%"
style="position: relative; width: 100%; height: 100%; padding: 0px; margin: 0px; border: 0px">
</div>
<div class="cpanel_widget_progress_bar_percent" style="display: none">0</div>
</div>
</td>
</tr>
</tbody>
</table>
How can I find "Disk Usage space" value using "HTML::TreeBuilder". I have many tds with same classes from above code,
Find the <td> with the matching content, in this case "Disk Space Usage" and then find the next <td>.
Once you have an element tree:
my $usage = $t->look_down(
_tag => 'td',
sub {
$_[0]->as_trimmed_text() =~ /^Disk Space Usage$/
}
)->right()->as_trimmed_text();
You may want to wrap that in an eval block in case look_down doesn't find a match.
The tree navigation methods in HTML::Element are a key part of making effective use of HTML::TreeBuilder effectively.
Mohini asks, "why doesn't this work?"
(formatting added by me)
use strict;
use warnings;
use HTML::TreeBuilder;
my $tree = HTML::TreeBuilder->new_from_file( "index.html");
my $disk_value; my $disk_space;
for ( $tree->look_down( _tag => q{tr}, 'class' => 'row-odd' ) ) {
$disk_space = $tree->look_down(
_tag => q{td},
'class' => 'stats_left'
)->as_trimmed_text;
if ( $disk_space eq 'Home Directory' ) {
$disk_value = $tree->look_down( _tag => q{td}, 'class' => 'stats_right' )
->right()
->as_trimmed_text();
}
}
print STDERR "my home value is $disk_space : $disk_value\n";
look_down starts from the root node you invoke it from, and looks down the element tree (these trees grow upside down) and returns either the list of matching nodes or the first matching node, depending on context.
Since all calls to look down are on tree, you repeatedly find the same nodes each time through the loop.
Your loop should look something more like this:
my %table_stuff;
for my $odd_row ( $tree->look_down( _tag => q{tr}, 'class' => 'row-odd' ) ) {
$heading = $odd_row->look_down(
_tag => q{td},
'class' => 'stats_left'
);
$table_stuff{ $heading->as_trimmed_text() } = $heading->right()->as_trimmed_text();
}
This populates a hash with table elements.
If you only want the one value, don't use a loop at all. look_down already acts as a loop.
my $heading = $t->look_down(
_tag => 'td',
sub {
$_[0]->as_trimmed_text() =~ /^Home Directory$/
}
);
my $value = $heading->right();
# Now $heading and $value have HTML::Element nodes that you can do whatever you want with.
my $disk_value = $value->as_trimmed_text();
my $disk_space = $heading->as_trimmed_text();

Display a public Facebook event on a web page

How can I show information from a specifiec Facebook event on a webpage?
Things like: time, location, created by, more info, and also the image used as thumbnail.
Even if I didn't created that event.
Someone else created it.
I just have the url of that Facebook event and I can find the user id of the person who created it.
I found this page:
http://www.codeofaninja.com/2011/07/display-facebook-events-to-your-website.html
But it's not the same as I want, and I couldn't figure it out how to do it.
Any help is much appreciated.
What languages do you program in? say php or javascript, and i will post a quick sample?
You can use one of the many SDK's but for me the 2 that are easiest to use are the php-sdk and javascript-sdk.
https://developers.facebook.com/docs/sdks/
Using either you can just send a request to the graph api to retrieve the info about the event by its id.
https://developers.facebook.com/docs/reference/api/
https://developers.facebook.com/docs/reference/api/event/
EDIT: Sample assumes php-sdk 3.1.1 is installed and initiated. Will show 8 of the most recent events added to page or app. App Access Token is required.
<?php
$pageid='YourPageId.or.AppId';
$MEevents = $facebook->api('/'.$pageid.'/events?access_token='.$app_access_token.'&fields=id,name,from,start_time,location,end_time&limit=8');
echo '<div align="center" style="border: 0px solid; width: 100%;">';
foreach ($MEevents as $key=>$value) {
$i=1;
foreach ($value as $fkey=>$fvalue) {
if($fvalue[id]==h){
}else{
$i++;
$whofrom = $fvalue[from];
$whofromname = $whofrom[name];
$whofrompic = $whofrom[id];
echo '<div title="'.$fvalue[name].'" style="vertical-align: top; border: 1px inset; width: 700px; min-height: 80px; margin: 2px;">';
echo '<div id=""></div>';
echo '<div style="margin: 4px; padding: 3px; text-align: left;">';
echo '<img src="https://graph.facebook.com/'.$fvalue[id].'/picture" style="float: left; clear: left; margin: 5px;"> ';
echo ''.$fvalue[name].'<br />';
echo 'Location: '.$fvalue[location].'<br />';
echo 'Starts '.nicetime($fvalue[start_time]).' - Ends '.nicetime($fvalue[end_time]).'<br />';
echo '</div>';
echo '</div>';
}
}
}
echo '</div>';
?>