Send attachment with Sendmail - php

I want to send an attachment with PHPmailer, but when I add the 'addattachment' string, the mail is being send in plain text. This is my code:
$headers .= "AddAttachment('downloads/file.pdf','file.pdf')\n";
$headers .= "Reply-To: Contoso <noreply#contoso.com>\n";
$headers .= "From: Contoso <info#contoso.com>\n";
$headers .= "Organization: Contoso \n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=UTF-8\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-Mailer: PHP". phpversion() ."\n" ;
mail($to, $subject, $body, $headers,$param);
So the script is working without the 'AddAttachment' rule. How can I add an attachment succesfully?

Here is a tutorial how to send an attachment.
http://webcheatsheet.com/php/send_email_text_html_attachment.php#attachment
Its not enough to set only the header. You have to add the attachment as base64 encoded tring to your message.
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
Perhaps its much easier to use a lib with do the work something like PHPMailer or Swift Mailer what i prefer.
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

Related

PHP Code to send html email with attachment to Iphone Mail

I have a website contact form that sends HTML email with photo attachments as an email to Iphone mail application. I am receiving the following error message on my Iphone:
"This message cannot be displayed because of the way it is formatted. Ask the sender to send it again using a different format or email program. multipart/mixed".
Is this has something to do with email content-type and how to fix it in my PHP code.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$sender_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message_body));
$sentMail = mail($recipient_email, $subject, $body, $headers);

Yahoo showing blank email body for php 5.4 [duplicate]

I'm using PHP to send out a multipart/mixed message (plain text, html and attachments). However, whilst it works for most accounts, Yahoo, GMail and Sky seem to show blank emails. Where as everything else seems to display the email. ANY HELP WOULD BE GREATLY APPRECIATED!
My headers are
$headers .= "Content-Type: multipart/mixed; boundary=\"mixed-" . $random_hash . "-mixed\"\n";
$headers .= "MIME-Version: 1.0\n";
And the content is;
--mixed-7df05b31-mixed
Content-Type: multipart/alternative; boundary="alt-7df05b31-alt"
--alt-7df05b31-alt
Content-Type: text/plain; charset=utf-8
Hello how are you? I am just checking the mailing function.
Hopefully this will work!
Cheers.
--alt-7df05b31-alt
Content-Type: text/html; charset=utf-8
<div style="font-family:Arial, Helvetica, sans-serif; font-size: 10pt;">
<div>
Hello how are you? I am just <b>checking</b> the mailing function.<br><br>
Hopefully this will work!<br><br>
Cheers.</div></div>
--alt-7df05b31-alt--
--mixed-7df05b31-mixed
Content-Type: text/plain; name="abc.txt"
Content-Disposition: attachment; filename="abc.txt"
Content-Transfer-Encoding: base64
SEVMTE8gSlVTVCBURVNUSU5HIC4uLiA=
--mixed-7df05b31-mixed--
It may be an artifact from pasting, but try removing the blank space at the end of each boundary. (Highlight the text and you'll notice the boundaries have an extra space, but the closing boundaries-- do not)
Please verify whether your code is look like the following because the following code is working fine for me.
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $msg."\r\n\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-Type:application/html; name=\"".$filename."\" \r\n"; // use different content types here
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$uid."--";
Don't build your own MIME messages. use Swiftmailer or PHPMailer to do it for you.

send html email using php deliver message as html code

iam sending html message contains table
when i recive message on gmail,hotmail,yahoo account it works fine but when receiving on other client
ie Microsoft outlook it resolve it as html code !!
here is my email headers
'MIME-Version: 1.0' . "\r\n" .
'Content-Type: text/html;charset= UTF-8' . "\r\n" .
'From: me <me#client.com>'
I Always use this function ,and it helps
function sendHTMLemail($HTML,$from,$to,$subject,$fromname)
{
$headers = "From: ".$fromname." <".$from.">\r\n";
$headers.= "Reply-To: ".$fromname." <".$from.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$boundary = uniqid("HTMLEMAIL");
// First we be nice and send a non-html version of our email
$headers .= "Content-Type: multipart/alternative;".
"boundary = $boundary\r\n\r\n";
$headers .= "This is a MIME encoded message.\r\n\r\n";
$headers .= "--$boundary\r\n".
"Content-Type: text/plain; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode(strip_tags($HTML)));
// Now we attach the HTML version
$headers .= "--$boundary\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($HTML));
// And then send the email ....
mail($to,$subject,"",$headers);
}
I know this isn't an answer for your question, but I suggest using a mailing library, that will allow you to send mails much more easily, and supports features such as attachments, authentication, etc.
I recommend SwiftMailer which works great, is simple and well documented.

Blank Multipart email

I'm using PHP to send out a multipart/mixed message (plain text, html and attachments). However, whilst it works for most accounts, Yahoo, GMail and Sky seem to show blank emails. Where as everything else seems to display the email. ANY HELP WOULD BE GREATLY APPRECIATED!
My headers are
$headers .= "Content-Type: multipart/mixed; boundary=\"mixed-" . $random_hash . "-mixed\"\n";
$headers .= "MIME-Version: 1.0\n";
And the content is;
--mixed-7df05b31-mixed
Content-Type: multipart/alternative; boundary="alt-7df05b31-alt"
--alt-7df05b31-alt
Content-Type: text/plain; charset=utf-8
Hello how are you? I am just checking the mailing function.
Hopefully this will work!
Cheers.
--alt-7df05b31-alt
Content-Type: text/html; charset=utf-8
<div style="font-family:Arial, Helvetica, sans-serif; font-size: 10pt;">
<div>
Hello how are you? I am just <b>checking</b> the mailing function.<br><br>
Hopefully this will work!<br><br>
Cheers.</div></div>
--alt-7df05b31-alt--
--mixed-7df05b31-mixed
Content-Type: text/plain; name="abc.txt"
Content-Disposition: attachment; filename="abc.txt"
Content-Transfer-Encoding: base64
SEVMTE8gSlVTVCBURVNUSU5HIC4uLiA=
--mixed-7df05b31-mixed--
It may be an artifact from pasting, but try removing the blank space at the end of each boundary. (Highlight the text and you'll notice the boundaries have an extra space, but the closing boundaries-- do not)
Please verify whether your code is look like the following because the following code is working fine for me.
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $msg."\r\n\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-Type:application/html; name=\"".$filename."\" \r\n"; // use different content types here
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$uid."--";
Don't build your own MIME messages. use Swiftmailer or PHPMailer to do it for you.

How do I send emails with Arabic content via PHP's mail function?

I'm having a challenge with sending emails with arabic content using PHP's mail function. Let's say I have this simple arabic string:
بريد
I've tried several ways to utilize the headers, but the emails content all still end up with something like: X*X1X(X1Y X/. However, the email subject is correctly encoded if I use arabic characters (thanks to the base64_encode, see function below)
Here's one of the email functions I've tried
function sendSimpleMail($to,$from,$subject,$message) {
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'To: '.$to ."\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'Content-type: text/plain; charset=UTF-8; format=flowed' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'."\r\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=',$message, $headers);
}
Any suggestions on alternative ways to achieve this goal?
Unfortunately, 8bit encoding is not reliable in e-mail. Many mail transport agents will remove the top bit of every byte in the mail body. بريد is "\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF" in UTF-8 bytes; remove the top bit from those bytes and you get ASCII "X(X1Y\nX/".
The way to get non-ASCII characters into a mail body is to set Content-Transfer-Encoding to either base64 or quoted-printable, and the encode the body with base64_encode or quoted_printable_encode, respectively.
(quoted-printable is better if the mail is largely ASCII as it retains readability in the encoded form and is more efficient for ASCII. If the whole mail is Arabic, base64 would probably be the better choice.)
$boundary = uniqid(rand(), true);
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\n";
$headers .= "This is a MIME encoded message.\n\n";
$headers .= "--$boundary\n" .
"Content-Type: text/plain; charset=UTF-8 \n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($plaintext));
$headers .= "--$boundary\n" .
"Content-Type: text/html; charset=ISO-8859-1\n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($msg));
$headers .= "--$boundary--\n" .
mail($address, $subject, '', $headers);
This one works for me.
Try this
$headers .= 'From: =?UTF-8?B?'.base64_encode($from). "\r\n";
Your code works for me as-is.
Are you sure that $message contains a valid UTF-8 string?

Categories