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
Related
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!
I am developing a Joomla!3.0/3.1 component that allows people to book a golf lesson online. When they complete the booking and process paypal payment, I want to send them a receipt/confirmation email.
I can do this fine and send the information in html or plain text format. However I don't like the fact that I now have view (email content) data within my controller code.
Is it possible to have a sort of template view file which can be parsed and sent as the contents instead? or am I stuck with it in my code?
Thanks,
Chris
On admin side create a section for editing your email template with Joomla's default editor
you an create an editor on the backend and open an html file inside that, for changing the styles ,formats content etc for users.
keep your template file as an HTML file on server and email sending time something like below.
function send_mail_account_pages($email_title,$greeting_text,$subject,$email_content,$user_email){
$config = JFactory::getConfig();
$data['fromname'] = $config->get('fromname');
$data['mailfrom'] = $config->get('mailfrom');
$data['sitename'] = $config->get('sitename');
$path = JPATH_BASE."/email_tmpl/";
$fp = fopen($path."email_tmpl.html","r");
$fsize = filesize($path."email_tmpl.html");
$fcontent = fread($fp,$fsize);
fclose($fp);
$date_format = date("M.d, Y");
$fcontent = str_replace("{email_page_title}",$email_title,$fcontent);
$fcontent = str_replace("{date}",$date_format,$fcontent);
$fcontent = str_replace("{greeting_text}",$greeting_text,$fcontent);
$fcontent = str_replace("{email_content}",$email_content,$fcontent);
$fcontent = str_replace("{thanks_text}",THANKS_TEXT,$fcontent);
$return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $user_email, $subject, $fcontent,1);
}
This way you can make admin users to give access to change the template styles make sure admin users do not edit the {place holders}.
Hope its helps..
Add a configuration field to your component of type textarea or editor, and let the user write the email template. You should explain which tokens are available or at least provide a default text, such as
Dear {USERNAME},
today I received {NUM_MESSAGES} messages on your behalf,
and your profile has been viewed {NUM_PROFILEVIEWS} times.
Then simply replace the {TOKENS} in your code with str_replace.
I am trying to send an email using the Zend_Mail class that will include text in HTML format, a plain text alternative and inline images referenced in the HTML. I only want the images to show embedded, but not as attachments.
My current code is the following:
$mail = new Zend_Mail('UTF-8');
$mail->addTo($recipient);
$mail->setFrom($sender);
$mail->setSubject($subject);
$mail->setBodyText($text);
$mail->setBodyHtml($html);
$images = array('name1','name2','name3');
foreach ($images as $i){
$filename = Zend_Registry::get('config')->path->localimages . '/' . $i . '.png';
$at = new Zend_Mime_Part(file_get_contents($filename));
$at->filename = $i . '.png';
$at->type = 'image/png';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_BASE64;
$at->id = $i;
$mail->addPart($at);
}
$mail->send();
Since both setBodyText() and setBodyHtml() are used, Zend automatically generates a multipart/alternative MIME message. The images are then shown embedded on the HTML code but also as attachments.
I want the images to only show embedded. For that, I understand I would need to have the text and html inside a MIME part with type multipart/alternative, and then to set the type of the whole email to multipart/related. How can I achieve this in Zend 1.10? I know in ZF2 you only have to build a MIME multipart message and then use setBody() to that message, but in Zend 1.10 you must set either setBodyText() or setBodyHtml(). Is it possible to control how the MIME multipart message is created?
I'm using the code below which sends an html email using a template as the body. How can I include a text only version along with the html version? I'd like to use another template which would be all text to include.
Thanks
$transport = new Zend_Mail_Transport_Smtp('smtp.domain.com', $config);
$HTMLText = $html->render('forgot-password.phtml');
$mail = new Zend_Mail();
$mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
$mail->setBodyHtml($HTMLText);
$mail->setFrom('name#domain.com', 'Name');
$mail->addTo($userDataPost['user_email']);
$mail->setSubject('Your new password');
$mail->send($transport);
How about setBodyText(string) function?
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.