Trying To read data from data base using php - php

I am trying to read an email Address from a database and send mail to the email.
Here is my code to read the email address:
$recipient = mysql_query("SELECT recipientEmail FROM flightStatus
WHERE arrivalStatus = 'Landed'");
There is currently only one data in the database with the condition landed.
Using the phpMailer example to work around the mailing function so my mail function looks like this:
$mail->From = "xxxx.yyyyy#gmail.com";
$mail->AddAddress($recipient);
But I get message can't send message.

You're trying to send a mail to mysql-resource. You have to 'fetch' the results first with eg. mysql_fetch_assoc.

Here is a very good link to get you started or to improve your mysql + php skills
Database and other Animals
Note you will need to loop through your query result, even if you only have one entry in it because eventually you will have more than one entry

$recipient is a resource (mysql_query). To get to the selected recipientEmail you can use:
$resource = mysql_query("SELECT recipientEmail FROM flightStatus WHERE arrivalStatus = 'Landed'");
$row = mysql_fetch_array($resource);
$mail->From = "...";
$mail->AddAdrress($row['recipientEmail']);

Related

How can I send an email to lots of users using a proper way in CodeIgniter 3

I need some advice because I am building a subscription module. And I have a list of so many emails. Let say 1052 emails. And I have a code like this:
$email_list = $this->getClientEmails(); //all email for now returns 1052 valid emails
$valid_email = array();
$invalid_email = array();
if(count($email_list) > 0) {
for($x = 0; $x < count($email_list); $x++) {
if(valid_email($email_list[$x]['email'])) {
$valid_email[] = $email_list[$x]['email'];
}
//get all invalid emails
/*else {
$invalid_email[] = array(
'id' => $email_list[$x]['id'],
'email' => $email_list[$x]['email']
);
}*/
}
}
$email_string = implode(',', $valid_email);
$this->email->set_mailtype("html");
$this->email->from($from, 'Sample Admin');
$this->email->to($email_string); //send an email to 1052 users
$this->email->cc('test#sampleemail.com.ph');
$this->email->subject($subj);
$this->email->message($content);
$send_mail = $this->email->send();
if($send_mail) {
fp('success');
} else {
fp('failed');
}
Is it fine if I send an email like this? Or should I make a loop to send my email to different users? Means I will not use my imploded string. I will do the sending once in every week. And also what if the sending of email suddenly stops in the middle what should I do? Do I need to resend it again? Or should I make a column in my table that will update if the email is sent or not?
Can you give me some advice about this? That's all thanks.
Okay because you have a mailing list the first thing that i would recommend is that you push the script to background. Use selinium or cron for the same that way page render won't get stuck.
Once done You can send emails either way, send to multiple people or one at a time, both of them are valid and won't cause any problem. The point you need to consider here is the SMTP connection that you maintain.
If you are sending them all individually, you don't want to close connection to SMTP server and reconnect every time to send the mail which would only cause the overhead.
I should say that from your case the most valid way to send email is make a queue on some database preferably redis and have a task handle them in background (cron job if you are on cpanel or selinium if you own the server)
Finally this is a part that you might wanna test out. Because you have a mailing list i am guessing you don't want people to see through your whole list so check the headers when you are sending mails to all at once and if you don't see email from other users , you are good to go else send to each of them separately.
Also one final thing, emails not being delivered is usually bounced which may reflect bad on your server so have a script that flags emails that are constantly rejected and stop sending mails to the same or your ip address might end with bad repo and mails might end up in spam.
Have you thought of using PHPMailer as a library on your CodeIgniter installation?
You could just do it like this:
if(count($email_list) > 0) {
for($x = 0; $x < count($email_list); $x++) {
if(valid_email($email_list[$x]['email'])) {
$mail->addAddress($email_list[$x]['email'], $x);
}
}
}
Please refer to this example on how to use PHPMailer.
I hope this helps, or at least that it gives you a different perspective on how can this be done.
Referring to:
Or should I make a column in my table that will update if the email is sent or not?
Yes, I think that if you want to control if an email has been sent you should use a 1 character field on your table as a "flag" to corroborate that the email has been sent to your users.

Read out mails from exchange with php and display it as a website

I would like to read the mailbox on an exchange with the help of PHP.
This Exchange is our domain.
The idea is to display the report mails of the backupruns on a website.
Only certain messages should be displayed, they should be filtering by using to subject of the mail.
Example: Backup Exec Alert: Job Success (Server: "SERVER") (Job: "JOB")
In the subject is the word "Job Success", if it finds "Job Success" it should be displayed on the site including server and job ofcourse. The date and time should also displayed and it only have to show the last mails from the 24h.
I hope you understand my question and you can help me out.
Here is what I got, I don't know if this is right.
<?php
$server = "{webmail.domain.xx}INBOX";
$user = "...";
$passwd = "";
$mbox = imap_open($server,$user,$passwd) or die("Could not open Mailbox - try again later!");
...
?>

Can't use an array in Swift Mailer function

I'm using Swift Mailer to send emails to users. I've following script to send email to a number of users at a time.
$j=0;
while($row = mysql_fetch_assoc($email))
{
$message[$j]->addTo($row['email_invited']);
$mailer->send($message[$j]);
$j++;
}
I get the following error while doing this:
Fatal error: Cannot use object of type Swift_Message as array in
/home/public_html/example.com/people.php on line 21
If I try to send emails like this:
while($row = mysql_fetch_assoc($email))
{
$message->addTo($row['email_invited']);
}
$mailer->send($message);
It sends the email normally & every user gets it but the issue with that is every user can see what other users have got the email along with him. So basically in "To" field, every email is bound & thus every users gets to see all other email addresses.
What I want to achieve is every user should get a personalized email only even though it's sent in bulk. That is why I tried to use array & separate email but getting error in that.
Any idea how to achieve that?
How about doing. I think you need to create new instance of message (and maybe mailer too) in every loop.
while($row = mysql_fetch_assoc($email))
{
$mailer = Swift_Mailer::newInstance($transportName);
$message = Swift_Message::newInstance($subject);
$message->addTo($row['email_invited']);
$mailer->send($message);
}

How to get the sender's email address from Zend_Mail_Message?

I'm using the Zend_Mail_Message class to display emails inside of my PHP app, and am able to output the subject, content, date and even the NAME of the sender (using $message->from) but I can't figure out how to get the email address of the person who sent the message. The documentation is no help and googling finds a million results for how to send messages with Zend, but nothing about getting the address that sent the message.
EDIT:
This is how I ended up doing it. After some more digging, I found the sender's email in a field called 'return-path'. Unfortunately, this field has a dash in the name (WTF??) so to access it, I had to do this:
$return_path = 'return-path';
$message->reply_to = $zendMessage->$return_path;
Using the return-path caused some problems with some emails, specifically messages from no-reply accounts (mail-noreply#gmail.com, member#linkedin.com etc). These addresses would end up looking something like this:
m-9xpfkzulthmad8z9lls0s6ehupvordjdcor30geppm12kbvyropj1zs5#bounce.linkedin.com
...which obviously doesn't work for display in a 'from' field on the front-end. Email clients like Apple Mail and Gmail show mail-noreply#gmail.com or member#linkedin.com, so that's what I was going for too.
Anyways, after some more research, I discovered that the 'from' field in a Zend Mail Message object always looks something like this:
"user account name" <user#email.com>
The part in < > is what I was after, but simply doing $from = $zend_message->from only gave me the user's account name (hence my original question). After some more playing around, this is how I finally got it working:
$from = $zendMessage->from;
$start = strpos($from, '<');
$email = substr($from, $start, -1);
$result = str_replace('<', '', $email);
Hopefully this will save someone some frustration. If anyone knows of a simpler way of doing this, please let me know.
This works well..
$senderMailAddress = null;
foreach ( $message->getHeader('from')->getAddressList() as $address ) {
if ( $senderMailAddress === null) {
$senderMailAddress = $address->getEmail();
}
}
The main problem here is that many email programs, relay agents and virus scanner along the way do funny stuff to an actually simple and well defined email standard.
Zend_Mail_Message extends to Zend_Mail_Part which has a method called getHeaders(). This will have all the data from an email stored in the head versus the body which is accessed with getContent() and the actual email message.
With this method you'll get an array of all the key/value pairs in the header and while developing you should be able to determine which header field you will actually want. Once you know that you can then get the actual field with getHeader('field_name') or with its actual name directly.
However, if you have to many different email senders you may want to stick with the complete header array though and evaluate multiple fields for the best result like if there's an "reply-to" address. Again there are many uncertainties because the standard isn't always obeyed.

phpMailer - How do you Remove Recipients

There are a lot of StackOverflow questions on this topic, but I couldn't find one that was able to help with the issue I'm having. The script that I'm writing sends out multiple emails to various recipients with different message contents.
I can get this working by re-initializing the phpMailer object multiple times, but what I'd like to be able to do is create the object a single time, and then re-assign the following fields:
$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->IsHTML(false);
$mail->Body = $message;
That way I can just run those four lines of code and then send the mail out, again and again, as many times as necessary. The Subject, IsHTML, and Body fields are easily changed, so the problem I'm having is in the AddAddress function.
As you can probably guess, after I send out the first email, changing recipients for future emails will result in those stacking onto the current list of recipients.
To put it simply, how can I remove the email addresses associated with my $mail object so that I can assign them each time while removing the old addresses?
Is there another function besides AddAddress that I can use that will just assign the addresses?
You can use clearAllRecipients( )
$mailer->clearAllRecipients( ); // clear all
im using this always before sending email to recipients:
// clear addresses of all types
$mail->ClearAddresses(); // each AddAddress add to list
$mail->ClearCCs();
$mail->ClearBCCs();
then im doing just this: (not using CC or BCC, $toaddress is just an array of recipients)
foreach($toaddress as $key=>$val) { $mail->AddAddress( $val ); }
im using PHPMailer 5.2

Categories