i´m trying to send an email from the schedule of laravel, all the API configuration works in controllers, but in schedule not
public function handle()
{
$lateLoans = ReaderRecord::where('estado', 'atraso')->get();
$stolenLoan = ReaderRecord::where('estado', 'robo')->get();
foreach ($lateLoans as $lateLoan)
{
$loanDate = new Carbon($lateLoan['entrega']);
$today = Carbon::today();
$mailData = [
'title' => 'Informe de préstamo',
'name' => $lateLoan->reader['nombre'],
'lastName' => $lateLoan->reader['apellido_paterno'],
'book' => $lateLoan->record->book['titulo'],
'idBook' => $lateLoan->record['id'],
'days' => $loanDate->diffInDays($today),
'endDate' => $lateLoan['entrega']
];
$email = $lateLoan->reader['email'];
$name = $lateLoan->reader['nombre'];
Mail::send('mail.loan.late', $mailData, function($message) use (&$email, &$name) {
$message->to($email, $name)->subject('Atraso de entrega de préstamo');
});
}
}
I don´t know why, but the solution was this
https://github.com/yabacon/paystack-php/wiki/cURL-error-60:-SSL-certificate-problem:-unable-to-get-local-issuer-certificate-(see-http:--curl.haxx.se-libcurl-c-libcurl-errors.html)
download the file https://curl.haxx.se/ca/cacert.pem
edit the php.ini and add this
curl.cainfo = your\absolute\path\cacert.pem
Related
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?
Let's start with example.
if I have fixed form parameter (name , email , phone) then Guzzle Post method code would be like this :
public function test(Request $request){
$client = new \GuzzleHttp\Client();
$url = www.example.com
$res = $client->post($url.'/article',[
'headers' => ['Content-Type' => 'multipart/form-data'],
'body' => json_encode([
'name' => $request['name'],
'email' => $request['email'],
'write_article' => $request['article'],
'phone' => $request['phone'],
])
]);
}
Above code is working perfectly.
But when don't have fix form parameter then How to send data using Guzzle ?
Foe example first time when I have submited form I have name , email , phone field. next time may be fields would be name , email , phone , father_name , mother_name, interest , etc.. . next time may be it would be name , email , father name
So How to work with this dynamic form field situation ?
Try this:
public function test(Request $request)
{
$client = new \GuzzleHttp\Client();
$url = 'www.example.com';
$body = [];
// exceptions, for when you want to rename something
$exceptions = [
'article' => 'write_article',
];
foreach ($request as $key => $value) {
if (isset($exceptions[$key])) {
$body[$exceptions[$key]] = $value;
} else {
$body[$key] = $value;
}
}
$res = $client->post($url.'/article',[
'headers' => ['Content-Type' => 'multipart/form-data'],
'body' => json_encode($body)
]);
}
I want to be able to send an automated email to all my users with the top 5 projects of my application. I wanna do this with a cron job on my server.
I have constructed my function in my Marketing class. I created an artisan command but I just have no idea if my method will work. I have no idea how I can get the data in my variables to send in the mail.
I am not sure if my variable $name, $email, $projects will work in the mail and if the mail will actually be sent every month. I just don't think this code will work. And I don't have time to wait a month to test it out :p
My SendNewslettterCommand:
public function handle()
{
foreach(User::all() as $user)
{
$name = $user->name;
$email = $user->email;
$projects = "a query to find top 5 projects of the week";
$Marketing = new Marketing;
$Marketing->sendNewsletter($name, $email, $projects);
}
}
My sendNewsletter function in my Marketing class:
public function sendNewsletter($name, $email, $projects)
{
// In template content you write your dynamic content if you use <mc:edit> tags.
$template_content = [];
$message = array(
'subject' => 'SP*RK - TOP 5 projecten van de maand!',
'from_email' => 'noreply#spark.com',
'from_name' => 'SP*RK',
'to' => array(
array(
'email' => $email,
'name' => $name,
'type' => 'to'
)
),
'merge_vars' => array(
array(
'rcpt' => $email,
'vars' => array(
array(
'name' => 'NAME',
'content' => $name
),
array(
'name' => 'PROJECTS',
'content' => $projects
)
)
)
)
);
MandrillMail::messages()->sendTemplate('newsletter', $template_content, $message);
}
If this is all correct my next steps would be to register my command in artisan by editing app/start/artisan.php and adding:
Artisan::add(new SendNewsletterCommand);
After this I should my command to my crontab
Is this correct or not?
I am using sync (local driver) for pushing up a queue in a update method of EmailCampaignController, which uses another method of the same controller named emailQueue
like this
Queue::push('EmailNewsletterController#emailQueue', array('campaign_id' => $campaign_id));
The emailQueue uses a foreach loop which runs correctly for once after that it gives error as if the $campaign_id is undefined
here is the emailQueue method
public function emailQueue($job, $data) {
// Queue Starts here
$emailCampaign = EmailCampaign::find($data['campaign_id']);
$emailCampaign->status = 'In Progress';
$emailCampaign->last_activity = Carbon::now();
$emailCampaign->save();
$data = $emailCampaign->emailCampaignNewsletter;
$contacts = $emailCampaign->contactList->contacts;
foreach ($contacts as $contact) {
$emailBody = [
'message' => [
'subject' => $data['email_title'],
'html' => $data['email_body'],
'from_email' => $data['from_email'],
'to' => [['email' => $contact['email_address']]]
]
];
$response = Mandrill::request('messages/send', $emailBody);
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
$contact->last_activity = Carbon::now();
$contact->save();
}
$emailCampaign->status = 'Sent';
$emailCampaign->save();
$job->delete();
// Ends here
}
What am I doing wrong here? why is it not working like a normal loop ?
The problem was with email_campaign_id to be null because $data['campaign_id'] was null the correct foreign key was $data['email_campaign_id'] that's what stopped the process - I should have tested it before putting it in the queue
after changing the code
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
to
EmailCampaignRecord::create([
'email_campaign_id' => $data['email_campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
the problem was solved
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'));
}
}