PHP not encoding quoted_printable_encode correctly - php

I'm trying to add an emoticon(fire) in email subject. Below is the code I'm using:
$subject = "Test email \xF0\x9F\x94\xA5";
$charset = "UTF-8";
$subject = sprintf('=?%s?Q?%s?=', $charset, quoted_printable_encode($subject));
// send email....
This is adding a fire emoticon to the email subject successfully. But when I try to get the subject from database like:
$subject = $themeArray['templateSubject']; // Test email \xF0\x9F\x94\xA5
$charset = "UTF-8";
$subject = sprintf('=?%s?Q?%s?=', $charset, quoted_printable_encode($subject));
// send email....
This is not converting the characters to emoticon. Instead I'm getting the subject in email as
Test email \xF0\x9F\x94\xA5
Why its not converting when I get subject from database?
Can anyone help me to fix this? Thanks in advance.

Related

duplicate empty messages in php imap?

I am fetching mail messages in php via imap.
Its working well to retrieve the email header and body however frequently some of the messages seem to be coming through twice, the only difference being that in that duplicate the email BODY is empty - all that exists are the email headers.
I have added logging and it seems that the EMPTY message normally comes through first and then on subsequent fetches the full message is retrieved.
Can anyone explain this to me? Is there any reason why retrieving messages from imap will first return empty body emails in SOME but not all cases?
This is my code that is fetching the emails.
$messages = imap_sort($imap, SORTARRIVAL, $sortorder,0,$searchval);
foreach ($messages as $message) {
$header = imap_header($imap, $message);
//Get user from reply-to header info
$useremail = "{$header->reply_to[0]->mailbox}#{$header->reply_to[0]->host}";
$userfullname = $header->reply_to[0]->personal;
//get subject from header
$subject = $header->subject;
$fh = imap_fetchheader($imap, $message);
$fb = imap_body($imap, $message);
$fullbody = $fh.$fb;
// GET TEXT BODY
$bodyTxt = get_part($imap, $message, "TEXT/PLAIN");
//GET HTML BODY
$bodyHtml = get_part($imap, $message, "TEXT/HTML");
In many cases the first time the message is retrieved it will have an empty body (ie $fullbody will be empty). Often this comes in batches for eg 10 messages will be logged as having empty body.
Subsequently the same batch is then retrieved but this time with a body - and I have no explanation for it.
Can anyone offer any ideas?
Thanks

How to send an email in multiple chunks?

I'm using the php mail function to send email and it's working fine.
The email body is dynamically generated and the problem is that the email recipient can only receive emails that are 160 characters or less. If the body of the email is longer than 160 characters, then I want to split the body into separate blocks, each less than 160 characters.
I'm using CRON Jobs and curl to automatically send the email.
How can I send each separate email to the same recipient when more than one body is generated? Below $bodyAll represents that there is only one email to be sent because the dynamically generated content fits within 160 characters. If the body content is more than 160, then $bodyAll would not be sent and $bodyFirstPart would be sent to the recipient, then $bodySecondPart, etc., until all the separate body texts are sent.
$body = $bodyAll;
$body = $bodyFirstPart;
$body = $bodySecondPart;
$body = $bodyThirdPart;
$mail->addAddress("recepient1#example.com");
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body</i>";
if(!$mail->send())
you can use strlen to check the length inside a while loop trimming it down with substr, and send each chunk every loop iteration:
<?php
$bodyAll = "
some really long text that exceeds the 160 character maximum for emails,
I mean it really just tends to drag on forever and ever and ever and ever and
ever and ever and ever and ever and ever and ever and ever......
";
$mail->addAddress("recepient1#example.com");
$mail->Subject = "Subject Text";
while( !empty($bodyAll) ){
// check if body too long
if (strlen($bodyAll) > 160){
// get the first chunk of 160 chars
$body = substr($bodyAll,0,160);
// trim off those from the rest
$bodyAll = substr($bodyAll,160);
} else {
// loop terminating condition
$body = $bodyAll;
$bodyAll = "";
}
// send each chunk
$mail->Body = "$body";
if(!$mail->send())
// catch send error
}

php send mail to both parties POST collected email address

This is probably an easy question but I am not sure the proper syntax.
I am sending an email to my client - but my client also wants that person to receive the emails once they hit submit.
Currently its a POST php form and it works fine - but need to include the POST-email address that was entered into the form.
$from_email = $_POST['emailAddress'];
$to = 'owen#gmail.com';
AND I have mail($to, $subject, $message, $header..
So how do I rewrite the code:
$to = 'owenpiccirillo#gmail.com';
to have also send to the email submitted in the form? because this works.
$to = 'owen#gmail.com, whatever#aol.com';
but this doesnt work....
$to = 'owen#gmail.com' . $from_email;
Thanks in advance
-O
You need a comma and space in the $to concatenation:
NOT
$to = 'owen#gmail.com' . $from_email;
// results in 'owen#gmail.comwhatever#aol.com'
but THIS
$to = 'owen#gmail.com, ' . $from_email;
// results in 'owen#gmail.com, whatever#aol.com'
If this works:
$to = 'owen#gmail.com, whatever#aol.com';
then what exactly is the issue? The reason this doesn't work:
$to = 'owen#gmail.com' . $from_email;
is because there's no comma separating the values. So it would evaluate to:
$to = 'owen#gmail.comwhatever#aol.com';
which isn't a valid email address.
This is because the . is concatenating the two addresses together. So if you do an echo or print_r you would see something like this:
owen#gmail.comwhatever#aol.com
You need to add a comma:
// Value will be injected into the string with double quotes.
$to = "owen#gmail.com, $from_email";
Alternatively, you can use CC and BCC to send copies.

Aid me with php imap? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP imap problems
I need to be able to use my mail (gmail) from a php script. But wherever I try, the email body comes out all rubbbish with characters like =3D and random equals signs. Sometimes it comes in as base64 or nothing at all. How can I get an email and display it in HTML Purifier clean html or plain text in a pre tag if no html is available.?
$overview = imap_fetch_overview($inbox,$email_number,0);
$body_pre = imap_fetchbody($inbox, $email_number, 2.1);
$message = imap_fetchbody($inbox, $email_number, 2.2);
$message = base64_decode($message);
if (empty($message)){
$message = $body_pre;
$message = preg_replace('#(https?://([-\w\.]+)+(:\d+)?((/[\w/_\.%\-+~]*)?(\?\S+)?)?)#', '$1',$message);
$message = '<pre>'.htmlentities($message).'</pre>';
}else{
$cleanconfig = HTMLPurifier_Config::createDefault();
$cleanconfig->set('Core.Encoding', 'UTF-8');
$cleanconfig->set('HTML.Doctype', 'HTML 4.01 Transitional');
$purifier = new HTMLPurifier($cleanconfig);
$message = $purifier->purify($message);
}
this code, $message just comes blank.
The message with "characters like =3D and random equals signs" is quoted-printable encoded. You can decode using php with quoted_printable_decode.
There are different ways how mail messages can be encoded, like quited-printable or base64. The sender decides on the used encoding.

Email Form more than 20 values

I have a form with more than 20 input fields. The PHP mail function is not working since it can only accept 5 parameters. How can I send these values to my email address?
$to = 'myemail#mydomain.com';
$subject = 'form values';
$message = '';
foreach( $_POST as $key => $ value ) {
$message .= $key . ' => ' . $value . '<br>';
}
mail( $to, $subject, $message);
The parameters in the PHP mail() function have different meaning. They are not the data, sent to the user. Try using the following script and if it works - format the $message to match your requirements.
$email = 'rec#example.com';
$subject = 'Subject';
$message = print_r($_POST,true);
mail($email,$subject,$message);
More information can be found # http://php.net/manual/en/function.mail.php
Well I don't know what kind of input fields you have, but they could all be passed in the "subject" field. However, if you have a sufficiently complex email to send, and you really need to write everything yourself, you should look into PEAR.
So either add the 20 input fields onto the subject $subject .= 20things or use a tool that is inherently secure without having to remember to escape input and validate using regex.

Categories