I'm having some strange issues with PHPMailer. I'm trying to send some content which I generate with PHP in HTML and plain text, but the body gets truncated. What's even stranger is that this happens only to the email I generate, if I put in there some generic content in much greater length, it gets sent properly. I must also mention, I did echo the content of both $content and $nohtmlcontent variables and everything is there like it should be, but when I receive email into my mailbox, it's truncated.
My PHP code for creating plain text and HTML email body:
$content="<BODY bgColor=\"#ffffff\"><FONT face=\"Verdana\" size=\"2\">";
$content.="Hello $name.<br /><br />Administrator of $url has created a new account for you.<br /><br />Your new account details:<br />";
$content.=$message."<br /><br />";
$content.="If you see something wrong, please reply with correct details and we will update your account.<br /><br />";
$content.="Have a nice day,<br />$url</FONT></FONT></BODY>";
$nohtmlcontent="Hello $name.\n\nAdministrator of $url has created a new account for you.\n\nYour new account details:\n\n";
$nohtmlcontent.=$usrEmail."\n\n";
$nohtmlcontent.="If you see something wrong, please reply with correct details and we will update your account.\n\n";
$nohtmlcontent.="Have a nice day,\n$url";
All variables are populated with proper data.
My PHPMailer code for sending email:
require_once("class.phpmailer.php");
$mail=new PHPMailer(true);
try {
$mail->AddAddress($email);
$mail->SetFrom('admin#example.com', 'example.com');
$mail->CharSet = 'UTF-8';
$mail->Subject = "New account for you";
$mail->IsHTML(true);
$mail->AltBody = $nohtmlcontent;
$mail->Body = $content;
$mail->Send();
return true;
}catch(phpmailerException $e){
trigger_error("PHPMailer failed: ".$e->errorMessage());
return false;
}
Result:
Hello 12 23.
Administrator of admin.localhost.dev has created a new account for you.
Your new account details:
Username: user1
Password: 123456
E-Mail Address: info#tourazore.com
Subscription Status: Not Verified (you must verify your email address before you can use your account)
Package: Free (limitations: 1 tour, 5 items)
First Name: 12
Last Name: 23
City 34
Country 45
Your verification link: http://admin.localhost.dev/verify-account/882672636ce2ad8c498f75a9b836ff055aecf573/
If you see something wrong, please reply with correct details and we will update you
Expected result:
Hello 12 23.
Administrator of admin.localhost.dev has created a new account for you.
Your new account details:
Username: user1
Password: 123456
E-Mail Address: info#tourazore.com
Subscription Status: Not Verified (you must verify your email address before you can use your account)
Package: Free (limitations: 1 tour, 5 items)
First Name: 12
Last Name: 23
City 34
Country 45
Your verification link: http://admin.localhost.dev/verify-account/882672636ce2ad8c498f75a9b836ff055aecf573/
If you see something wrong, please reply with correct details and we will update your account.
Have a nice day,
admin.localhost.dev
Please notice the extra content in the end.
I have also tried using PHP's function mail() to send the same content, it also gets truncated.
Any ideas?
SOLUTION: The PHP code generated really long line, after adding a few newline characters, the complete content got through.
Also this may not be related but you're using Body and not MsgHTML
$mail->Body = $content;
But it looks like your using HTML expressions in your content.
I'm fairly new to this but from what I've read to use HTML in PHPMailer you should use
$mail->MsgHTML = $content;
Although your text looks likes it is displaying fine and you solved your problem. But I thought I'd share incase it helps.
Some useful info here
https://phpbestpractices.org/
(scroll down to email info)
and here:
https://github.com/PHPMailer/PHPMailer
Related
I have a custom table with serial numbers in WordPress. I have successfully got the serial number to appear on both Order received page after testing with Stripe:
https://prnt.sc/9tz8i3BW7lJR
and it also appears on WooCommerce Admin Orders Page:
https://prnt.sc/jLyb5CQqSAH5
I am using the woocommerce_email_before_order_table action. (on customer_completed_order)
I have the code below and I have echoed the Order ID and the Custom TableName and they BOTH appear in the Thanks for shopping with us email.
It seems the $license query returns nothing and I just can't see why it won't appear.
If I exchange the $woo_order_id for the previous order no, like EMS-0051 the serial number appears.
Is this query too early and it hasn't been populated in the custom table before the query is run?
I cannot get it to work..can anyone see what I have done wrong, please?
The Thanks email and CODE are below.
https://prnt.sc/38wa50jTyr3U
<?php
add_action( 'woocommerce_email_before_order_table', 'add_serial_to_email', 25, 4 );
function add_serial_to_email( $order, $sent_to_admin, $plain_text, $email ) {
global $wpdb;
$ipn_tables = $wpdb->prefix ."ipn_data_tbl";
///////BELOW is using 'seq Order No' plugin..this checks if WOO O/N or plugins O/N.
if (empty($order->get_id)) {
$woo_order_id = $order->get_order_number();
}
elseif (empty($order->get_order_number)) {
$woo_order_id = $order->get_id();
}
///check order ID and Table name are there:
if (!empty($woo_order_id && $ipn_tables )) {
echo '<b>ORDER ID:</b> '.$woo_order_id.'<br>'; // echos the Order ID - appears on "Thanks for shopping with us" email
echo '<b>TABLE NAME:</b> '.$ipn_tables.'<br>'; // echo my Custom table name - appears on "Thanks for shopping with us" email
////But the below $license variable doesn't. I think it's a timing thing.
//$license = $wpdb->get_var(" SELECT serial_no FROM $ipn_tables WHERE woo_order_id = $woo_order_id " );
$license = $wpdb->get_var( $wpdb->prepare( "SELECT * FROM {$ipn_tables} WHERE woo_order_id = %s", $woo_order_id ) );
}
if ( $email->id == 'customer_completed_order' ){
printf( '<p class="custom-text">' .__( 'Your Software Serial Number: '.'<span style="color:red;font-weight:bold;font-size:15px">'.$license ));
}
}//function-END
?>
Forgot to show the MyPHPAdmin table:
https://prnt.sc/A4DH1v2STWrL
edit:
I should have mentioned that I put that license check for orderID and table just to see if it was being checked..it appears my get_var query isn't working (empty?) but that same query is used in the other PHP pages I edited.
Looks like I found the issue. It was the fact that 'woocommerce_payment_complete' hook
was too early BUT the hook 'woocommerce_pre_payment_complete' is called first after payment is made but before order status change and before the email is sent. :)
So all I changed in the add_action was change:
woocommerce_payment_complete' TO woocommerce_pre_payment_complete that's it.
And it worked.
part of the add_action updated code
I'm sending some emails via sendgrid and nodejs. I have a content to sent, but i'm unable to add a good body like below
Body:
Hi
You are receiving this email as a reminder to enter time for the day.
Best Regards, Operations Team
I'm able to send mail with the message only and couldn't find a way to add the "Best Regards,
Operations Team" lines. Please give an insight.
my code,
sgmail.setApiKey(process.env.API_KEY);
const msg = {
to: '########', // Change to your recipient
from: "######", // Change to your verified sender
subject: `Reminder for Time Entry`,
text: "Hi You are recieving this email as a reminder to enter time for the day.",
html: "<h1> Hi You are recieving this email as a reminder to enter time for the day.</h1>",
}
sgmail.send(msg);
In plain text you can add line breaks and they will display in the email. Note that you can use backticks (`) to write multiline strings in JavaScript. For HTML emails, you should wrap different lines in different paragraph (<p>) tags. Try the following:
sgmail.setApiKey(process.env.API_KEY);
const textMessage = `Hi
You are receiving this email as a reminder to enter time for the day.
Best Regards, Operations Team`;
const htmlMessages = `<p>Hi</p>
<p>You are receiving this email as a reminder to enter time for the day.</p>
<p>Best Regards, Operations Team</p>`;
const msg = {
to: '########', // Change to your recipient
from: "######", // Change to your verified sender
subject: `Reminder for Time Entry`,
text: textMessage,
html: htmlMessage,
}
sgmail.send(msg);
in a Sandbox environment nlapiSendEmail (defined inside a suitelet) returns SSS_AUTHOR_MUST_BE_EMPLOYEE even when the sender id is correct
My distribution is Kilimanjaro, with SuiteScript 1.0. I have an administrator role, when calling nlapiSenEmail() directly from the backend model with my employee id, the email was sent to my employee profile, but not to the specified email, which is really a company distribution list. Even when I did not specify the logged customer email, a copy was sent to the logged customer email, a gmail account. The backend model operates only for the MyAccount application. It's worth noting that in this scenario nlapiSendEmail() return value was undefined. In my experience, Netsuite is really ambiguous in its behavior returning values or just functioning in an expected way, due to the "execution context". So, with the same data I put my call inside a suitelet, and now I am having the return SSS_AUTHOR_MUST_BE_EMPLOYEE.
function sendEmailWithAPI(request, response)
{
var senderId = request.getParameter('senderId');
var to = request.getParameter('emailTo');
var subject = request.getParameter('subject');
var body = request.getParameter('body');
var cc = request.getParameter('emailCC');
var result = {success:false, errorInfo:''};
try
{
var sendingResult = nlapiSendEmail(senderId, to, subject, body, cc);
result.success = true;
}
catch (errorOnMailSending)
{
result.returnValue = sendingResult;
result.errorInfo = errorOnMailSending.details;
}
response.write(JSON.stringify(result));
}
What is the record type of the senderId? NetSuite only accepts Employee records as sender of script generated emails. Also in Sandbox accounts, the emails are re-routed to the logged in user, specific list, or not at all. This is actually based on the Company preference in your Sandbox account. The reason for this is Sandbox is usually used for testing and you don't want to send test emails to actual customers.
In the end the "problem" was that I was just working in the Sandbox, as I prepared a snippet to test email sending in production everything went right. In the sandbox you can still send emails specifying a list at the subtab "Email options"
with the option "SEND EMAIL TO (SEPARATE ADDRESSES WITH COMMAS)"
this is located at Setup > Company > Email Preferences.
I set a macro for my html email template like so:
message = message.Replace("\n", "<br>"); //tried with and without
message = Server.HtmlEncode(message); //tried with and without
MacroContext.GlobalResolver.SetNamedSourceData("Message", message);
But the email just renders the <br> tag as text.
I am definitely receiving the html email and not the plain text one.
If I don't manipulate the text and leave it go through with the \n intact and check the email in the sent queue, it displays as it should!
How do I get the newlines to the email template?
This is Example code that is sending the Email and works for me:
var emailTemplate = EmailTemplateProvider.GetEmailTemplate(EmailName, SiteContext.CurrentSiteID);
var message = "<br/>";
MacroResolver resolver = MacroResolver.GetInstance();
resolver.SetNamedSourceData("Message", message);
EmailMessage message = new EmailMessage();
message.From = resolver.ResolveMacros(emailTemplate.TemplateFrom);
message.Recipients = user.Email;
message.Body = resolver.ResolveMacros(emailTemplate.TemplateText);
message.Subject = resolver.ResolveMacros(emailTemplate.TemplateSubject);
EmailSender.SendEmail(SiteContext.CurrentSiteName, message, emailTemplate.TemplateName, resolver,
false);
Macro in the Email Template:
{%Message%}
Well the HtmlEncode will make your <br> look like text. At least you need to to reverse 1st and 2nd line.
I'm trying to send an email from a FirefoxOS App to share content generated by it.
Currently I'm using:
var createEmail = new MozActivity({
name: "new",
data: {
type : "mail",
}
});
But I haven't been able to find any way of appending or attaching content to this email
Thanks to #sebasmagri answer I learnt that the "mailto" URI accepts many more fields than I knew about. Specially interesting is the body and subject:
mailto:someone#example.com?
cc=someone_else#example.com
&subject=This%20is%20the%20subject
&body=This%20is%20the%20body
This allows me to set the different parts of the email as I wanted to.
The final code looks like:
var body = encodeURIComponent(JSON.stringify(event.target.result));
var createEmail = new MozActivity({
name: "new",
data: {
type : "mail",
url: "mailto:?subject=FiREST%20Request&body=" + body,
}
});
It looks like you can set attachments through data.blobs and data.filenames, and misc content (to, subject, content) through data.URI.
Detauls about the mailto: syntax can be found in the MDN entry on Email links.
Regards,
Edit May 2014
As the mail app was refactored, I've dropped the old broken code link in favour of MDN docs.