Passing HTML to PHP parameters? - php

I need to pass HTML to the following function
$response = good_mail_using_mandrill($to,$subject,$body,$from_name,$from_email,$MandrilluserAPIKey,$SubAccountID );
At the moment $body takes the entire HTML content from a MySQL field and sends the API call to Mandrill.
What I find is that certain HTML goes through but others dont. I am not sure what tags in the HTML cause this failure. Are there any restrictions on what content can be sent in the $body variable?
This is driving me nuts. Mandrill says that they are receiving "null" content.
Is there a way to read the API call at delivery?

Related

Is there a way to get the body of a Laravel Mail object to store in a variable?

I'm using the Laravel Mail API to generate mailables and send them out.
Everything is working fine with that.
However, I also want to log the email bodies in the DB.
In order to do that though, I need to first get the bodies of the emails I'm sending out.
In the Laravel docs, they have a snippet about how to output an email to the browser here:
https://laravel.com/docs/5.7/mail#previewing-mailables-in-the-browser
However, I don't want to output the email to the browser. I want to store it in a variable.
Is that possible? If so, how? Thanks.
Like the docs suggested, I tried something like the following:
$body = new App\Mail\InvoicePaid($invoice);
However, I got an error saying that I have to actually return the new Mail object in order for it to work (i.e., do what they do in the example code in the docs).
Previous paragraph tells you how to capture the rendered template:
$html = (new App\Mail\InvoicePaid($invoice))->render();
echo $html;

PHP prepare MAILTO with HTML

Im looking for a solution, to prepare a complete email for opening with a mail client
PHP is used for generating the link.
Here is the usefull part of my code.
$body = str_repalace('&amp','%26',$body);
$mailtarget = "mailto:".$adress."?cc=".$ccs."&Subject=".rawurlencode($subject)."&Body=".$body.
header("Location = $mailtarget");
This solution works fine, unless i use html in the mail, which is necessary (<br>,<hr>, <table>.
If used HTML sometimes nothing gets in Body, and sometimes just a part of the text.
When I mail the HTML with sendmail.exe, it works perfect. But this is not an option, since its required to add attachments manually to the mail.
Is there any way, to get this properly to work?
It's not possible to include HTML in body's mailto.
You can try to url_encode your HTML and set it as body in your mailto link, but it has a lot of risks of not working in many email clients.
From this answer, it's even defined in the RFC 2368 :
The special hname "body" indicates that the associated hvalue is the
body of the message. The "body" hname should contain the content for
the first text/plain body part of the message. The mailto URL is
primarily intended for generation of short text messages that are
actually the content of automatic processing (such as "subscribe"
messages for mailing lists), not general MIME bodies.
Try: &Body=".urlencode($body);
Use rawurlencode function to insert carriage return :
<?php
$body = '
First line
Second line';
header("location: mailto:emailadresse#domain.com?subject=Hello&body=".rawurlencode($body));
?>

PHP Mail Sending a dynamic PHP Contents

I'm Creating a weekly Updates to my clients, and I want to include the latest (News, Articles, Photos) in this mail. So I created "webmail.php" page that's been created Dynamically using MySql, contains all my updates I want to send to my clients, with heavy css and html contents.
I'm using this PHP code in my script
ob_start();
include ('webmail.php');
$content = ob_get_clean();
$message = $content;
mail($email,$subject,$message,$headers);
The problem is I'm facing (500 Internal Server Error). I'm sure that my webmail.php contains no errors and this problem happens because this page has been created Dynamically.
Any Idea to solve this problem?. Thanks
I think you're missing a point there... If webmail.php is dynamically generated (which means it actually contains your information), then you can read its contents using :
$news = file_get_contents("webmail.php");
and just send $news as your email body. However, if webmail.php actually generates the content (which means it produces it when passed to the PHP interpretor), then maybe you should consider using a function in this file instead :
webmail.php
function latest_news(){
// Gets news from database, put them into $news.
return $news;
}
Then, on your first page (sending the email) :
include_once("webmail.php"); // Get the function.
mail("recipient#address.tld", "Our latest news", latest_news());

Get all content from a file, including PHP code

I'm making a small CMS for practice. I am using CKEDITOR and is trying to make it avaliable to write something like %contactform% in the text, and then my PHP function will replace it with a contactform.
I've accomplished to replace the text with a form. But now I need the PHP code for the form to send a mail. I'm using file_get_contents(); but it's stripping the php-code.
I've used include(); to get the php-code from another file then and that works for now. I would like to do it with one file tho.
So - can I get all content from a file INCLUDING the php-code?
*UPDATE *
I'll try to explain in another way.
I can create a page in my CMS where I can write a header and some content. In the content I am able to write %contactform%.
When I get the content from the database I am replacing %contactform% with the content from /inserts/contactform.php, using file_get_contents(); where I have the form in HTML and my php code:
if(isset($_POST['submit'])) {
echo 'Now my form is submitted!';
}
<form method="post">
<input type="text" name="email">
<input type="submit" name="submit">
</form>
Now I was expecting to retrieve the form AND the php code active. But If I press my submit button in the form it's not firing the php code.
I do not wan't to show the php code I want to be able to use it.
I still have to guess, but from your update, I think you ultimatly end up with a variable, which contains the content from the database with %contactform% replaced by file_get_contents('/inserts/contactform.php').
Something like:
$contentToOutput = str_replace(
'%contactform%',
file_get_contents('/inserts/contactform.php'),
$contentFromDatabase
);
If you echo out that variable, it will just send it's content as is. No php will get executed.
Though it's risky in many cases, if you know what you're doing you can use eval to parse the php code. With mixed code like this, you maybe want to do it like the following.
ob_start();
eval('; ?>' . $contentToOutput);
$parsedContent = ob_get_clean();
$parsedContent should now contain the results after executing the code. You can now send it to the user or handle it whatever way you want to.
Of course you'll have to make sure that whatever is in $contentToOutput is valid php code (or a valid mixture of php with php-tags and text).
Here is a link to the symfony Templating/PhpEngine class. Have a look at the evaluate method to see the above example in real code.
yes...
$content = file_get_contents( 'path to your file' );
for printing try
echo htmlspecialchars( $content );
From reading the revised question, I think the answer is "You can't get there from here." Let me try to explain what I think you will encounter.
First, consider the nature of HTTP and the client/server model. Clients make requests and servers make responses. Each request is atomic, complete and stateless, and each response is complete and usually instantaneous. And that is the end of it. The server disconnects and goes back to "sleep" until the client makes a new request.
Let's say I make a request for a web page. A PHP script runs and it prepares a response document (HTML, probably) and the server sends the document to my browser. If the document contains an HTML form, I can submit the form to the URL of the action= script. But when I submit the form, I am making a new request that goes back to the server.
As I understand your design, the plan is to put both the HTML form and the PHP action script into the textarea of the CKeditor at the location of the %contactform% string. This would be presented to the client who would submit the form back to your server, where it would run the PHP script. I just don't think that will work, and if you find a way to make it work, you're basically saying, "I will accept external input and run it in PHP." That would represent an unacceptable security exposure for me.
If you can step back from the technical details and just tell us in plain language what you're trying to achieve, we may be able to offer a suggestion about the design pattern.

HTML in php mail function

I have a contact form on my website so users can send me an email but I have run into a problem.
I want to use an HTML link inside the email and I also want the content the user is sending to me to be formated how they would like it.... let me explain.
If a user sends this:
Hello World!
Isnt it a great Day?
without using headers to enable html, then it says formated like that when it reaches me.
If I use headers (MIME) to enable html, to also include a link in the email (which I would like to do), then it reaches me as:
Hello World!Isnt it a great Day?
How can I include html, and also keep the email formatted properly?
Thanks
And hopefully all this makes sense :S
Use nl2br on your message - http://php.net/manual/en/function.nl2br.php
It will replace all newlines with HTML <br>
$message = nl2br($message, false);
The second parameter *is_xhtml* is optional if you want to send an HTML email instead of XHTML

Categories