I'm using Weblex/laravel-imap to load all my mails from Gmail.
But it takes really a lot of time to load the mails when I go to mywebsite.com/api/mails.
It took 12 seconds for loading only 5 mails.
This is my config/imap.php
'accounts' => [
'gmail' => [
'host' => 'imap.gmail.com',
'port' => 993,
'encryption' => 'ssl',
'validate_cert' => true,
'username' => 'username#gmail.com',
'password' => 'passwd',
],
..
],
..
.
This is the controller:
## web.php -> Route::get('/api/mails', 'Controller#imap');
use Webklex\IMAP\Facades\Client;
use Response;
public function imap(){
$mail = Client::account('gmail');
$mail->connect();
// dd($mail->getFolders());
$f = $mail->getFolder('INBOX');
$messages = $f->query()->since(now()->subDays(1))->get(); // today
$body = $subject = $date = $from = [];
foreach ($messages as $msg) {
array_push($body, $msg->getHTMLBody(true));
array_push($subject, $msg->getSubject(true));
array_push($date, $msg->getDate(true));
array_push($from, $msg->getFrom(true));
}
return Response::json([
'body' => $body,
'subject' => $subject,
'date' => $date,
'from' => $from
]);
}
How do I speed it up?
Related
config/mail.php
<?php
return [
'driver' => 'smtp',
'host' => '',
'port' => '',
'from' => ['address' => null, 'name' => null],
'encryption' => '',
'username' => '',
'password' => '',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
Usercontroller.php
public function welcome_email_confirmation($user)
{
$data['first_name'] = $user->first_name;
$data['email'] = $user->email;
$data['token'] = str_random(100); // Generate random string values - limit 100
$data['type'] = 'welcome';
$data['url'] = url().'/';
$data['locale'] = App::getLocale();
$password_resets = new PasswordResets;
$password_resets->email = $user->email;
$password_resets->token = $data['token'];
$password_resets->created_at = date('Y-m-d H:i:s');
$password_resets->save();
$data['subject'] = trans('messages.email.confirm_email_address');
Mail::send('emails.email_confirm', $data, function($message) use($data){
$message->to($data['email'], $data['first_name'])->subject
($data['subject']);
});
return true;
}
In this code I am simply doing sign up but I have got this error when click on sign up button Address in mailbox given [API base URL: ] does not comply with RFC 2822, 3.6.2.. How can I solve this problem? Please help me.
Thank You
in some similar situations I realized that the error was fired for sender email address and not destination email address.
In your config/mail.php you are not setting sender email credentials.
Regards.
I want to send email with some accounts to some targets.But when use this code all emails are delivered to first sender account only.
from() just change name of sender in message and it could not change sender account
while(true)
{
$config = array(
'driver' => 'smtp',
'host' => $smtp,
'from' => array('address' => $senders[$p], 'name' =>
$senderName),
'username' => $senders[$p],
'password' => $senderpasses[$p],
'port' => '587',
'encryption' => 'tls'
);
Config::set('mail', $config);
$data = [
'target' => $email[$m],
'text' => $text,
'title' => $title,
'sender' => $senders[$p],
'senderName' => $senderName
];
try {
Mail::send('emails.mail', ['data' => $data], function
($message) use ($data) {
$message->from($data['sender'], $data['senderName']);
$message->to($data['target'])-
>subject($data['titl']);
});
} catch (\Exception $e) {
echo $e->getMessage();
}
$m++;
$p++;
if ($p >= count($senders)) {
$p = 0;
}
if ($m >= count($email)) {
return ($m);
}
}
it send email just with first sender and other users are not used.
Emails are, by definition, sent from a single sender to multiple addresses, so it is not possible to achieve what you are asking for.
You have to send the mail multiple times, one for each sender. May I ask you what is the purpose of this scenario?
I try to send SMS by PHP, SMPP protocol and must use Net_SMPP library. My code:
$smsc = new Net_SMPP_Client('xxx.xxx.xxx.xxx', xxxx);
// Make the TCP connection first
$smsc->connect();
// Now bind to the SMSC. bind() and unbind() return the response PDU.
$resp = $smsc->bind(array(
'system_id' => '*****',
'password' => '*******',
'system_type' => ''
));
$message = "Привет!";
$message=iconv("utf-8", "UCS-2", $message);
$message=bin2hex ($message);
$ssm = Net_SMPP::PDU('submit_sm', array(
'source_addr_ton' => NET_SMPP_TON_ALNUM,
'dest_addr_ton' => NET_SMPP_TON_INTL,
'source_addr' => 'AnyName',
'destination_addr' => '7**********',
'short_message' => $message,
'dest_addr_npi' => 0x01,
'data_coding' => NET_SMPP_ENCODING_ISO10646 //UCS2 coding
));
$smsc->sendPDU($ssm);
But by phone comes message with braking-encoding (so-called foursquares).
So, changing data_coding by default
'data_coding' => NET_SMPP_ENCODING_DEFAULT
gives message "1f04400438043204350442042100" (hex-code of my message).
What did I go wrong?
Resolved!
$smsc = new Net_SMPP_Client('xxx.xxx.xxx.xxx', xxxx);
// Make the TCP connection first
$smsc->connect();
// Now bind to the SMSC. bind() and unbind() return the response PDU.
$resp = $smsc->bind(array(
'system_id' => '*****',
'password' => '*******',
'system_type' => ''
));
$message = "Привет!";
$message = iconv('utf-8', "UTF-16BE", $message);
$ssm = Net_SMPP::PDU('submit_sm', array(
'source_addr_ton' => NET_SMPP_TON_ALNUM,
'dest_addr_ton' => NET_SMPP_TON_INTL,
'source_addr' => 'Kristall',
'destination_addr' => '7**********',
'short_message' => $message,
'source_addr_npi' => NET_SMPP_NPI_ISDN,
'dest_addr_npi' => NET_SMPP_NPI_ISDN,
'data_coding' => NET_SMPP_ENCODING_ISO10646
));
$smsc->sendPDU($ssm);
I need to send two different emails one for the admin and the other email is a confirmation email for the user to confirm that we have received his request. I don't know how can I exactly send different emails to different email addresses in the same action in cakephp.
Code :
Controller
$Email = new CakeEmail('notifications');
$result = $Email->to(array('admin#example.com'))
->subject(__("Request Notification))
->send($message);
if($result){
$this->redirect('/pages/thankyou');
$companymsg= "Dear,Thank you for you interest we will contact you soon."
$Email = new CakeEmail('usernotifications');
$Email->to(array($email))
->subject(__(" Request"))
->send($companymsg);
}
Email Configuration
public $notifications = array(
'transport' => 'Mail',
'from' => array('notifications-noreply#example.com' => '(Notification)'),
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
'emailFormat' => 'html',
);
public $usernotifications = array(
'transport' => 'Mail',
'from' => array('no-reply#example.com' => 'My Project'),
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
'emailFormat' => 'html',
);
Try:
$Email = new CakeEmail('notifications');
$result = $Email->to(array('admin#example.com'))
->subject(__("Request Notification))
->send($message);
$companymsg= "Dear,Thank you for you interest we will contact you soon."
$Email = new CakeEmail('usernotifications');
$Email->to(array($email))
->subject(__(" Request"))
->send($companymsg);
$this->redirect('/pages/thankyou');
I've got a problem with sending mail using CakePHP. Everythings giong well, but i didn't receive any single mail , i tired to send to 2 different emails .
//WebsitesController.php
App::uses('AppController','Controller');
App::uses('CakeEmail','Network/Email');
class WebsitesController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Email','Session');
public function contact()
{
$this->set('dane', $this->Website->findById(4));
}
public function contact_email()
{ /* all data is taken from contact.ctp, I debuged all data below and it's correct */
$useremail = $this->data['Website']['useremail'];
$usertopic = $this->data['Website']['usertopic'];
$usermessage = $this->data['Website']['usermessage'];
$Email = new CakeEmail();
$Email->from(array($useremail => ' My Site'));
$Email->to('wigan#mail.com');
$Email->subject($usertopic); // all data is correct i checked several times
$Email->send($usermessage);
if($Email->send($usermessage))
{
$this->Session->setFlash('Mail sent','default',array('class'=>'alert alert-success'));
return $this->redirect(array('controller'=>'websites','action'=>'contact'));
}
$this->Session->setFlash('Problem during sending email','default',array('class'=>'alert alert-warning'));
}
}
//contact.ctp
<fieldset>
<?php
echo $this->Form->create('Website',array('controller'=>'websites','action'=>'contact_email'));
echo $this->Form->input('useremail',array('class'=>'form-control'));
echo $this->Form->input('usertopic',array('class'=>'form-control'));
echo $this->Form->input('usermessage',array('class'=>'form-control'));
echo $this->Form->submit('Send',array('class'=>'btn btn-default'));
echo $this->Form->end();
?>
</fieldset>
all seems to be fine, even if statement in function contact_email is approved.
configuration ( i'm working on localhost, xampp, netbeans 7.4)
public $smtp = array(
'transport' => 'Smtp',
'from' => array('site#localhost' => 'My Site'),
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
try this, you didn't set the config
public function contact_email()
{ /* all data is taken from contact.ctp, I debuged all data below and it's correct */
$useremail = $this->data['Website']['useremail'];
$usertopic = $this->data['Website']['usertopic'];
$usermessage = $this->data['Website']['usermessage'];
$Email = new CakeEmail();
$Email->config('smtp')
->emailFormat('html')
->from($useremail)
->to('wigan#mail.com')
->subject($usertopic); // all data is correct i checked several times
if($Email->send($usermessage))
{
$this->Session->setFlash('Mail sent','default',array('class'=>'alert alert-success'));
return $this->redirect(array('controller'=>'websites','action'=>'contact'));
} else {
$this->Session->setFlash('Problem during sending email','default',array('class'=>'alert alert-warning'));
}
}
Please follow the steps:
step 1: In this file (app\Config\email.php)
add this:
public $gmail = array(
'transport' => 'Smtp',
'from' => array('site#localhost' => 'Any Text...'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'youremail#example.com',
'password' => 'yourPassword',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
step 2: Add an email template (app\View\Emails\html\sample.ctp)
<body>
<h1>Email Testing: <?php echo $first_name?></h1>
</body>
step 3: Change the code in your method as shown below:
public function send_my_email() {
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->config('gmail'); //configuration
$Email->emailFormat('html'); //email format
$Email->to('receiveremail#ex.com');
$Email->subject('Testing the emails');
$Email->template('sample');//created in above step
$Email->viewVars(array('first_name'=>'John Doe' ));//variable will be replaced from template
if ($Email->send('Hi did you receive the mail')) {
$this->Flash->success(__('Email successfully send on receiveremail#ex.com'));
} else {
$this->Flash->error(__('Could not send the mail. Please try again'));
}
}