Set Zend_Mime_Message as Zend_Mail body - email

I'm trying to send email with inline images and attachments with Zend Framework 1.
$mail = new Zend_Mail();
$mail->setType(Zend_Mime::MULTIPART_MIXED);
$mail->setSubject('Message');
$mail->setFrom('user#example1.com', 'Example user #1');
$mail->addTo('user#example2.com', 'Example user #2');
And trying to make nested email, by this example.
message
mainMultipart (content for message, subType="related")
->htmlAndTextBodyPart (bodyPart1 for mainMultipart)
->htmlAndTextMultipart (content for htmlAndTextBodyPart)
->htmlBodyPart (bodyPart1 for htmlAndTextMultipart)
->html (content for htmlBodyPart)
->textBodyPart (bodyPart2 for the htmlAndTextMultipart)
->text (content for textBodyPart)
->fileBodyPart1 (bodyPart2 for the mainMultipart)
->FileDataHandler (content for fileBodyPart1)
Small Example:
$html = '<p>Hello</p>';
$bodyHtmlPart = new Zend_Mime_Part($html);
$bodyHtmlPart->type = Zend_Mime::TYPE_HTML;
$bodyMsg = new Zend_Mime_Message();
$bodyMsg->addPart($bodyHtmlPart);
// And other nesting.. ending with Zend_Mime_Message
Question:
How to set Zend_Mime_Message to Zend_Mail body? Below added couple Zend_Mail functions, not really helpful.
$mail->setBodyHtml( ? );
$mail->setBodyText( ? );
I tried to look at Zend_Mail_Message function, but look alike it only works with ZF2.

What about
$mailObject->setParts($mimeMessageObject->getParts());
?

For inline images here is a class someone wrote for it:
http://davidnussio.wordpress.com/2008/09/21/inline-images-into-html-email-with-zend-framework/
If your wanting to send as an attachment, then its plain and simple from the docs:
$mail = new Zend_Mail();
$at = $mail->createAttachment($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_BASE64;
$at->filename = 'test.gif';
$mail->send();
Notice how on both Zend's example and that class from the link both have the following set for the disposition.
Zend_Mime::DISPOSITION_INLINE;
Zend Docs: http://framework.zend.com/manual/1.12/en/zend.mail.attachments.html
Hope that helps.

Related

Concrete5.7.5.2 - Where to put form file attachment headers?

I build my email headers like this:
$txt_message .= $this->txt_message;
$html_message .= $this->html_message;
$mh = Core::make('helper/mail');
$mh->to($this->email_to, $this->site_name);
$mh->from($this->email, $this->name);
$mh->replyto($this->email, $this->name);
$mh->setSubject($this->subject);
$mh->setBody($txt_message);
$mh->setBodyHtml($html_message);
#$mh->sendMail();
Some posts say an attachment can be added with
$mh->addAttachment($file);
but $file must be a file object. How can I make the uploaded file a file object?
I also found this post:http://www.adrikodde.nl/blog/2012/mail-attachments-concrete5/
But I get errors for all Zend stuff. Is Zend Mail still available in C5.7?
Where do I put headers for a file attachment? Where can I find out more about what really sends the message (is it still a Zend Mail?) and what methods are available?
Thank you.
[SOLVED]
Thanks to Nicolai, here's a working example for attaching files:
$file = $_FILES['image']['tmp_name'];
$filename = $_FILES['image']['name'];
$importer = new \Concrete\Core\File\Importer();
$file_version = $importer->import($file, $filename);
$attachment = $file_version->getFile();
$mh->addAttachment($attachment);
//Delete the file if not wanted on server
$attachment->delete();
PS. Don't forget to check the file really selected/exists/uploaded before you try to send it!
if (!empty($this->image)) {
$importer = new \Concrete\Core\File\Importer();
$image_version = $importer->import($this->image, $file_name);
if ($image_version instanceof \Concrete\Core\File\Version) {
$attachment = $image_version->getFile();
$mh->addAttachment($attachment);
}
}
#$mh->sendMail();
To add the file to your filesystem, you should take a look at this
http://concrete5.org/api/class-Concrete.Core.File.Importer.html.
On the returned object (which is a FileVersion on success), you should be able to call getFile( ) to get the actual Concrete5 File object

Zend Framework Send Mail either Text or Html

Hey I'm using something like this to send either a text mail or and html but it send both in the html version . What i'm doing wrong ? How can i do to send only text in case the email can't receive html ?
$htmlPart = new MimePart($htmlBody);
$htmlPart->type = "text/html";
$textPart = new MimePart($textBody);
$textPart->type = "text/plain";
$body = new MimeMessage();
$body->setParts(array($textPart, $htmlPart));
$message = new Message();
$message->addFrom($from, "My Corp.")
->addTo($email)
->setSubject(mb_convert_encoding($subject,"UTF-8"));
$message->setBody($body);
$message->setEncoding("UTF-8");
$options = /* … */
$transport = new SmtpTransport();
$transport->setOptions($options);
$transport->send($message);
Add:
$message->getHeaders()->get('content-type')->setType('multipart/alternative');
after your setBody() call. Everything else looks okay. If it's still not working after that, please add some more information to your question about specifically what you're seeing in the email client.

Zend Mail 1.12 setReturnPath

When i set the Return Path with setReturnPath, the e-mail header of the generated e-mail is different from the SET value (info#test.com). Is this an issue of Zend Framework 1.12.3?
Or can i fix it with another setting / header?
$mail = new Zend_Mail();
$mail->setBodyHtml($html);
$mail->setReplyTo('info#test.com');
$mail->setReturnPath('noreply#test.com');
$mail->setFrom('info#test.com', '...');
$mail->addTo(...);
$mail->setSubject(...);
$mail->send();
Have you thought about the order of the code.
This code is untested my or my not work.
//Create Zend_Mail object.
$MailObj = new Zend_Mail();
$message= "<h1>Welcome to the example</h1><br>" .
"<p>An example email.</p>";
//Initialize parameters.
$fromEmail = "FROM_EMAIL_ADDRESS";
$fromFullName = "FROM_FULL_NAME";
$to = "YOUR_EMAIL_HERE";
$subject = "This is an example";
$MailObj->setBodyHtml($message);
$MailObj->setFrom($fromEmail, $fromFullName);
$MailObj->addTo($to);
$MailObj->setSubject($subject);
$MailObj->setReplyTo('info#test.com');
$MailObj->setReturnPath('noreply#test.com');

Bulk email in cakephp using sendgrid

I am using cakePHP to send smtp email with sendgrid. I would like to be able to execute a single send() to multiple recipients and allow sendgrid to use the vars to replace -name- with the names from a array.
In my cakephp controller method I am testing:
protected function fwtEmail() {
$config = 'sendGrid';
$subject = "test";
$Email = new CakeEmail('sendGrid');
$names = array('user#domain.com'=>'John','user2#domain' =>'Paul');
$Email->To($names);
$vars = array('Paul', 'John');
$Email->viewVars(array('name' => $vars));
$Email->from( array('admin#testdomain.com' => 'Jim') );
$Email->subject($subject);
$template = 'bulk';
$Email->template($template, 'default');
$Email->sendAs = 'both';
return $Email->send();
}
Here is a post on the SendGrid blog that does what you describe.
edit: apologies, this appears not to work in the latest cakephp. I'm trying to find a solution for you.

How to send email message to two email id Address in zend Mail

Hi I am new in zend framework.
I have 2 email id's and i want to send message to 2 mail ids in zend mail
this is my code
$to1='abcd#gmail.com'
$to2='xyz#gmail.com'
$mailObj = new Zend_Mail()
$mailObj->setSubject($subject)
mailObj->setBodyHtml($message)
$mailObj->addTo($to1, $name='test')
$mailObj->setFrom($from, $name = null)
$mailObj->send()
You have to add Recipients using array like below
$recipients = array('abcd#gmail.com','xyz#gmail.com')
$message = new Zend_Mail();
$message->setFrom('fake#email.com', 'My Fake Mailing List')
->setSubject($subject)
->setBodyText($body);
foreach($recipients as $each_recipient){
$message->addTo($each_recipient);
}
$message->send();
for detail documentation you can check Zend_Mail - adding recipients
let me know if i can help you more.
You can define receiver as an array, for example:
$mailObj->addTo(array('address1#example.com', 'address2#example.com'), 'test');