Email with special characters to Tumblr - php

I'm using a PHP script to email a posting to Tumblr and Posterous. For Posterous special characters are showing up in the posting but for Tumblr it don't.
(In general Tumblr does support special character - I tried it out with emailing from Gmail)
So what could be the problem?
Here my PHP header:
$headers .= "MIME-Version: 1.0\r\n"
."Content-Type: multipart/alternative; boundary=\"PHP-mixed-$bound_text\"\r\n";
$message = "--PHP-mixed-$bound_text\r\n"
."Content-Type: multipart/related; type=\"text/html\"; boundary=PHP-mixed2-$bound_text\r\n\r\n"
."--PHP-mixed2-$bound_text\r\n"
."Content-Type: text/html; charset=\"utf-8\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n";
I already tried several charsets and Content-Transfer-Encoding combinations without any different result.
Any ideas are welcome.
Michael

I looked into how my GMail account sent it and found out it used windows-1252 for the charset and the text is base64 encoded (this may not be case in using other GMail accounts). The email I sent below was successfully posted by tumblr and I used the function utf8_decode to generate the correct base64 string.
$data = wordwrap(base64_encode(utf8_decode($stringWithSpecialCharacters)));
The raw email message is below:
To: myuploademail#tumblr.com
Subject: tumblr post from sudocode
from: omime class <script#sudocode.net>
MIME-Version: 1.0
Content-type: multipart/alternative;
boundary=omime_1308940625_c0d4e36fd504619028804e8b23ceb12a
--omime_1308940625_c0d4e36fd504619028804e8b23ceb12a
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: base64
38TW+eLq7vT76//mP9nCys4=
--omime_1308940625_c0d4e36fd504619028804e8b23ceb12a
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: base64
38TW+eLq7vT76//mP9nCys4=
--omime_1308940625_c0d4e36fd504619028804e8b23ceb12a--
I used my omime class to send the email and here's the code I used:
$email = new omime('alternative');
$email->attachText('ßÄÖùâêîôûëÿæœÙÂÊÎ');
$email->attachHTML('ßÄÖùâêîôûëÿæœÙÂÊÎ');
$email->send('mysecretuploademail#tumblr.com', 'test tumblr post', 'from: omime class <script#sudocode.net>');

Related

Sending raw email header with SwiftMailer

What I want is to send raw header using SwiftMailer
Currently this is what I can do with SwiftMailer
// Create the message
$message = Swift_Message::newInstance();
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You {fullname}, are our best client ever thanks " .
" to the {transactions} transactions you made with us.");
$message->setFrom("account#bank.com", "Your bank");
Using $message->setFrom() I can set the From field in the header but what I want is something like this
$rawHeader = 'MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="_=_swift_v4_1467746603_bb0677c43b2ba275b8602c907ef7228a_=_"
List-Unsubscribe: <http://go.proctorgallagher.com/unsubscribe/u/45802/41c5731bb75f7959e5880279725cb9b7b4f8996214976c36a0f93e36e88e7705/876424750>
X-Report-Abuse-To: abuse#pd25.com
x-job: 7215816_12853
--_=_swift_v4_1467746603_bb0677c43b2ba275b8602c907ef7228a_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable'
$message->rawHeader($rawHeader);
Or something where I don't have to let the swiftmailer know that Content-Type is field and value is text/plain
I mean whatever I put get sent as it is don't have to put everything separately.
Have you tried:
$message->setBody("You {fullname}, are our best client ever thanks " .
" to the {transactions} transactions you made with us.", "text/plain", "utf-8");

unable to send email as both plain text and html in cakephp

App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->to($emailContentArray['To']);
$Email->from(array($emailContentArray['From'] => Configure::read('FROM_NAME')));
$Email->subject($emailContentArray['Subject']);
$Email->emailFormat('both');
$response=$Email->send($emailContentArray['Body']);
if we check the resulting email it looks like:-
Content-Type: multipart/mixed; boundary="782f009f669cbcf2faafff59fe0eeb5d"
Content-Transfer-Encoding: 8bit
X-Identified-User: {:test25.xyzzzz.com:testttt.com} {sentby:program running on server}
--782f009f669cbcf2faafff59fe0eeb5d
Content-Type: multipart/alternative; boundary="alt-782f009f669cbcf2faafff59fe0eeb5d"
--alt-782f009f669cbcf2faafff59fe0eeb5d
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
<div>​<BR>Hi<BR><BR>Soon you will reach the action limit. To keep yourself updated please pay the payment.<BR><BR> Happy to have you<BR><BR>Thanks,<BR>Apps Team<BR>​</div>
--alt-782f009f669cbcf2faafff59fe0eeb5d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<div>​<BR>Hi<BR><BR>Soon you will reach the action limit. To keep yourself updated please pay the payment.<BR><BR> Happy to have you<BR><BR>Thanks,<BR>Apps Team<BR>​</div>
In text/plain the email should not show the html tags - how can I get both html and plain text to be sent correctly?
Encode the content and the subject in base64:
"=?utf-8?b?".base64_encode($s)."?=";
And before sending the emails don't forget to set the header:
'Content-Transfer-Encoding: base64';
If I'm not wrong you can make to set header: $Email->setHeader('X-Content-Transfer-Encoding', 'base64');
You can take a look at Cakephp sending UTF-8 Emails and lineLength
And at http://book.cakephp.org/2.0/en/core-utility-libraries/email.html

Sending base64 emails with SWIFTMAIL

I am just trying to get my multipart emails encoded with base64 and send via swiftmail. Here is the code I have so far:
$message = Swift_Message::newInstance("Email Template Test")
->setBoundary($boundary)
->setFrom(array('no-reply#domain.net' => 'Mailer Service'))
->setTo(array("a#d.com","a#b.com"))
->setBody($plaintext)
->addPart($htmlmail,"text/html");
$headers = $message->getHeaders();
$headers->addTextHeader('Content-Transfer-Encoding','base64');
$contenttype = $message->getHeaders()->get('Content-Type');
$contenttype->setValue('multipart/alternative');
As far as I can see from the documentation (which I don't find too clear), The Content-Transfer-Encoding header is a text header, so i should be able to set it as above. Before this, I ran an output of all the current headers, and Content-Transfer-Encoding was not listed in there, so It needed to be set. Hence why in the above code I have tried to set it.
The output is fine, I get the emails, they work, but when I view source they are not encoded. I have tried with the same above code but changing $plaintext to base64_encode($plaintext), but just received the encoded message. How is it done>
In version 5.4 you can set the encoder. Otherwise Swift_Message will use the native encoder to encode the message.
$message = \Swift_Message::newInstance("Email Template Test");
$message->setEncoder(\Swift_Encoding::getBase64Encoding());
//...
Additionally there is a bug (as of version 4 and 5) with encoding and addPart. Where the MimePart will not inherit the encoding from the origin message.
To do this you need to manually create the MimePart and attach it to the origin message.
$part = \Swift_MimePart::newInstance();
$part->setEncoder($message->getEncoder());
$part->setBody($htmlmail, 'text/html');
$message->attach($part);
This will automatically add the Content-Type: multipart/alternative; boundary=****, boundary charset and Content-Transfer-Encoding: base64 header information as well.
Result:
var_dump($message->toString());
string 'Message-ID: <2f48c04910b97f730834e92f268d3410#example.com>
Date: Thu, 14 Jan 2016 20:45:30 +0000
Subject: Email Template Test
From: Mailer Service <no-reply#domain.net>
To: a#d.com, a#b.com
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_"
--_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
VGhpcyBpcyBhbiBodG1sIG1lc3NhZ2U=
--_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
VGhpcyBpcyBhIHRleHQgbWVzc2FnZQ==
--_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_--
' (length=751)
SwiftMailer 6
In swiftmailer 6 the Swift_Encoding class and ::newInstance() methods were removed.
The original usage of \Swift_Encoding::getBase64Encoding(), looked like
public static function getBase64Encoding()
{
return self::_lookup('mime.base64contentencoder');
}
private static function _lookup($key)
{
return Swift_DependencyContainer::getInstance()->lookup($key);
}
Therefor you can call the mime.base64contentencoder directly from the Swift_DependencyContainer instead.
$encoder = \Swift_DependencyContainer::getInstance()->lookup('mime.base64contentencoder');
$message = (new \Swift_Message("Email Template Test"))
->setEncoder($encoder);
I just wanted to do this myself recently, and
\Swift_Encoding::getBase64Encoding()
is apparently removed so I had to use Swift_DependencyContainer like this:
$message->setEncoder(\Swift_DependencyContainer::getInstance()->lookup('mime.base64contentencoder'));
Now swiftmailer will use base64 as the content transfer encoding.

Sending email using php mail function

I am sending emails using php's mail function. Some addresses in email clients such as aol, hotmail.com and msn are not receiving it. Gmail works fine! I find it strange as it can pass through Gmail but not others:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: $from";
$headers .= "\r\nMIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
And then I am sending email using:
$ok = #mail($to, $subject, $message, $headers);
For diagnostic purposes $header string contains:
From: xxx#example.com
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_xd31b2fcd6941ba77b38f866330c24944x"
and the $message string contains:
This is a multi-part message in MIME format.
--==Multipart_Boundary_x0dde39863d56158409aa962fc1dd9a3bx
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
The email message appears here
--==Multipart_Boundary_x0dde39863d56158409aa962fc1dd9a3bx
Content-Type: {"application/octet-stream"};
name="afile.pdf"
Content-Disposition: attachment;
filename="afile.pdf"
Content-Transfer-Encoding: base64
The attachment's contents here
--==Multipart_Boundary_x0dde39863d56158409aa962fc1dd9a3bx
Some addresses in email clients such as aol, hotmail.com and msn are not receiving it
Your code is not the best place to start investigating this - check your email logs and bounce messages in the first place. The most likely cause is that your email is being flagged as a spam. You could verify this by setting up a mailbox yourself and sending mesages to it. How to prevent your emails being flagged as spam is a question which has been asked and answered here many times.
I am not sure whether the header is correct
It would have been mkore helpful to see an example of the email and headers generated (trimed down to a sensible size).

PHP mail() specific question

So, I would like to use mail() to send registration emails for my website, however I'd like to make it look nice while falling back to good old plaintext when necessary; a mixed message email.
However I would like it to be sent from John Doe who's email is johndoe#example.com to recipient#example.com.
The HTML code should be <html><head><title>HTML email!</title></head><body><p>This is HTML!</p></body</html> and the plaintext message should be This is plaintext.
What would be the arguments to mail() to accomplish this? I know a lot of it deals with changing the header in some crazy way.
Thanks so much!
Use something like SwiftMailer instead, as it has nice things like header injection prevention. With that in mind, yes, you have to set custom headers and use a multi-part body to achieve what you want:
/***************************************************************
Creating Email: Headers, BODY
1- HTML Email WIthout Attachment!! <<-------- H T M L ---------
***************************************************************/
#---->Headers Part
$Headers =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="$boundary1"
AKAM;
#---->BODY Part
$Body =<<<AKAM
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="$boundary1"
This is a multi-part message in MIME format.
--$boundary1
Content-Type: text/plain;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$TextMessage
--$boundary1
Content-Type: text/html;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$HTMLMessage
--$boundary1--
AKAM;
Source: http://www.php.net/manual/en/function.mail.php#83491
This is a lot of work. Which, once again, is why I recommend having a library that can handle all of this for you, plus other features.
Everything is here (Example #4): http://php.net/manual/en/function.mail.php

Categories