Php loop foreach behave strange and prints out wrong html - php

I am trying to prepare email html and I have 2 groups of customers-
1 group just will receive email
2 group will receive same email + voucher inside..
I have prepared a short code just to illustrate the problem. Here it is:
$voucher='<div>VOUCHER IMAGE COMES HERE</div>';//only group for voucher must see it
$mail_description='Here comes my mail text';
$voucherarray=array("mail1#gmail.com","mail3#gmail.com");
$mailaray=array("mail1#gmail.com","mail2#gmail.com");
$html='';
foreach($mailaray as $email){
$html.='<div>'.$mail_description.'</div>';
if(in_array($email, $voucherarray)){
$html.=$voucher;
echo $email.'is for voucher';
}else{ $email.'is not for voucher';}
$html.='<table><tr><td>here is some other text</td></tr></table>
<div clss="footer"></div>';
mail($email,'subject',$html);
}
echo $html.'<br />';
this code prints out:
mail1#gmail.comis for voucher
Here comes my mail text
VOUCHER IMAGE COMES HERE
Here comes my mail text
here is some other text
Here comes my mail text
VOUCHER IMAGE COMES HERE
Here comes my mail text
here is some other text
What is wrong with it and why it prints out VOUCHER IMAGE COMES HERE for both mails as only one is for voucher?
Also it sends same mail to all groups users

Your need to separate between the html you send in the mail and the output you want to see for the job that sends the mail. second thing you need to empty the mail body variable after each iteration, you don't want every client to receive the mail content of the previous clients.
$voucher='<div>VOUCHER IMAGE COMES HERE</div>';//only group for voucher must see it
$mail_description='Here comes my mail text';
$voucherarray=array("mail1#gmail.com","mail3#gmail.com");
$mailaray=array("mail1#gmail.com","mail2#gmail.com");
$html='';
foreach($mailaray as $email){
$mailHtml = '';
$mailHtml.='<div>'.$mail_description.'</div>';
if(in_array($email, $voucherarray)){
$mailHtml.=$voucher;
echo $email.'is for voucher<br>';
}else{
echo $email.'is not for voucher<br>';
}
$mailHtml.='<table><tr><td>here is some other text</td></tr></table><div clss="footer"></div>';
mail($email,'subject',$mailHtml);
$html .= '<br>' . $mailHtml;
}
echo $html.'<br />';

Related

How can I send a copy of an HTML form to the sender's email using a text box?

My code is same as here: How to send copy of PHP / HTML form to sender's email?
I would want that when I write email address to text box then I will get copy of the form to email I wrote.
I have replaced HTML code:
<label><input type="checkbox" name="sendcopy" value="Yes" checked/>Send copy to your mail</label>
to
<label><b>Write your email here:</b><div style="width: 33%;"><input class="contact-input"style="width: 500px;" name="sendcopy"><br/></div></label>
I have replaced PHP code:
$sendCopy = isset($_POST['sendcopy']);
to
$sendCopy = $_POST["sendcopy"];
And I replaced PHP code:
if ($sendCopy) {
$sentToSender = mail($email, "=?$charset?B?" . base64_encode($subject) . "?=", $body, $head);}
to
if (mail($sendCopy, "=?$charset?B?" . base64_encode($subject) . "?=", $body, $head))
Now when I write email address to text box, I will get the copy of the form to email I wrote, but when I leave the text box to blank the submission of the form ends with an error.
So, the main thing is that when form sender writes his/her email to a text box he/she will get the copy of the form to his/her email. And when you leave the text box blank, the form will send only to recipient email. How can I do this?
I have replaced PHP code:
$sendCopy = isset($_POST['sendcopy']);
That will always be considered as being "set" and will return a Boolean.
And when you leave the text box blank, the form will send only to recipient email. How can I do this?
You're looking to use a ternary operator.
From the manual:
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
So your attempt would round off to:
$action = (isset($_POST['sendcopy'])) ? 'default' : $_POST['sendcopy'];
While replacing default to what you want it to be as a default.
Side note: isset() in this instance is good for radios buttons and checkboxes. If you don't want empty fields, you could use empty() for text inputs, etc. besides radios buttons and checkboxes.

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

PHP : Read GMAIL Email with imap

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

PHP : mailto function gets breaking when populating text having lines and url

Hi I am creating a task assignment page, in which after creating the task admin can send a mail to assignee to know the task. I cant use PHP mail function since I cant configure office SMTP mail address. So I am using mailto function.
Issue happens when my Commenst got symbols like " &, ", ' . It will stop executing the remaining part of code
In my mail it just populates the first line. So my comments is getting broken.
Here is my code to mailto function :
';
You put everything into one line which brings you into devils kitchen. Instead put the functionality apart (e.g. first load the data from the database), then process the data (e.g. build the mailbox name, the subject and the body) and then finally create the mail URL.
<?php
# load data from database
$assignee = mysql_result($result, $i, 'assignee');
$comments = mysql_result($result, $i, 'comments');
# process data
$mailbox = sprintf('%s#symantec.com', rawurlencode($assignee));
$headers = array(
'subject' => 'Task Assignment',
'body' => "Hello $assignee,\n\nDescription : $comments ...\n\nFor more details log in to ...",
);
# generate the mailto URL
$url = sprintf('mailto:%s?%s', $mailbox, http_build_query($headers));
You can then just output the $url:
printf('<img src="images/mail.png" width="32" height="32" p="center">',
$url);
You need to explicity urlencode everything that is non alphanumeric.
Note, urlencode (the php command) doesn't urlencode everything such as underscore and letters and perhaps other things.
Example:
$test="this: is it's dog/+_";
echo "<br>";
echo urlencode($test);
echo "<br>";
echo $test;
produces:
this%3A+is+it%27s+dog%2F%2B_
this: is it's dog/+_

Codeigniter: Email attachment of last emails not cleared while sending multiple emails in loop

My code sends multiple emails in loop with attachment,
Problem is attachments of last(previous all) emails get attached to next email.
ex. suppose 3 emails in database with 1 attachment in each(a1.pdf, a2.pdf, a3.pdf)
then,
it sends email with attachment as
email 1:
attachment :a1.pdf
email 2:
attachment :a1.pdf, a2.pdf
email 3:
attachment :a1.pdf, a2.pdf, a3.pdf
I am using codeigniter framework.
My code is (this code is called in loop)
.
.
.
$this->email->subject($item->subject);
$this->email->message($message);
$attachments='';
if(strlen($item->attachment) > 5)
{
$attachments = explode(',', $item->attachment);
foreach($attachments as $attachment)
{
if(strlen($attachment)>5)
$this->email->attach(FCPATH . 'attachments/' . $attachment);
}
}
$this->email->send();
.
.
.
You need to reset it in CodeIgniter.
At the end of the loop add:
$this->email->clear(TRUE);
This resets all email variables including the attachments, allowing you to create a new mail.
You need to use $this->email->clear(); to clean out the variables set within the loop. Read the manual.

Categories