PEAR Mail_mime not showing appropriate version - email

I have some trouble with PEAR when I'm using the Mail_mime class to send out HTML/text mail with embedded images.
What I need script to do, is to provide an email with both a text and HTML version of the content. The content will be somewhat different.
The text version will contain some text, and an image attachment.
The HTML version will have a layout with some links and an embedded image. This image is the same as the attached image in the text version.
What I've got so far, is a script that send a plain text version and an HTML version. The text version is in fact not the text version which I'm telling it to send, but a stripped down version of the HTML email.
After some investigation, I found out that the plain text version actually gets sent in the email, but the email clients only show the stripped HTML version for some strange reason. It also seems like it's the addHTMLImage() method that breaks it. Without the embedded image the
What my code looks like, at the moment:
<?php
require 'Mail.php';
require 'Mail/mime.php';
$to = 'your#email.com';
$additional_headers = array(
'Subject' => 'Email subject',
'From' => 'my#domain.com'
);
$text_body = <<<TEXT
This is the plain text version.
TEXT;
$html_body = <<<HTML
<p>This is the HTML version</p>
<p><img src="image.jpg" alt="" /></p>
HTML;
$mime = new Mail_mime();
$mime->setTxtBody($text_body);
$mime->setHTMLBody($html_body);
$mime->addHTMLImage(file_get_contents('default.jpg'), 'image/jpeg', 'image.jpg', FALSE);
$body = $mime->get();
$headers = $mime->headers($additional_headers);
$mailer =& Mail::factory('smtp', array(
'host' => 'my.mailserver.net',
'port' => 0,
'auth' => TRUE,
'username' => 'myusername',
'password' => 'mypassword'
));
$res = $mailer->send($to, $headers, $body);
if (PEAR::isError($res)) {
echo 'Couldn\'t send message: '.$res->getMessage();
}
?>
As far as I know, there doesn't seem to be anyone else with this problem. Is there something wrong with my code, or with my PEAR installation?

Pass in the end of line character to the constructor of the Mail_mime class.
Windows in "\n\r" - or the other way around, can't remember

We ran into the same issue today and have done extensive research in to it. What we've uncovered is that Mozilla Thunderbird chokes on ABNF strings that don't follow the Content-Type capitalization pattern. In all other email clients we've tested, the text alternative worked as expected.
So, you're problem is likely your client. Hope this helps point you in the right direction.

Related

override the content of cakePHP3 emails

I have built a cakePHP3 application and I'm looking for some advice concerning the following situation. An e-mail configuration is setup as follows, to send a mail whenever a new order has been created.
public function orderCreated($order, array $attachments = [])
{
$email = $this
->locale($order->customer->language)
->to($order->customer->email)
->from(Configure::read('Webshop.email'), Configure::read('Webshop.name'))
->subject(__('Confirmation of your order {0}', $order->nr))
->template('orders' . DS . 'order_created')
->set(['order' => $order])
->attachments($attachments);
return $email;
}
Has worked perfectly fine for ages, but I'd like to add some new functionality to this specific e-mail (and others). Admins should be able to override the content of the orders/order_created.ctp template if they want to, so they can define the content of this e-mail theirselves. So they don't have to rely on the same content in the order_created.ctp template I did provide.
Creating a UI for saving their own e-mails is not the issue. But the issue I don't really know how I could provide the overrided content to the cakePHP3 mailer. I tried setting
->template(false)
->message($new_content)
But that didn't help. Message doesn't reach mailbox because the body is empty.
Thanks.
I think I'd go with something like this:
->template('orders' . DS . 'order_created_custom')
->set(['order' => $order, 'content' => $new_content])
And then in the new order_created_custom.ctp you would output the $content. This gives you the option to do some text replacements on the content based on the $order, as well as perhaps having things like a standard salutation or signature.

My template emails always send as plain text not HTML

I'm new to SendGrid and I'm trying to work out why the emails are always sent as plain text instead of the designed HTML transactional template I've made. If I use the sendgrid "preview/test" feature to send myself the email, it comes through looking exactly how it should, images, HTML etc.
However, when I use the PHP API to send the email, the email is sent but only in plain text. I use this line to tell SendGrid which template to use:
$mail->setTemplateId([my template ID]);
Are there some other things I should be setting via the PHP API before I finally call the following?
$sg->client->mail()->send()->post($mail);
Below is all my SendGrid code:
$from = new SendGrid\Email([website name], [website email address]);
$subject = "test subject";
$to = new SendGrid\Email(null, $email);
$content = "";
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$mail->setTemplateId([my template ID]);
//$mail->setASMGroupId(3057);
$mail->personalization[0]->addSubstitution("%email%", $email);
$mail->personalization[0]->addSubstitution("%hash%", $hash);
$apiKey = [my api key];
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
I dont put anything in for the $content because the content is dictated in the template.
If anyone has any idea why it's only sending a plain text version of my templated email when sent from PHP, any advice would be much appreciated.
Thank you
EDIT
I thought I might have to set the content-type in the header so I added:
$mail->addHeader("Content-Type", "text/html");
But this just gives me an error. Is there something I'm missing?
To force the email to be treated as HTML, just add an empty HTML body to the message:
$mail->setHtml(" ");
$mail->setText(""); //Sendgrid requires a plain-text body too
You can also use
$content = new \SendGrid\Content("text/html", "<html><body>some text here</body></html>");
$mail = new \SendGrid\Mail($from, $subject, $to, $content);
as shown in the example code:
https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php

perl Email::Send with gmail fails with images

this question is related to HTML image not showing in Gmail , but the answers there do not (or no longer) work.
the problem is that my perl program (below) fails when my html-formatted messages that I want to send off as soon as an img tag is included. it does not matter whether the image itself is served from the google drive or not. I am guessing that I am running into a novel gmail restriction (my script used to work), but I am not sure.
(of course, this problem is not that my recipients do not see the image; it is that the sending perl script aborts with an error---unfortunately, not with more information explaining to me why it is an error. of course, I understand that my recipients need to agree to view images to prevent tracking.)
so here are my questions:
is this a gmail or a perl module problem?
is it possible to send images, so that if my recipients want to see
images from my website (preferably not just images from my google drive as in my example below), they can agree to see this?
is it possible to get a better error message from google about why it fails?
here is the [almost] working code:
#!/usr/bin/perl -w
use strict;
use warnings;
use Email::MIME::CreateHTML;
use Email::Send;
use Email::Send::Gmail;
my $toemail = 'ivo.welch#gmail.com';
my $subject = 'testing image mailing';
my $bodytext= '<html> <body> fails: <img src="https://drive.google.com/open?id=1K4psrWWolTSqx_f6MQP-T1-FMFpegT1Trg" alt="photo" /> </body> </html>\n';
use readcredentials;
my $gmailaccount= readcredentials( 'account' );
my $gmailuserlogin= readcredentials( 'userlogin');
my $gmailpasswd= readcredentials( 'gmailpassword');
eval {
my $message = Email::MIME->create_html(
header => [
From => $gmailaccount,
To => $toemail,
Subject => $subject,
],
body => $bodytext,
);
my $sender = Email::Send->new(
{ mailer => 'Gmail',
mailer_args => [
username => $gmailuserlogin,
password => $gmailpasswd,
]
}
);
$sender->send($message);
};
warn "Error sending email: $#" if $#;
print STDERR "emailed!\n";
I ran your script, with some modifications to remove the dependency on readcredentials to make it run in my environment, and the email was delivered with no problem. Problems could be:
You could have some problems with your gmail credentials.
The script downloads and attaches the image, so perhaps your local environment is preventing the image from being downloaded.
But without your specific error message, it's hard to diagnose any further.

CakePHP HTML email rendered as view

I'm trying to send an html email but it is rendered as a view and then everything stops.
My code is very basic
$Email = new CakeEmail();
$Email->template('enquiry', 'default')
->emailFormat('html')
->to('mymail#mail.com')
->from('notme#mail.com');
$Email->send()
Text email works fine.
I followed the trail code through the cake codebase down to _render in CakeView. I think that something is preventing the rendered email template to be returned and echoes it to the screen instead.
I checked output_buffering in php.ini, it's set to 4096.
This is an inherited project, was working on the original server but not on mine.
Do you have any ideas about what else to look for? Thanks
Check Path of templete file app\View\Emails\html\enquiry.ctp
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->config('default');
$email->from(array('notme#mail.com' => 'Not Me'));
$email->to('mymail#mail.com');
$email->subject('Test mail');
$email->viewVars($message);
$email->template('enquiry', 'default');
$email->emailFormat('html');
$email->send();
Hope this will help!

Validation of Zend_Captcha_Image when using in form

I have the following code in a form in zend framework application.
$captcha = new Zend_Form_Element_Captcha('captcha', array(
'label' => "",
'captcha' => 'image',
'captchaOptions' => array(
'captcha' => 'image',
'font'=> APPLICATION_PATH . '/../public_html/assets/fonts/akbar.ttf',
'imgDir'=> APPLICATION_PATH . '/../public_html/assets/captcha/',
'imgUrl'=> '/assets/captcha/',
'wordLen' => 1,
'fsize'=>20,
'height'=>60,
'width'=>200,
'gcFreq'=>50,
'expiration' => 300)
));
and the display of the form element is as expected.
When I try to validate the form using the following code it always returns error even if I enter the captcha correctly.
if($this->getRequest()->isPost()) {
if($this->view->form->isValid($_POST)) {
Any solution on how to validate it correctly will be of great help.
Thanks
Nizam
I'm going to guess that you're also doing $captcha->generate(); before the isPost() check. The problem with this is that when you submit the form, you're generating a new CAPTCHA before checking the POST data, so the check will always fail because it's validating it against the new CAPTCHA. The solution is simply to move the generate call further down.
I put up a blog post a while back with some code examples of this component, see here - http://tfountain.co.uk/blog/2009/1/6/zend-captcha-image-experiences but the post is a couple of years old now so some things may have changed.
If this doesn't help, please edit your question to include a bit more code so we can see what else might be causing the problem.
Check this post: Zend Framework: Captcha problem
Basically you have to remove the "viewhelper" from the element.
ex.:
$form->getElement('captcha')->removeDecorator("viewhelper");