What is the maximum file size in sending an email with attachment in codeigniter?
When i test my website it will only accept 24MB file attachment but i have more than 24MB. What is the best solution for this matter? THANKS!
$id = 17;
$email = "stephencabalida80#gmail.com";
$this->load->library('email');
$attach = $this->m_admin->getAttachment($id);
$this->email->from('office#escaperoomfactory.net', 'Escaperoom Factory');
$this->email->to(urldecode($email));
$this->email->subject('Escaperoom Files');
$this->email->message('Hi, Thank you for purchasing our game/s. Have Fun!');
$path = 'upload/'.$id;
foreach ($attach as $row) {
$this->email->attach("".$path.'/'.$row->name."");
}
$this->email->send();`
Related
I am trying to limit the file size and type of attachments processed by phpmailer. The answer in this post does not solve the multiple attachments issue. The issue seems to be the form's input name with [] which converts form input to array. I am not able to write its correct syntax.
HTML is:
<input type="file" multiple="multiple" name="attach_file[]" />
PHP is:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
$name = $_POST['name'];
$mail = new PHPMailer(true);
try {
$mail->setFrom('abc#abc.org', 'abc');
$mail->addAddress('xyz#xyz.com', 'xyz');
// Attachments
foreach(array_keys($_FILES['attach_file']['name']) as $key) {
$source = $_FILES['attach_file']['tmp_name'][$key];
$filename = $_FILES['attach_file']['name'][$key];
$mail->AddAttachment($source, $filename);
}
$mail->isHTML(true);
$mail->Subject = 'Case History';
$mail->Body = $name;
$mail->send();
echo 'Message has been sent';
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
If in foreach loop I use this:
$maxsize = 2 * 1024 * 1024; // 2 MB
$types = array('image/png', 'image/jpeg', 'image/gif'); // allowed mime-types
if(filesize($filename) < $maxsize && in_array(mime_content_type($filename),$types)){
$mail->AddAttachment($source, $filename);
}
I get these errors :
filesize(): stat failed
mime_content_type(): failed to open stream.
No such file or directory
What is the correct way of writing the IF condition when input name is for multiple attachments. Thanks.
You should note that $filename is just the original name of the file that has been uploaded (e.g. photo.jpg) - but the filesystem pathname of the uploaded file is actually $source (always some random filename, sitting in the OS temp folder).
I need to be able to send one, maybe more, files stored on an Amazon S3 server as attachments in an email created using SendGrid.
The problem I have is that I'm not a web dev expert and the sparse PHP examples I can find are not helping me much.
Do I need to download the files from the S3 server to the local /tmp directory and add them as attachments that way, or can I pass the body of the file from the FileController and insert it as an attachment that way?
I'm not really sure where to start, but here's what I've done so far:
$attachments = array();
// Process the attachment_ids
foreach($attachment_ids as $attachment_id) {
// Get the file if it is attached to the Activity
if (in_array($attachment_id, $activity_file_ids)) {
$file = File::find($attachment_id);
$fileController = new FileController($this->_app);
$fileObject = $fileController->getFile($attachment_id);
error_log(print_r($fileObject, true));
$attachment = array();
$attachment['content'] = $fileObject;
$attachment['type'] = $fileController->mime_content_type($file->file_ext);
$attachment['name'] = explode(".", $file->filename, 2)[0];
$attachment['filename'] = $file->filename;
$attachment['disposition'] = "inline";
$attachment['content_id'] = '';
}
}
My next step would be to push the $attachment array to the $attachments array. Once $attachments is complete, iterate through it and add each $attachment to the SendGrid e-mail object (the e-mail is working fine without attachments, btw.)
Problem is, I'm not sure I'm going down the right road with this or if there's a much shorter and neater (and working) way of doing it?
FileController->getFile() essentially does this:
$file = $this->_s3->getObject(array(
'Bucket' => $bucket,
'Key' => $filename,
));
return $file['Body'];
Any help (especially code examples) would be greatly appreciated!
Okay, I've got a working solution to this now - here's the code:
// Process the attachment_ids
foreach($attachment_ids as $attachment_id) {
// Get the file if it is attached to the Activity
if (in_array($attachment_id, $activity_file_ids)) {
// Get the file record
$file = File::find($attachment_id);
// Get an instance of FileController
$fileController = new FileController($this->_app);
// Set up the Attachment object
$attachment = new \SendGrid\Attachment();
$attachment->setContent(base64_encode($fileController->getFile($attachment_id)));
$attachment->setType($fileController->mime_content_type($file->file_ext));
$attachment->setFilename($file->filename);
$attachment->setDisposition("attachment");
$attachment->setContentId($file->file_desc);
// Add the attachment to the mail
$mail->addAttachment($attachment);
}
}
Don't know if it will help anybody else, but there it is. The solution was to get the file from the S3 server and pass base64_encode($file['Body']) to the setContent function of an instantiated Attachment object, along with setting a few other fields for it too.
how could i change my code to only accept some extensions.
look my code :
<?php
ob_start();
$_SESSION['nomecomp'] = $_POST['nomecomp'];
$email_env = $_POST['email_env'];
if (isset($email_env)) {
//variaveis vindas da pagina
$varcritico = $_POST['varcritico'];
$nomecomp = $_POST['nomecomp'];
$chapa = $_POST['chapa'];
$funcao = $_POST['funcao'];
$setor = $_POST['setor'];
$unidade = $_POST['unidade'];
$deschelp = $_POST['deschelp'];
//variveis do modal
//$email_env = $_POST['email_env'];
//$senha_env = $_POST['senha_env'];
<td>$deschelp</td>
</tr>
</table>'";
/**
* PHPMailer multiple files upload and send example
*/
$msg = '';
//if (array_key_exists('userfile', $_FILES)) {
// Create a message
// This should be somewhere in your include_path
include ("lib/PHPMailerAutoload.php");
$mail = new PHPMailer();
i have tried add some codes , but dont made sucess , for example , could i try push the array and see if the extension are inside some array ?
thanks.
Presuming you want to check the uploaded file types before moving them:
$AllowedFileTypes = array("pdf","txt"); // build array
$FileName = $_FILES['userfile']['name']; // get filename of file input
$FileType = end((explode(".", $FileName))); // get file type/extension
if(in_array($FileType, $AllowedFileTypes)){ // check to see if file type is allowed
// perform copy/move file
}
else{
// ignore/alert/whatever
}
Note: You may have to amend the variables to suit your requirements.
If you wish to validate the file type/extension before submitting the form, take a look at jQuery validation for file input: https://stackoverflow.com/a/20929391/715105
I'm currently using Laravel and sendgrid in an application of mine.
I want to be able to receive emails with attachements and store them on the server. But the problem here is, HOW should I do this? I currently have a code written, but that code isn't working at all. I can receive the emails, but it doesn't store the attachments. Can someone help me out?
Here is the code I currently have:
public function ReceiveMail(Request $request)
{
//inserting the email into the database
// getting all of the post data
$files = $request->get('attachments');
// Making counting of uploaded images
$file_count = count($files);
//var_dump($files);
//die;
if($file_count > 0){
foreach($files as $key=>$value) {
$attachment[] = $value;
$destinationPath = 'public/uploads/pictures/rallypodium/website/mail/attachments/';
$extension = $attachment->getClientOriginalExtension();
$filename = $attachment->getClientOriginalName();
$uploadSuccess = $attachment->move($destinationPath, $filename.$extension);
}
}
return '250';
}
I have a simple page, which is sending an Email message and multiple attachments, through phpmailer.
I have to attach the multiple attachments to the Email message to send, and also upload these files o server at same time, For which i m using the following loop:
$MyUploads = array();
foreach(array_keys($_FILES['attach']['name']) as $key)
{ $Location="uploads/";
$name=$_FILES['attach']['name'][$key];
$filePath = $Location . $name;
$source = $_FILES['attach']['tmp_name'][$key]; // location of PHP's temporary file for
$tmp=$_FILES['attach']['tmp_name'][$key];
if($mail->AddAttachment($source, $name))
{if(move_uploaded_file($tmp, $filePath)){
$MyUploads[] = $filePath;}
else
{$MyUploads[]='';
echo "not uploaded";}
}
}
The problem is, when i use the function move_uploaded_file(), the files are uploaded to the server folder, but are not sent with the attachments. As i comment out this function the attachments are sended.
Can;t find out, why these two dnt work together. Please any body help
Here is the loop that sends Attachments, And move them to a target path, for further use. I hope any one else can be helped by this:
$MyUploads = array();
$numFiles = count(array_filter($_FILES['attach']['name']));
for ($i = 0; $i < $numFiles; ++$i) {
$target_path = 'uploads/' . basename($_FILES['attach']['name'][$i]);
if(move_uploaded_file($_FILES['attach']['tmp_name'][$i], $target_path)) {
echo "the file ".basename($_FILES['attach']['name'][$i])." has been uploaded<br />";
$MyUploads[] = $target_path;
echo $MyUploads;
$mail->AddAttachment($target_path);
}
}