send email with attachment in cakephp - email

i want to send email with some attachment in cakephp.
here is my code:
$Email = new CakeEmail('default');
$Email->from(array('info#test.com' => $message['Message']['subject']))
->to($email)
->sender(array('info#test.com' => $message['Message']['subject']))
->replyTo(array('info#test.com' => $message['Message']['subject']))
->subject($message['Message']['subject'])
->attachments(array(
1 => 'http://test.com/files/message_file/file/9/56b22b15b3cec.jpg'
))
->send($message['Message']['description']);
but i face to this error when i run it:
File not found: "http://test.com/files/message_file/file/9/56b22b15b3cec.jpg"
Error: An Internal Error Has Occurred.

Use sever absolute path to file for attachment
$Email
->attachments(array(
1 => WWW_ROOT . 'files' . DS. 'message_file' . DS . 'file'.DS.'9'.DS.'56b22b15b3cec.jpg'
))

You need to use a filesytem path not URL for attachments.

Related

Why is attachment file on email blank?

My script sends email to a user with an attachment file.
The file name is hello.txt.
It contains "HELLO WORLD!! :D".
When I send the email, the file hello.txt is attached with the email.
But, when I open the file, hello.txt is blank and empty.
It should contain "HELLO WORLD!! :D"
The CODE
#!/usr/bin/perl
use MIME::Lite;
$to = 'abc#gmail.com';
$from = 'abc#gmail.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Type => 'multipart/mixed' #I SUSPECT, THE PROBLEM AT HERE
);
# Add your text message.
$msg->attach(
Type => 'text',
Data => $message
);
# Specify your file as attachement.
$msg->attach(Type => 'application/text', #I SUSPECT, THE PROBLEM AT HERE
Path => 'C:\Users\Desktop',
Filename => 'hello.txt',
Disposition => 'attachment'
);
$msg->send;
print "Email Sent Successfully\n";
Should I change Type or application/text?
Path => 'C:\Users\Desktop',
Path should be the path name of the file on your system. What you gave is only the directory name, where the file is located. Trying to read this will result in empty data - and thus empty content for the attached file. Instead you need to use
Path => 'C:\Users\Desktop\hello.txt',

People in cc are not receiving the mail using MIME::Lite api

Please find below my code snippet that send a mail to person and his friend in cc.
In the cc list I also have a DL.
use MIME::Lite;
$to = 'ABC#DOMAIN1.com';
$from = 'MAILER#DOMAIN2.com';
$subject = 'How are you doing';
$message = 'This is test email sent by Perl Script';
my #cc=('XYZ#DOMAIN2.com','DL#DOMAIN2.com');
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc =>\#cc,
Subject => $subject,
Data => $message
);
$msg->send('smtp','smtpserver', Timeout => 60 );
print "Email Sent Successfully\n";
The problem is the person and DL is cc are not receiving the mails.
Is there any log in the api MIME::Lite where I can check what is the error(if any)Or what do you think the problem can be?
Perhaps you want to take a closer look at the documentation for MIME::Lite. Here's the first example from the synopsis.
use MIME::Lite;
### Create a new single-part message, to send a GIF file:
$msg = MIME::Lite->new(
From => 'me#myhost.com',
To => 'you#yourhost.com',
Cc => 'some#other.com, some#more.com',
Subject => 'Helloooooo, nurse!',
Type => 'image/gif',
Encoding => 'base64',
Path => 'hellonurse.gif'
);
$msg->send; # send via default
The Cc parameter here is sent as a text string containing comma-separated email addresses. You are passing in a reference to an array of email addresses.

MIME::Lite - Mail to multiple recipients [duplicate]

Please find below my code snippet that send a mail to person and his friend in cc.
In the cc list I also have a DL.
use MIME::Lite;
$to = 'ABC#DOMAIN1.com';
$from = 'MAILER#DOMAIN2.com';
$subject = 'How are you doing';
$message = 'This is test email sent by Perl Script';
my #cc=('XYZ#DOMAIN2.com','DL#DOMAIN2.com');
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc =>\#cc,
Subject => $subject,
Data => $message
);
$msg->send('smtp','smtpserver', Timeout => 60 );
print "Email Sent Successfully\n";
The problem is the person and DL is cc are not receiving the mails.
Is there any log in the api MIME::Lite where I can check what is the error(if any)Or what do you think the problem can be?
Perhaps you want to take a closer look at the documentation for MIME::Lite. Here's the first example from the synopsis.
use MIME::Lite;
### Create a new single-part message, to send a GIF file:
$msg = MIME::Lite->new(
From => 'me#myhost.com',
To => 'you#yourhost.com',
Cc => 'some#other.com, some#more.com',
Subject => 'Helloooooo, nurse!',
Type => 'image/gif',
Encoding => 'base64',
Path => 'hellonurse.gif'
);
$msg->send; # send via default
The Cc parameter here is sent as a text string containing comma-separated email addresses. You are passing in a reference to an array of email addresses.

Using the new 'data' option for CakeEmail attachments with CakePHP 2.4.x

The CakeEmail help page states that the data option has been added as of 2.4, so you no long have to have a physical file to add an attachment to an email.
I've got the following code:
$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf', array('data' => $pdf)))
->send($this->request->data['email-message']);
But whenever I run that I get an Internal Error saying File Not Found: "".
I had a look at the source code (which I'm beginning to learn is often more useful than reading the documentation!): https://github.com/cakephp/cakephp/blob/master/lib/Cake/Network/Email/CakeEmail.php
Changing my code to:
$Email = new CakeEmail('default');
$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf' => array('data' => $pdf, 'mimetype' => 'application/pdf')))
->send($this->request->data['email-message']);
Notice on the attachments line, the array is assigned to the filename variable, rather than passed in as a parameter!
For completeness, if anyone else is reading this and is wondering how I am generating my PDF with CakePDF:
// Create PDF for attachment
$CakePdf = new CakePdf();
$CakePdf->template('claim', 'default');
//get the pdf string returned
$pdf = $CakePdf->output();

Image Upload with Zend_Service_Nirvanix

I can't seem to upload an image using Zend_Service_Nirvanix. Is it even possible?
I have a feeling that my problem has something to do with not being able to figure out how to set the UploadHost on the Transfer Service.
Any help is greatly appreciated! My deadline is July 16th!
Here is my code:
$nirvanix = new Zend_Service_Nirvanix(array('appKey' => $key,
'username' => $user,
'password' => pass));
$NSImfs = $nirvanix->getService('IMFS');
$options = array('sizeBytes' => filesize($source));
$storageNode = $NSImfs->getStorageNode($options);
$NSTransfer = $nirvanix->getService('Transfer');
$options = array('uploadToken' => $storageNode->getStorageNode->UploadToken,
'path' => $original,
'fileData' => file_get_contents($source));
$result = $NSTransfer->uploadFile($options);
Here is the error I keep getting:
Zend_Service_Nirvanix_Exception: XML
could not be parsed from response:
Server Error in '/' Application. The
resource cannot be found. Description:
HTTP 404. The resource you are looking
for (or one of its dependencies) could
have been removed, had its name
changed, or is temporarily
unavailable. Please review the
following URL and make sure that it is
spelled correctly.
Requested URL:
/ws/Transfer/UploadFile.ashx
in
/Applications/MAMP/bin/php5/lib/php/Zend/Service/Nirvanix/Response.php
on line 119
You're getting a 404?
Have you checked for an updated version of that library?
Try going into the libray and changing UploadFile.ashx to UploadFile.aspx. I don't think ashx is not a standard extension.
Maybe that will fix it.
There's a commercial upload tool from Aurigma that has support for file and image upload to Nirvanix. Here's the link (see Uploading to Nirvanix section there) to the help topic to check.
To do a local upload (rather than a web upload via the browser) you just have to call the putContents method passing the files data.
Example:
$nirvanix = new Zend_Service_Nirvanix(array('appKey' => $key,
'username' => $user,
'password' => pass));
$NSImfs = $nirvanix->getService('IMFS');
$response = $NSImfs->putContents($destination_file_and_path,
file_get_contents($source_file));
if($response->ResponseCode != 0)
{
echo 'Fail!';
}
You would only call GetStorageNode if you want to generate a token to pass a browser the upload token.