PHP - auto-download image in HTML email - php

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.

Related

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.

how to send embed images in email using php and html

i know this question is asked several time and it is basic question but i searched last three days but i am still unable to send images in email.please help how can i embed images in my email that is which headers should i include in headers
i write the following code.
$to = "example#gmail.com";
$subject = "new account created";
$body = "New account has been created
<img border=’0' src=’http://www.example.com/myimages/track.php?MID=1111&SID=2222' width=’1' height=’1'>
" ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: admin#gmail.com";
$neww=mail($to,$subject,$body,$headers);
The reason you can't see your images in email is most email readers stript them out since they can be used for verifying valid email addresses which is a common tactic used by spammers. Your code screams tracking which only proves the point.
There is no way around this as the image filtering is done by the email clients and cannot be controlled by any code you write.
Check out this thread.
I posted a solution whereby you could use Mozilla Thunderbird to fetch the html image code for you which you can place in your script. The image will be embedded and would depend on no links or external content.
Look for my answer with the big screenshot of a lizard.
But, even despite that, users can still block images of all kinds. But emebedded images might be considered more acceptable than linked content - it depends on the client as to how images are handled.

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);

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/

how to send email with graphic via 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.

Categories