PHPMailer sending email to wrong recipient - php

I have a situation where I sending an 2 different emails, one to a customer and one to a member after the member accepts a task. Within the function I am calling 2 functions, one that sends to the client and one is sent to the member. The what is happening is that the second email is being sent to both the member and the client, yet the client, the first email, only gets the email expected.
What I figured is happening is that because both of these functions are occurring at the same time, some how the addAddress() function is taking both email addresses over to the second email. My proof of this is I changed the order of the functions so the Member send first and the Client sent second and they both got the Client email where before the Client sent first and the Member sent second.
My question than is how do I make the first email break for a few seconds before the second email is sent to stop this from happening.
Here is the basic set up of the code.
function memberAccept()
{
// SQL to mark in DB that member accept task
$this->memberEmail($id)
$this->clientEmail($id)
}
function memberEmail($id)
{
// SQL to gather information about member inner join with task
$subject = 'Email';
$body = 'Email to member'
$this->sendEmail($member['email'], $subject, $body)
}
function clientEmail($id)
{
// SQL to gather information about client inner join with task
$subject = 'Email';
$body = 'Email to client'
$this->sendEmail($client['priemail'], $subject, $body)
}
sendEmail() is in another Class set up to use PHPMailer functions to send the email.

In your case, you must use the following function before adding new recipient for the mail:
$mailer->ClearAllRecipients( ); // clear all
Basically, the problem is that, when you add first recipient, mailer successfully delivers the mail, but the object is not destroyed yet. Hence, when the next mail is sent, member's email is still in the recipient, so alter your code to call this ClearAllRecipients function before adding the new recipient.

Related

Can't embed variable in mail function php

I want to send email with different subjects. I have the following code that get mail subject and message from the database.
$sql = "SELECT mail_subject, mail_content FROM new_business WHERE business_name = '$biz_name'";
$query = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($query)) {
$subject = $row['mail_subject']; //Getting mail subject to send
$mail_content = $row['mail_content']; //Getting mail content
}
echo $subject; //checking if it gets subject and it surely does.
$to = 'utopian51#gmail.com';
mail($to, $subject, $mail_content);//Mail is not sent
If subject pulls data from database mail is not sent. I have checked several times including spam and also checked if subject gets any data from database before embedding it in mail function. It seems it gets the right data but the mail is not sent.
But when i hardcode the subject like this: $subject = 'this is a test subject'; , the mail is sent successfully. Why mail function does not let me embed variable that gets subject from database? I need to send the mails with different subject from database. Whats the solution?
There's many things which are confusing about the code you've written. You never check for error conditions in the mysqli_ calls nor the mail() call. You run a query which will discard all but the last row (if any). However the way to diagnose issues with mail() is not to watch your inbox. In more than 99% of cases the problem is with the interface between PHP and the MTA or with the MTA configuration itself.
You have provided no details of either.
I would recommend you use simply:
mail('utopian51#gmail.com', 'test', date('r'));
and start debugging your MTA.
Independently you can work on your bulk emailing script, simply writing to a file instead of calling mail() until both parts are working.

Unable to get the unread mails from office365 mail box using PHP imap_headers() function

I was trying to read the gmail mail box using imap_headers and imap_header() function available in php, it was working fine without any problems. Here in gmail imap_header() function gives the count of only un-read mails and imap_headers($message_number) function would take the message number as input and would return the header information of the mail. Executing imap_fetchstructure() and imap_fetchbody() the message would be marked as read and that message would not come in the next run, since it was marked as read.
Now when I am using office365 account the function imap_header() is returning the count of all mails in the inbox and not the unread mails count, also as contrary in previous case imap_fetchstructure() and imap_fetchbody() are not marking the mail as read and hence the read mails also get tracked in the next job cycles.
Any inputs on this is really helpfull.....Thanks:)
The imap_header() will give you all the mail as its definition.
You should use imap_search() to filter out the unread mail.
You can use the below code:
$imapobj = imap_open(SERVER,USERNAME,PASSWORD);
$result = imap_search($imapobj, 'UNSEEN');
foreach($result as $res=>$value){
$maildetails = imap_headerinfo($value);
$status = imap_setflag_full($imapobj, $value, "\\Seen");
}

How to not make PHPmailer send an email with multiple 'to' addresses?

I have a table in my database which contains emails to be sent. A script runs periodically as a cron job to read the data from table and dispatch the emails via the SMTP server. I am using PHPMailer for the work here. The script runs a loop for sending emails until all the emails in the table have been sent.
The problem is that most consecutive emails being sent have a common subject. So when the loop runs, somehow all the emails that get sent have a long 'to' list. So basically if 5 email entries (with different to addresses) have the same subject and same content, then it ends up sending only 1 email with 5 addresses (one of each email entry) in the 'to' field. This reveals the email address of every person to everyone else on the list. This is undesirable and the emails must be sent to only that one person for whom it is meant.
I don't know what's at play here. Any suggestions?
Are you doing something like this?
$mailer = new PHPMailer();
while($row = fetch_from_db()) {
$mailer->AddAddress($row['email']);
$mailer->send();
}
If so, you need to do a
$mailer->ClearAllRecipients()
after you send each email, so you start out with a "fresh" To: list.
while($row = fetch_from_db()) {
$mailer->AddAddress($row['email']);
$mailer->send();
$mailer->ClearAllRecipients()
}
1st, get the contacts from your db
$contacts = 'GET CONTACTS ARRAY FROM DB'
if(!empty($contacts)){
foreach ($contacts as $crt_contact){
$emails[] = $crt_contact->email ;
}
}
2nd use the following to create the "to" field
$to = implode(',', array_unique($emails));
Use the $to to send in mail() function

How to get the message body from PHPMailer?

I use PHPMailer to send email via SMTP. It works, but it doesn't save the sent emails in
sent items. I want to make to sent emails in the sent items, any idea?
I know can use imap_append function to achieve it. But, how to do that?
The PHPMailer seems doesn't has the function to return the $message.
if ($return = $mail->Send()) {
$stream = imap_open("{{$host}:993/imap/ssl}Sent Items", $user_name, $user_pwd);
imap_append($stream, "{{$host}:993/imap/ssl}Sent Items" , $message , "\\Seen");
imap_close($stream);
};
How to get the message body from PHPMailer?
The instance variables from PHPMailer are all public.
Looking at the source you can just use:
$mail->Body
If you want the plain body including the all boundaries you could also use:
$mail->CreateBody();
See:
http://phpmailer.svn.sourceforge.net/viewvc/phpmailer/trunk/phpmailer/class.phpmailer.php?revision=452&view=markup
The PHPMailer seems doesn't has the function to return the $message.
Yes, it does. At least in version 5.2.28 there is a method getSentMIMEMessage() to get the full MIME message (different from the email body). That method only works after sending the email, or calling the preSend() method.

which of this method is recomended - mail function

hay
now i want to send mail to 200 users
first way
$users = 'user1#,user2#,user3,etc';
foreach(explod(',',$users as $mail){
mail($mail,'','','');
}
or
mail(mail one,mail 2,mail3,mail4,etc)
i know the code is fully error
but i want the meaning
which is the best
mail with multi by spreate by ,
or looping the mail function with the one mail every time
Ideally, you should use an external piece of mailing software so you can send an e-mail to a list and it will handle individual recipients; this way, you can avoid looping mail calls (and queued sendmail requests) while not disclosing your mailing list.
Of the options you presented, it is best to send your email using a loop with individual calls to the mail function so as not to disclose your recipients.
Finally, maybe try something like this:
$recipients = array('user#example.com','admin#example.org',); // mail list
$bcc = join(',', $recipients);
mail(
'"Undisclosed Recipients" <no-reply#example.com>',
$subject,
$message,
"BCC: {$bcc}"
);
However, if you are using this, make sure whatever sendmail client you are using strips the BCC header before sending.

Categories