How to add multiple values to model before save in Laravel? - php

I have a custom fuction to parse incoming emails and their attachements. Of course one email may contain multiple attachement.
I have this code:
public function parseEmail() {
// read from stdin
$fd = fopen("php://stdin", "r");
$rawEmail = "";
while (!feof($fd)) {
$rawEmail .= fread($fd, 1024);
}
fclose($fd);
$parser = new Parser();
$parser->setText($rawEmail);
$email = new Email;
$email->to = $parser->getHeader('to');
$email->from = $parser->getHeader('from');
$email->subject = $parser->getHeader('subject');
$email->body_text = $parser->getMessageBody('text');
$attachments = $parser->getAttachments();
$filesystem = new Filesystem;
foreach ($attachments as $attachment) {
$filesystem->put(public_path() . '/uploads/' . $attachment->getFilename(), $attachment->getContent());
$email->attachement()->name = $attachment->getFilename();
}
$email->save();
}
Now this code can store only one attachement. But how can I update to store multiple attachements in model?

You can change this line $email->attachement()->name = $attachment->getFilename(); to $email->attachement()->name[] = $attachment->getFilename(); for pushing to array all attachments, after that you can serialize it and save, if this ok for businesses logic.

Related

problem with metadata of multiple audio files using laravel

I'm creating a music player using Laravel. I'm trying to implement 'laravel-getid3' package into my project. Here is my code which processes the file and uploads it to the database
public function store(Request $request)
{
$input = $request->all();
$datas = [];
$result = [];
if($request->hasfile('songs')){
foreach ( $request->file('songs') as $key => $file){
/*$trackInfo = new getID3($request->file('songs'));
$tifo = $trackInfo->extractInfo();
print_r($tifo);
*/
$nametag = $file->getClientOriginalName();
$name = explode('.',$nametag)[0];
$extension = $file->extension();
$filesize = $file->getSize();
$input['songs'] = time() .uniqid().'.' . $file->extension();
$location = $input['songs'];
$file->storeAs('public/songs',$input['songs']);
$datas[$key] = $name;
$datas[$key] = $extension;
$datas[$key] = $filesize;
$datas[$key] = $input['songs'];
$file = new MusicUpload();
foreach ($datas as $data){
$file->user_id = Auth::user()->id;
$file->filename = $name;
$file->extension = $extension;
$file->filesize = $filesize;
$file->location = $location;
//$file->save();
}
}
}
return response() -> json([
'uploaded' =>true
]);
}
This code can handle multiple files but is very limited i.e. can only gather a limited amount of info from the files. Trying to implement the package i.e. if I comment the other parts and uncomment the commented part and try to run the code leads to error as it can only handle a single file.
and I'm unable to loop the code so it can handle each file one by one. Any solutions for the particular code or any new solutions that combats both?
I see few issues in your flow
you are passing all the songs files array to the getID3, in each iteration
$trackInfo = new getID3($request->file('songs'));
this is wrong, by their docs you should pass only one file, the one you are iterating
i.e $trackInfo = new getID3($file);
your are overriding the $input['songs'] in every iteration, and you don't need it all, you can just save the location of each file in $location
You have the $datas array, and in every request file you iterating this $datas and it will cause duplication in DB (assuming all will work without any errors).
you iterating the requests songs as $file
and then you set it again and overriding it as $file = new MusicUpload(); and this is wrong.
please try this updates code:
public function store(Request $request)
{
if($request->hasfile('songs')){
foreach ( $request->file('songs') as $key => $file){
$trackInfo = new getID3($file);
$tifo = $trackInfo->extractInfo();
//dd($tifo);
$nametag = $file->getClientOriginalName();
$name = explode('.',$nametag)[0];
$extension = $file->extension();
$filesize = $file->getSize();
$location = time() .uniqid().'.' . $file->extension();
$file->storeAs('public/songs', $location);
$music_upload_file = new MusicUpload();
$music_upload_file->user_id = Auth::user()->id;
$music_upload_file->filename = $name;
$music_upload_file->extension = $extension;
$music_upload_file->filesize = $filesize;
$music_upload_file->location = $location;
$music_upload_file->save();
}
}
return response() -> json([
'uploaded' =>true
]);
}

SendGrid and PHP to multiple recipents

I'm using sendgrid to send out html emails from a document system I'm building. When a file uploads sendgrid needs to email all those associated with that case. I have everything working for individual emails and I can customs the template I have saved but cant send the email to multiple recipients
I have generated an array of recipients
$email = array(j.bloggs#bloggs.com, j.doe#me.net, d.smith#smith.co.uk);
I want to pass this into my sendgrid email object to send to them all
private function send() {
$sg = new \SendGrid(self::$key);
$response = $sg->client->mail()->send()->post(self::$mail);
return $response->statusCode();
}
public function file_saved($file="", $case="") {
self::$from = new SendGrid\Email($this->fromName, $this->fromEmail);
self::$to = new SendGrid\Email($this->toName, $this->toEmail);
self::$content = new SendGrid\Content("text/html", "Hello, Email!");
self::$mail = new SendGrid\Mail(
self::$from,
$this->subject,
self::$to,
self::$content
);
$str = "<p>A file has been successfully uploaded to {$case->case_name} ({$case->case_code}).</p>
<br />
<p>{$file->file_name} - ".size($file->file_size)."</p>";
self::$mail->personalization[0]->addSubstitution("-name-", $this->toName);
self::$mail->personalization[0]->addSubstitution("-str-", $str);
self::$mail->personalization[0]->addSubstitution("-btn-", "Download File");
self::$mail->personalization[0]->addSubstitution("-url-", HTTP.BASE_URL.DS.'uploads'.DS.$file->file_path);
self::$mail->setTemplateId("2f845487-6243-4562-b6fb-022185b7fde7");
if (!$this->send() == 202) {
return false;
}
else {
return true;
}
}
I tried to use the personalization->to and pass that the array but get the error
Call to a member function to() on null in includes/classes/mail.php on line <b>82</b><br />
I just created a loop that went through each recipient and sent them an email, not as efficient as generating a multiple to: list but for now its working until I get a response from the sendgrid dev team
$this->email = array(j.bloggs#bloggs.com, j.doe#me.net, d.smith#smith.co.uk);
private function send() {
$sg = new \SendGrid(self::$key);
$response = $sg->client->mail()->send()->post(self::$mail);
return $response->statusCode();
}
public function file_saved($file="", $case="") {
$str = "<p>A file has been successfully uploaded to {$case->case_name} ({$case->case_code}).</p>
<br />
<p>{$file->file_name} - ".size($file->file_size)."</p>";
self::$from = new SendGrid\Email($this->fromName, $this->fromEmail);
self::$content = new SendGrid\Content("text/html", "Hello, Email!");
foreach($this->email as $email_addr) {
self::$to = new SendGrid\Email($this->toName, $email_addr);
self::$mail = new SendGrid\Mail(
self::$from,
$this->subject,
self::$to,
self::$content
);
self::$mail->personalization[0]->addSubstitution("-str-", $str);
self::$mail->personalization[0]->addSubstitution("-btn-", "Download File");
self::$mail->personalization[0]->addSubstitution("-url-", HTTP.BASE_URL.DS.'uploads'.DS.$file->file_path);
self::$mail->setTemplateId("2f845487-6243-4562-b6fb-022185b7fde7");
if (!$this->send() == 202) {
$response[$address] = false;
}
else {
$response[$address] = true;
}
}
return $response; // contains true/false for each email address if sent or not.
}

Email using cron and including attachment to email from moodle

I would like to send emails only to users that have completed a specific course and add a pdf file (a certificate for completing the course) as attachment to the email, and do so at a specific time using moodle cron.
I have looked at some plugins to find out how it's done, but I'm still not sure how exactly I should do this.
I need:
1. to know how I would add an attachment to an email (and which API to use),
2. how I would use cron to send the emails to the desired group at a certain time,
3. how to retrieve users that have completed the course so that I could send emails (with attachment) to them.
Thanks in advance.
(I'm using moodle version 3.0)
This is an overview.
First create a local plugin. For example /local/yourplugin
https://docs.moodle.org/dev/Local_plugins
Then set up a message provider
https://docs.moodle.org/dev/Message_API
defined('MOODLE_INTERNAL') || die();
in local/yourplugin/db/messages.php
$messageproviders = array (
'coursecompleted' => array (
),
Then add an event observer - you will want to respond to the course_completed event
https://docs.moodle.org/dev/Event_2
in /local/yourpluginname/db/events.php
have something like
$observers = array(
array(
'eventname' => '\core\event\course_completed',
'callback' => 'local_yourplugin_observer::course_completed',
),
);
Now add the message code
Add something like this to '/local/message/classes/observer.php'
defined('MOODLE_INTERNAL') || die();
class local_yourplugin_observer {
/**
* Triggered when 'course_completed' event is triggered.
*
* #param \core\event\course_completed $event
* #return bool
*/
public static function course_completed(\core\event\course_completed $event) {
// Your code here.
$message = new \core\message\message();
$message->component = 'local_yourplugin'; // Name of your local plugin.
$message->name = 'coursecompleted'; // Name of message provider.
$message->userfrom = $USER;
$message->userto = $user;
$message->subject = 'message subject 1';
$message->fullmessage = 'message body';
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = '<p>message body</p>';
$message->smallmessage = 'small message';
$message->notification = '0';
$message->contexturl = 'http://GalaxyFarFarAway.com';
$message->contexturlname = 'Context name';
$message->replyto = "random#example.com";
$content = array('*' => array('header' => ' test ', 'footer' => ' test ')); // Extra content for specific processor
$message->set_additional_content('email', $content);
// Create a file instance.
$usercontext = context_user::instance($user->id);
$file = new stdClass;
$file->contextid = $usercontext->id;
$file->component = 'user';
$file->filearea = 'private';
$file->itemid = 0;
$file->filepath = '/';
$file->filename = '1.txt';
$file->source = 'test';
$fs = get_file_storage();
$file = $fs->create_file_from_string($file, 'file1 content');
$message->attachment = $file;
$messageid = message_send($message);
}
}

SWiFT + TWIG loop different senders

We have a script that monitors the database. In the database JSON data and fromName and fromEmail are stored. If the handledAt timestamp is null the email still has to be send. So we have created a cronjob that monitors the handledAt column. If it is null the email should be sent.
Our scripts works fine, however it is a bit slow. Therefore I was wondering if we are doing it in a proper manner.
Grab All emails that need to be sent
foreach($ResultSelectEmails AS $key => $value)
{
require_once '../library/Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader, array(
'cache' => '../tmp/cache/'
));
$Contents = $twig->render(stripslashes($Templ['templateHTML']), $EData);
$Subject = $twig->render(stripslashes($Templ['templateSubject']), $EData);
$WriteFile = fopen('logs/'.$PDFLogFile.'.html','w');
fwrite($WriteFile, $Contents);
fclose($WriteFile);
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.example.com', 25)
->setUsername('info#example.com')
->setPassword('examplePassword')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($Subject)
->setFrom(array($ResultSelectEmails[$key]['fromEmail'] => $ResultSelectEmails[$key]['fromName']))
->setTo(array($ResultSelectEmails[$key]['toEmail']))
->setBody($Contents, 'text/html', 'UTF-8')
;
if(isset($EmailAttachements))
{
foreach($EmailAttachements AS $key => $value)
$message->attach(Swift_Attachment::fromPath($value));
}
$result = $mailer->send($message);
}
In the script we create a separate transport for each email. Sometimes the mail server throws an error. Mainly that it can't login.
Can we improve the script performance?
Looking forward to your thoughts....
I see some calls that you shouldn't do in a foreach loop. I don't know how many mails you want to send but this is not optimal. As you're not processing the loop in parallel you can move a few lines outside which should prevent creating some instances over and over again. I don't know the exact purpose of your code and without any details it's hard to say if this is a noticable improvement, but you could give it a try:
require_once '../library/Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader, array(
'cache' => '../tmp/cache/'
));
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.example.com', 25)
->setUsername('info#example.com')
->setPassword('examplePassword')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
foreach ($ResultSelectEmails AS $key => $value) {
$Contents = $twig->render(stripslashes($Templ['templateHTML']), $EData);
$Subject = $twig->render(stripslashes($Templ['templateSubject']), $EData);
$WriteFile = fopen('logs/' . $PDFLogFile . '.html', 'w');
fwrite($WriteFile, $Contents);
fclose($WriteFile);
$message = Swift_Message::newInstance($Subject)
->setFrom(array($ResultSelectEmails[$key]['fromEmail'] => $ResultSelectEmails[$key]['fromName']))
->setTo(array($ResultSelectEmails[$key]['toEmail']))
->setBody($Contents, 'text/html', 'UTF-8')
;
if (isset($EmailAttachements)) {
foreach ($EmailAttachements AS $key => $value)
$message->attach(Swift_Attachment::fromPath($value));
}
$result = $mailer->send($message);
}

send email with attached files in ZF2

How to send email with text/plain, text/html and attaches in zf2 ?
I use this code to send email with smtp:
$files = $this->params()->fromFiles();
$smtp = new \Zend\Mail\Transport\Smtp();
$smtp->setAutoDisconnect(true);
$optn = new \Zend\Mail\Transport\SmtpOptions(array(
'host' => 'mail.myserver.com',
'connection_class' => 'login',
'connection_config' => array(
'username' => 'user#myserver.com',
'password' => 'mypassword',
),
));
$smtp->setOptions($optn);
$htmlPart = new \Zend\Mime\Part('<p>some html</p>');
$htmlPart->type = Mime::TYPE_HTML;
$textPart = new \Zend\Mime\Part('some text');
$textPart->type = Mime::TYPE_TEXT;
$i=0;
$attaches = array();
foreach($files as $file){
if ($file['error'])
continue;
$attaches[$i] = new \Zend\Mime\Part(file_get_contents($file['tmp_name']));
$attaches[$i]->type = $file['type'].'; name="'.$file['name'].'"';
$attaches[$i]->encoding = 'base64';
$attaches[$i]->disposition = 'attachment';
$attaches[$i]->filename = $file['name'];
$i++;
}
$parts = array();
if (count($attaches)>0) {
$parts = array_merge(array($textPart,$htmlPart),$attaches);
$type = Mime::MULTIPART_MIXED;
}
else{
$parts = array($textPart, $htmlPart);
$type = Mime::MULTIPART_ALTERNATIVE ;
}
$body = new \Zend\Mime\Message();
$body->setParts($parts);
$message = new \Zend\Mail\Message();
$message->setFrom('user#myserver.com');
$message->addTo('receiver#myserver.com');
$message->setSubject('subject');
$message->setEncoding("UTF-8");
$message->setBody($body);
$message->getHeaders()->get('content-type')->setType($type);
$smtp->send($message);
If I attach files, it sends files and contents but it shows plain and html text together in receiver inbox:
<p>some html</p>
some text
When I don't attach any files, it shows html text singly:
some html
Any help?
Currently there is no easy way in ZF2 (2.2) to combine a multipart/alternative body (html with text alternative for clients that cannot/do-not-want-to use html) with attachments.
If you add the 'multipart/alternative' content-type header to the entire message, in some email clients the attachment (link) will not be displayed.
The solution is to split the message in two, the body (text and html) and the attachment:
http://jw-dev.blogspot.com.es/2013/01/zf2-zend-mail-multipartalternative-and.html
an example:
$content = new MimeMessage();
$htmlPart = new MimePart("<html><body><p>Sorry,</p><p>I'm going to be late today!</p></body></html>");
$htmlPart->type = 'text/html';
$textPart = new MimePart("Sorry, I'm going to be late today!");
$textPart->type = 'text/plain';
$content->setParts(array($textPart, $htmlPart));
$contentPart = new MimePart($content->generateMessage());
$contentPart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $content->getMime()->boundary() . '"';
$attachment = new MimePart(fopen('/path/to/test.pdf', 'r'));
$attachment->type = 'application/pdf';
$attachment->encoding = Mime::ENCODING_BASE64;
$attachment->disposition = Mime::DISPOSITION_ATTACHMENT;
$body = new MimeMessage();
$body->setParts(array($contentPart, $attachment));
$message = new Message();
$message->setEncoding('utf-8')
->addTo('mywife#home.com')
->addFrom('myself#office.com')
->setSubject('will be late')
->setBody($body);
$transport = new SmtpTransport();
$options = new SmtpOptions($transportConfig),
));
$transport->setOptions($options);
$transport->send($message);
For the above you would need the following use statements:
use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;
use Zend\Mime\Mime;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;
ZF1 had a _buildBody() method in Zend_Mail_Transport_Abstract which did this automatically.
I have found it a better solution so I am writing it.
Namespace YourNamesapace;
use Zend\Mail\Message as ZendMessage;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;
use Zend\Mail\Transport\Sendmail;
class Testmail
{
public static function sendMailWithAttachment($to, $subject, $htmlMsg, $dir, $fileName)
{
$fileFullPath = $dir . '/' . $fileName;
// Render content from template
$htmlContent = $htmlMsg;
// Create HTML part
$htmlPart = new MimePart($htmlContent);
$htmlPart->type = "text/html";
// Create plain text part
$stripTagsFilter = new \Zend\Filter\StripTags();
$textContent = str_ireplace(array("<br />", "<br>"), "\r\n", $htmlContent);
$textContent = $stripTagsFilter->filter($textContent);
$textPart = new MimePart($textContent);
$textPart->type = "text/plain";
// Create separate alternative parts object
$alternatives = new MimeMessage();
$alternatives->setParts(array($textPart, $htmlPart));
$alternativesPart = new MimePart($alternatives->generateMessage());
$alternativesPart->type = "multipart/alternative;\n boundary=\"".$alternatives->getMime()->boundary()."\"";
$body = new MimeMessage();
$body->addPart($alternativesPart);
$attachment = new MimePart( file_get_contents($fileFullPath) );
$attachment->type = \Zend\Mime\Mime::TYPE_OCTETSTREAM;
$attachment->filename = basename($fileName);
$attachment->disposition = \Zend\Mime\Mime::DISPOSITION_ATTACHMENT;
$attachment->encoding = \Zend\Mime\Mime::ENCODING_BASE64;
$body->addPart($attachment);
// Create mail message
$mailMessage = new ZendMessage();
$mailMessage->setFrom('noreply#example.com', 'from Name');
$mailMessage->setTo($to);
$mailMessage->setSubject($subject);
$mailMessage->setBody($body);
$mailMessage->setEncoding("UTF-8");
$mailMessage->getHeaders()->get('content-type')->setType('multipart/mixed');
$transport = new Sendmail();
$transport->send($mailMessage);
}
}
Set the type from :
$attaches[$i]->type = $file['type'].'; name="'.$file['name'].'"';
To:
$attaches[$i]->type = \Zend\Mime\Mime::TYPE_OCTETSTREAM;
You will also want to confirm that if you are using an SMTP service that they allow attachements through the protocol.
E-Mail Messages with Attachments
$mail = new Zend\Mail\Message();
// build message...
$mail->createAttachment($someBinaryString);
$mail->createAttachment($myImage,
'image/gif',
Zend\Mime\Mime::DISPOSITION_INLINE,
Zend\Mime\Mime::ENCODING_BASE64);
If you want more control over the MIME part generated for this attachment you can use the return value of createAttachment() to modify its attributes. The createAttachment() method returns a Zend\Mime\Part object:
$mail = new Zend\Mail\Message();
$at = $mail->createAttachment($myImage);
$at->type = 'image/gif';
$at->disposition = Zend\Mime\Mime::DISPOSITION_INLINE;
$at->encoding = Zend\Mime\Mime::ENCODING_BASE64;
$at->filename = 'test.gif';
$mail->send();
An alternative is to create an instance of Zend\Mime\Part and add it with addAttachment():
$mail = new Zend\Mail\Message();
$at = new Zend\Mime\Part($myImage);
$at->type = 'image/gif';
$at->disposition = Zend\Mime\Mime::DISPOSITION_INLINE;
$at->encoding = Zend\Mime\Mime::ENCODING_BASE64;
$at->filename = 'test.gif';
$mail->addAttachment($at);
$mail->send();
Reference1
Reference2
Reference3

Categories