how to send email with graphic via php - php

I would like to send HTML email with graphic elements included. I have no idea to attach garaphics to this email.

You probably don't want to do an inline attachment by hand, it's far easier, and less error prone to use a library, like PHPMailer.
It can attach the inline images, or if you give it some HTML code, it will attach the images by itself and modify the code to display them.

You can try Swift Mailer

I'm not going to bore you with a mediocre explanation here so instead let me link to this great tutorial over at Sitepoint which explained it to me in plain English! - advanced-email-php

The short version is that you are probably best off creating a HTML formatted messages, and using the header parameter of the php mail function.
$headers = "From: sender#example.com\n" .
"MIME-Version: 1.0\n" .
"Content-type: text/html; charset=iso-8859-1";
mail(to#example.com, 'subject line', 'your message text <strong>with HTML in it</strong>', $headers);
The sitepoint.com article referenced by Jimmy provides an excellent and complete description of your options.

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

PHP - auto-download image in HTML email

I have a page which is sending an HTML email with the following PHP code:
$message = $body;//Create message body
$sender = $sender;//Create sender
$to = $customer_email;//Set who the email is being sent to
$subject = 'Quote - '.$_SESSION['quote_number'];//Subject for email
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";//Content-type header for mail() PHP built-in function
$headers .= 'To: '.$to . "\r\n";//To header for mail()
$headers .= 'From: '.$sender . "\r\n";//From header for mail()
$mail = mail($to, $subject, $message, $headers);//Send Email
Here is the part of the body with the image:
<img src="some_img.gif" alt="Title" width="400" height="55" align="left"/>
Right now when the email is sent, the image is not automatically downloaded, you have to right click it and download it. Is there a solution to automatically download this image within the mail() PHP function? If not, what is the easiest way I could do it?
I have done some research on this already, I have already looked at over 4 articles, here are a few that did not make sense to me, so I have not used the code in them. They are also pretty outdated.
How to embed images in email
Embed images for use in email message using PHP?
I am not interested in really complex code, for me I cannot stand classes and things like that where I just plain don't understand them.
Here is what you need:
<img src="data:image/gif;base64,<BASE64_ENCODED_IMG>" alt="Title" width="400" height="55" align="left"/>
<BASE64_ENCODED_IMG> should be the result of encoding the source binary of some_img.gif into Base64
You can do it on the fly with PHP: base64_encode(file_get_contents('/path/to/some_img.gif'));
Or get it done by an online service such http://www.base64-image.de/ and copy&paste the results.
You can find further info regarding this in Embedding Base64 Images
Edit: This may be obvious but... it is not possible to force the mail client to display images. But some clients just avoid to download them, so in this case this is a valid workaround.

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.

Sending HTML Email Using PHP

I am trying to send html email using php. it is working fine on gmail, yahoo etc. but on my cpanel (Horde Email Client) the text is comming as an attachment and it is not showing any body.
These are the headers i am using
$headers = "From: " ."XYZ<noRelpy#xxx.com>" . "\r\n";
$headers .= "Reply-To: ". "noRelpy#xxx.com" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=us-ascii" . "\r\n";
I have also tried changing the charset to iso-8859-1 but still no sucess
Sending emails is hard, and fraught with serious security problems (mostly related to spam). It takes years to write code that sends email reliably.
Instead of trying to do it yourself, this is a place where a third party library makes sense.
We have been using PHPMailer for years: http://code.google.com/a/apache-extras.org/p/phpmailer/
It is difficult to write custom code that sends HTML e-mail, especially ones with embedded images or attachments. I have written a class to do this, but once you do, you realise there are a bunch of other features you need.
Using a third-party library really is a better choice here if your core product is not a mailing library. PHPMailer is good, another good option is
swiftmailer
Good luck!

PHP email style

So on a website I am working on there is an automated email sent when you register. When it sends it shows the css.
$body '<span style="color:#444444;">Header</span>';
I also tried this code that I found online that had style=\"asdas\" then used a command to remove the slashes but that didn't work either. Is there a simple php code that will just simply embed the html in the email?
First, make sure you put the $body = as #JamWaffles suggests.
Next, are you setting your headers correctly?
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
You can find a pretty detailed tutorial here: http://css-tricks.com/sending-nice-html-email-with-php/
It shows importance of headers, HTML vs txt email and user desires. It should be a good starting point for you.
You can also add a tld check in your code and assume they would have an html capable email client/web interface(gmail/yahoo/live etc) or you can just ask them to provide you with this information and then send them the emails in the format they have asked you to.

Categories