Zend Framework Send Mail either Text or Html - zend-framework

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.

Related

phpmailer not handling Iphone subject line in emails

I am using the following script to email our website users:
function mailerExpressBlueHost(array $mailInputs){
require_once '../../includes/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsMail();
$mail->SetFrom('swag#sustainablewestonma.org');
$mail->addAddress($mailInputs['addAddress']); // use for production;
$mail->AddBCC("swag#sustainablewestonma.org"); // set BCC: counts as part of the 500 limit;
$mail->AddEmbeddedImage("../images/newswagimageSmall.jpg", "swag-logo");
$mail->Subject = $mailInputs['subject'] ;
$mail->Body = $mailInputs['body'];
$mail->IsHTML(true);
$mail->ContentType="text/HTML";
if(!$mail->send()) {
$msg = 'Message could not be sent.' . 'Mailer Error: ' . $mail->ErrorInfo;
}else{
$msg = 'Message has been sent';
}
$mail->ClearAddresses();
return $msg;
}
when viewing the email in ms outlook the email looks like:
but when viewed on an Iphone it looks like:
Is there a way to either hide or place in the header the subject line instead of it appearing in the body? (the subject line being: SWAG Mailing List Confirmation!)
This is just a difference between email clients. There's nothing you can do about it.
Separately, you are using a very old version of PHPMailer (so update it), and you can remove these lines from your script as they're not doing anything:
$mail->IsMail(); //This is the default, so does nothing
$mail->ContentType="text/HTML"; //Calling isHTML does this for you, but correctly
$mail->ClearAddresses(); //This does nothing as the instance is destroyed when it goes out of scope

recipient receives same mail twice while sending mail in yii 1.1

I am trying to send mail to recipient one at a time.My code is like this:
$message = new YiiMailMessage;
$message->view = "mail";
$params = array('sendEmail'=>$values);
$message->subject = $values['subject'];
$message->setBody($params, 'text/html');
$message->from =$values['email_from'] ;
$message->addTo($values['email_to']);
if(Yii::app()->mail->send($message)){
Yii::app()->user->setFlash('success','Success sending email. ');
$this->redirect(array('mail/admin'));
}else{
Yii::app()->user->setFlash('error','Error while sending email. ');
}
Mail is received by recipient but same mail is received twice.
Maybe you are using a create action to notify the user each time a new record is created. Verify Yii::app()->mail->send($message) appears one time in your action, for example:
public function actionCreate()
{
$model= new MyModel;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['SomeForm']))
{
$model->attributes=$_POST['SomeForm'];
if($model->validate()){
if($model->save()){
$message = new YiiMailMessage;
$message->setBody('<h1>mets-blog.com</h1>','text/html');
$message->subject = 'Service';
$message->addTo('mets#blog.com');
$message->from = 'your#email.com' you want
Yii::app()->mail->send($message);
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
maybe you are calling your action twice in your controller by render your view.

Showing Invalid Email in CakePHP

Hi please check my CakePHP code.
Code
//$from="my_email#domain.com";
$from="CakePHP Email<my_email#domain.com>";
$to="someone_email#domain.com";
$sendMessage="This is CakePHP test message";
$sendMessage =$message;
$Email = new CakeEmail();
$Email->template('default')
->emailFormat('html')
->to($to)
->from($from)
->bcc('learnphpchinu#gmail.com')
->subject('CakePHP test subject')
->send($sendMessage);
return true;
This email is not working properly. I am getting error message "Invalid email address CakePHP Email<my_email#domain.com>".
If i have used my_email#domain.com instead of CakePHP Email<my_email#domain.com> the email working fine but the from email is showing my_email in mail box.
I want to show CakePHP Email in my from email address. How could i fix that issue. Help me?
As it takes array as an argument for from name,change your $from to
$from = array('my_email#domain.com' => 'CakePHP Email');
Reference.
Try this >
if (filter_var($d['email_to'], FILTER_VALIDATE_EMAIL)) {
$this->Email->sendAs = 'html';
$this->Email->from = $d['email_from'];
$this->Email->to = $d['email_to'];
$this->Email->subject = $d['subject'];
$this->Email->send($d['message']);
}

Set Zend_Mime_Message as Zend_Mail body

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.

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');