HTML to send a pre-written email - php

I have put a website up on the internet, and I want to have a function where a user can enter their email address into a form and when submitted, they will receive a pre-set email.
There are other questions, but none of them seemed to help me.
I have basic code here:
<form action="send.php" method="post">
<input type="text" name="mail">
<input type="submit" value="send">
</form>
Here is the send.php
<?php
$mail = $_POST['mail'];
?>
<html>
<button>SEND</button>
</html>
Please excuse the fact that you have to press send twice, I will change that.
What I want to do, is send the user (email is represented by $mail) a pre written document via email, not as an attachment, but a the composition of the email.
I am unsure as to weather i can just use a word document, or there is complex code behind it.
Since i am only familiar with basic visual elements, please could you explain what parts of the code do what, as i may want to tweak it.
Thanks!

As Fred said, the best way to approach this is via PHP's mail function. When send.php runs, you can include PHP code like
mail($mail, "My Email Subject", "<html><b>Email Message</b></html>", $headers);
Where your header variable will have to contain all the necessary information to tell your client's mail software that the string message contains HTML that should be parsed. An example header setup is as follows, from CSS Trick's lovely example for this problem:
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Noting that you can replace the strip_tags call with whatever set emails you want. Then, you can replace my <html><b>Email Message</b></html> with whatever code you want to make a nicely formatted email.
Using PHP's mail() function also makes it so you do not need to click submit twice.

Related

VBS and Tinymce

So I have a question that some more experienced individuals may be able to shed some light on and steer me in the correct direction. Here is what I have and what I am trying to do.
I have tinymce up and working just fine on a php webpage, of course that is until I go to submit it.
I am trying to use tinymce as the text editor for an email body that I am inserting into a vbscript then calling wsh to execute the .vbs. Everything works fine when I really don't edit the text. Just typing in and submitting without any colored text or other formatting.
However, if I do anything to the text at all it causes the .vbs not to execute. Upon opening up the unexecuted file I see that the "#" character for example when tinymce inserts the span color is causing it to hang.
I have googled and not found this issue presented is there something that I am missing? Or is there a better way to go about sending html emails from a webpage based text editor?
As I said as long as I don't really want to edit the text it works but I could do that plainly with a simple textarea but that defeats the purpose of wanting to be able to produce formatted emails.
Any and all suggestions are greatly appreciated. Thank you in advance for your time and help.
E-Mail Example:
// Sending an email with PHP
$to = $email; // Send email to our user
$subject = 'Email Sent with PHP'; // Give the email a subject
$message = '
<html>
<head>
</head><body>
<h3>Hey!</h3>
</body>
</html>
'; // Our message above including the link
$headers = 'From:nobody#nowhere.com' . "\r\n"; // Set from headers
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers); // Send our email

Is there an e-mail template file like *.oft, which can be opened in all e-mail programs?

Currently I'm writing a web interface which is full of data. I would like to export this data as an e-mail template, so you can edit the e-mail afterwards.
Is there a format such as *.oft, which can be read by all email programs?
I know that there is such a function in HTML (<a href="mailto:[...]">). Since the e-mail text is very long and I want to attach files, this doesn't seems to be a good solution.
Can someone help me?
There's no such format as you're looking for. The internet engineers who design email protocols and format concern themselves with what goes out over the network, but not with what's internal to the machine. Email is highly interoperable when sending and receiving messages as a result, but there's no analogous standards body for internal API's. Sometimes there are de facto standards that arise from a imitating the behavior of popular piece of software, but that hasn't happened for the email feature you're looking for.
Maybe you mean .EML files? I remember that this file can be opened with nearly all e-mail clients such as Outlook, Thunderbird etc.
As far as I can tell you can go one of two ways:
1 - Use a prebuilt solution such as the one by these guys at postageapp.com their API looks pretty simple and does everything you want to achieve.
2 - More time consuming, but you can create your own templete using PHP. I would go for something along these lines. This is a very simple way of doing it. You could write your own CSS in the head and go nuts. Just remember that tables are king when it comes to HTML email :) Once the template has been written, you could just pass through the $content to fill it....
sender.php
<?php
$email = "Hello, \n";
$email.= "Very <strong>impressed</strong> with your email! \n\n";
$email.= "Faithfully\n";
$email.= "Mr Example\n";
$to = "me#me.me"; // <-- Set this as yours for testing...
$from = "someone#nice.me";
$subject = "An email";
$headers = "From: Someone nice <" . $from . ">\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= nl2br($email); // <---- if your email contains newline characters - it will convert them to <br />
$message .= '<br />www.example.com';
$message .= "</body></html>";
mail($to, $subject, $message, $headers);
// ADD LOTS OF PROTECTION IF NOT HARD CODING :)
?>
To extend this - if you for example use Gmail, then you could use Google's SMTP to send it - so that the email wouldn't say 'Sent on behalf of'.
Just create a email using HTML without CSS. Any data or binary can be passed through PHP and then emailed to the recipient.
To get you started, Create a file using fopen();. PHP has other functions that would allow you to edit, open, search&replace and even convert a file to a format of your choice. This is equivalent to a .oft file if not better, because all emails are be sent in text or a html equivalent regardless of the email client.
If you'd would like some more info on how to create this, let me know.

Best way to do html e-mail, sending via php?

I've never done html e-mail before, I've just set up php mail using http://www.postmarkapp.com
I was wondering how I would go about sending php mail as html?
Does anybody have any previews of a php page sending html e-mail I can look at to get the jist of how it works?
Currently I'm just putting text into a variable and sending it as a message, how is it done for html?
Regards
Do you still want to use Postmark to send emails?
In Postmark, you set the TextBody property to the text version of your message, and the HtmlBody property to the html version of it. It is good practice to always include both. Depending on whether your user's email client supports HTML or not, the appropriate message form is rendered. Read more on this here.
Edit: Added an example. I generally like splitting my string into separate lines so that I can indent it nicely like a real HTML file. Of course, if you used templates, that would make it so much better.
$htmlBody = "
<html>
<body>
Thank you for using our app!<br />
- Super Awesome App Team
</body>
</html>
";
http://phpmailer.worxware.com/
This is the best class I've found to send mail with PHP. It allows HTML formatting with alternate plain text part, as well as attachments. It also seems to filter out spam quite well when used for online forms.
Put the html code inside the variable where you need as you are doing while creating web pages and set the mail header to text/html.
$headers.= 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers.= "From: eaxfd#gmail.com \r\n";
$headers.= "Reply-To: eaxfd#gmail.com \r\n";
mail($to,$subject,$message,$headers);
You just have to include html headers in your call to the mail() function:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
mail($to, $subject, $message, $headers);

PHP sendmail() formatting not preserved

I am creating a web application for a client that has the ability to send out emails. I am using TinyMCE for my text editor, which works quite nicely. I am using sendmail() with PHP Swiftmailer to handle the actual sending of the email. Swiftmailer works nicely, as well.
The only problem that I am running into is that when I receive the email (in Gmail), the formatting is not rendered correctly. I am receiving the following in the body of my email:
<p>Oh Hello! This is a test <strong>message</strong>. Here is a link: Google.</p>\r\n<p> </p>\r\n<p> </p>\r\n<p> </p>\r\n<p>Line breaks!<br /> <br /> Shift breaks!</p>\r\n<p> </p>\r\n<p>Bye!</p>
The links render, and that's about it. What am I missing?
Thanks!
Check out the second and third code blocks in the documentation for how to set the HTML Content-Type...
http://swiftmailer.org/docs/header-parameterized
You need to send your e-mail in an actual HTML format. You are instead sending the plain text of the HTML. The links render because Google is nice and automatically links what it believes to be a valid URL.
I strongly recommend the Pear mail class. There are convenient functions for setting HTML and plain text message bodies.
What I would do in your case is something like this:
<?php
$headers = "From: Me <me#myemail.com>\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; //States it is HTML Content
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$subject = "This is my subject";
$message = <<<MESS
<p>Oh Hello! This is a test <strong>message</strong>. Here is a link: Google.</p>
<p> </p>
<p> </p>
<p> </p>
<p>Line breaks!<br /> <br /> Shift breaks!</p>
<p> </p>
<p>Bye!</p>
MESS;
if(mail("", $subject, $message, $headers) == True){
echo "Message Sent";
} else {
echo "Message NOT Sent"
}
?>
NOTE: GMAIL is REALLY picky about how it displays emails. Best to create a gmail account and test for yourself how it looks. Hotmail is similar

What to include in email headers, using php mail()?

When I want to send email, my codes look like these:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: me#localhost' . "\r\n";
// Email Variables
$toUser = 'you#something';
$subject = "Testing";
$body = '<html><head><title></title></head><body>
This is my picture <img src="http://domain.com/me.jpg">';
if(mail($toUser,$subject,$body,$headers)){
echo "Email sent!!";
}
Here are my questions:
Must I include the <html>, <head>, <title>... tags?
Can I use stylesheet? For example, <div style="..."> ?
I tested send the email to several accounts, if I include the <img> tag (for include image), it goes to junk mails, but if just plain text it goes to inbox. Anyone has any idea why it is? And how to solve it?
You should send your e-mail as multipart with a text/plain portion too.
That'll let people with text-only mail clients read it, and also count in your favour with any spam detection systems.
No, some mail clients (especially webmail) will strip out anything before <body>
You can use inline styles <div style=""> - using an external .css file is not recommended as it may not be loaded.
If you only have a bit of text and an image it looks a lot like spam. If you had more text it may get treated differently.
The problem with css in mail is that (again) is not standard in all mail services
here will find a complete descripction
http://www.campaignmonitor.com/css/

Categories