PHP : Read GMAIL Email with imap - php

here's my problem: I have a mailbox where came from 2 types of email:
1) e-mail with plain text
2) email, I think, base64-encoded text / html
I have a simple page in php that goes to read this email box, and prints out an HTML table with some information of each email
I recognize the two types of email from the object of the email, in the first case I have no problems, while in the second i have a problem
i read the "body" of the mail of the first type in this way (and I have no problem, I can print the contents of the email as plain text) :
$id = imap_uid($stream ,$email_id);
$message = imap_qprint(imap_body($stream, $email_id,FT_PEEK));
but in the second case it does not work this way, and I have tried the following
$message = imap_fetchbody($stream,$email_id,"");
$message = imap_fetchbody($stream,$email_id,"0");
$message = imap_fetchbody($stream,$email_id,"1");
$message = imap_fetchbody($stream,$email_id,"1.1");
$message = imap_fetchbody($stream,$email_id,"1.2");
$message = imap_fetchbody($stream,$email_id,"2");
echo "*".base64_decode($message)."*";
echo "*".imap_base64($message)."*";
but I have no result

Related

PHPMailer and attachments not displaying properly when opened

My apologies, I am very new and had difficulty with the editing process etc. I hope the following is more acceptable. The PHP application attaches to the 'receiving email box' and processes the received emails accordingly. It creates a new email that contains comments, attaches the original email and then sends the newly created email back to the 'from address' extracted from the original email. The original email may have been sent from any number of email programs available to users. It may also have pdf/text attachments. When I send the 'newly' created email to Gmail, it displays the attached original email (and it's attachments if any) correctly but not when it is sent to MS Outlook or Horde.
After execturing a new phpmailer class, I added the following:
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
Coding to save the original email:
$mime = imap_fetchbody($inbox, $email_number, "");
file_put_contents('./workfolder/OriginalEmail.eml', $mime);
I have also tried this coding when saving the original email:
$headers = imap_fetchheader($inbox,$email_number, FT_PREFETCHTEXT);
$body = imap_body($inbox, $email_number);
$structures = imap_fetchstructure($inbox, $email_number);
file_put_contents('./workfolder/OriginalEmail.eml', $headers . '\n' . $body . '\n' . $structures);
I have checked the mime type which displays: message/rfc822
Coding used to attach original email:
$mail->AddAttachment('./workfolder/OriginalEmail.eml');
I have also tried this when attaching the original email:
$mailer->AddAttachment('./workfolder/','OriginalEmail.eml','base64','message/rfc822');
Wednesday - I am having such problems with adding comments.
Here is what I tried:
$strOrigEmail = file_get_contents('./workfolder/OriginalEmail.eml');
echo '<BR><BR>$strOrigEmail: ' . $strOrigEmail;
$mail->addStringAttachment('$strOrigEmail', 'YourEmail.eml');
And the echo displays everything from the original email- headers, body etc. and the attachments (including their filename) but it is endocded. Below is a snippet below of what it looks like:
name=junk.txt Content-Disposition: attachment; size=11; filename=junk.txt junk file --=_7-AofrqAv83eu5i63j73DWn--
I have also tried this and no better - displays same thing as above:
$strOrigEmail = file_get_contents('./workfolder/OriginalEmail.eml');
echo '<BR><BR>$strOrigEmail: ' . $strOrigEmail;
$mail->addStringAttachment(base64_decode('$strOrigEmail'), 'YourEmail.eml');

php - trouble getting /r/n to be a <br> in email message

I have a web form that allows a person to enter text and then send an email using swift mail. The text they enter may contain \r\n. I send email from swift mail in text/html. Prior to sending the email, I have attempted to do a sting replace on the \r\n, as well as a nl2br function on the user entered text so that all occurrences of \r\n are changed to ; yet in all cases the email still arrives with \r\n being displayed in the text message, as opposed to actual line breaks.
below is the code snippet to prep the text and the email code.
/* replace all cr/lf with a <br> */
$ind_msg = nl2br($ind_msg);
/* send the email message */
$status = send_email($db, $ind_email, $ind_name, $sender, $sender_name, $msg_subj, $ind_msg, "");
email code
/* create the message */
$message = Swift_Message::newInstance();
$message->setTo(array($recipient => $recipient_name));
$message->setSubject($msg_subject);
$message->setContentType("text/html; charset=UTF-8");
$message->setBody($msg_body);
$message->setFrom(array($sender => $sender_name));
What am I missing or doing wrong?
nl2br do not replace newline-chars. It adds the "br" tag before all newline chars. If you want to replace them, use str_replace();
vg
bytecounter
You can try :
$ind_msg = str_replace(array('\r\n'), array('<br>'), $ind_msg);

PHPMailer - Sending to multiple address but unique content

I am building a quote system, the quote system sends out the quote to multiple people with a personalised message via email. I am using the following code to do so:
$mail = new PHPMailer();
$mail->AddReplyTo("myAddress#domain.com", "Company");
$mail->SetFrom('quotes#company.com', 'COMPANY');
// Set each of the recipients
$users = $quote->GetClients();
foreach($users as $user)
{
$mail->AddAddress($user["Email"], $user["Name"]);
}
$mail->Subject = "Hello" . $users[0]["Name"];
$body = "This is your personal Email";
$mail->MsgHTML($body);
$mail->Send();
The problem being is that, when I send this email, it sends to all the recipients that it should, but it includes all of the recipients names. Whereas, I just want to display for that particular person. I.e. If I get an email, it would be: "Hello Phorce, This is your Personal Email" but instead I get "Hello {user1} {user2} {user3}" and this is not what I want.
Is it possible to do what I am trying to do using PHPMailer?

Get Contents from Text File and Replace Keywords PHP

I have a form. I'm using "post" method that sends and emails to multiple people that registered. Then the $body of the email is based on a template. No database, no classes, simple form. Yes I read up about this already they're all there I just couldn't put it together, related to this case.
The text "email_template.txt" should have something like:
Hello #firstname# #lastname#. Thank you for registering! Your registered email is #email#
Would look like this upon processing by PHP
Hello **John Doe**. Thank you registering! Your registered email is **example#example.com**.
On my php I have something like:
<?php
//form and validation
$firstname = "John"; // inputed name on the form, let say
$lastname = "Doe"; // inputed last name
$email = "example"; // inputed email
//email message
$body = ... some type of a get_file_content....
mail($_POST['email'], 'Party Invitation', $body, 'From: admin#admin.com');
?>
Where $body is the email message to the registrants submitted via this PHP form.
sprintf will work good
your template could be:
Hello %s %s. Thank you for registering! Your registered email is %s
$body = sprintf($fileContents,$firstname,$lastname,$email);
or str_replace(), pretty much a find replace.
$body = "Hello #firstname# #lastname#. Thank you for registering! Your registered email is #email#";
$body = str_replace("#firstname#",$firstname,$body);
$body = str_replace("#lastname#",$lastname,$body);
$body = str_replace("#email#",$email,$body);

How can I add HTML formatting to 'Swift Mail tutorial based' PHP email?

I have developed a competition page for a client, and they wish for the email the customer receives be more than simply text. The tutorial I used only provided simple text, within the 'send body message'. I am required to add html to thank the customer for entering, with introducing images to this email.
The code is:
//send the welcome letter
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Thanks for entering the competition');
$message ->setFrom(array('info#examplemail.com' => 'FromEmailExample'));
$message ->setTo(array($info['email'] => $info['name']));
$message ->setBody('Thanks for entering the competition, we will be in touch if you are a lucky winner.');
$result = $mailer->send($message);
return $result;
}
This function.php sheet is working and the customer is recieving their email ok, I just need to change the
('Thanks for entering the competition,
we will be in touch if you are a lucky
winner.')
to have HTML instead...
Please, if you can, provide me with an example of how I can integrate HTML into this function.
You can also just add 'text/html' to setBody (ref):
->setBody($this->renderView('YouBundleName:Default:email.html.twig'), 'text/html');
$message = Swift_Message::newInstance();
$message->setContentType('text=html');
Will the same text remain or should it be styled somehow?
Editing your emails in html requires inline css styles eg:
('<p style="font-size:1.2em; color:#f0f0f0;">Thanks for entering the competition, we will be in touch if you are a lucky winner.</p>')
if you need a table just add:
('<table style="font-size:1.2em; color:#f0f0f0;"><tr><td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td></tr></table>')
or to make it more simpler
$message_body'
<table style="font-size:1.2em; color:#f0f0f0;">
<tr>
<td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td>
</tr>
</table>
';
$message ->setBody($message_body);
I know that when I need to send html emails I need to set the content-type to html which I believe you did on the following line
$body = format_email($info,'html');
I hope this is what you were looking for. if not let me know

Categories