PHPMailer - how to get the username at the time of sending in order to welcome him? - phpmailer

I want to send a message to the user through PHPMailer to get the user's login to greet him - "Dear". $username. "!". How can i do this? Given that I used $mail->addAddress($email, $username); more than once.

I don't know exactly how you have your emails/usernames implemented but the basic answer is to use a for loop (or possibly a while loop depending on implementation). The following code assumes you have two arrays, one for emails and one for usernames and their indices are appropriately aligned.
for ($i=0; $i < count($emails); $i++) {
$message = "Dear ".$usernames[$i]."!";
//Recipients
$mail->addAddress($emails[$i]); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Hello!';
$mail->Body = $message;
$mail->AltBody = strip_tags($message);
$mail->send();
$mail->ClearAllRecipients( );
}
The trick is to use $mail->ClearAllRecipients( ); and then add the new recipient and their custom body/header.

Related

How to add a good email in sendgrid api in nodejs

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

Kentico Email Template Macros with HTML code

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.

htaccess only send Email with special domain

is it possible to check the Url with regex to validate?
URL looks: https://www.example.com/secure/index.phpID=name#specialDomain.com
in the domain our marketing division can input an mail address only with -> #domain.com or domain-marketing.com .
(marketing division input the email address in the Url, send the link to User, they input her data and send the form to the *#domain.com or *#domain-marketing.com)
thanks
Solve this issue:
$email = $_GET['ID']; // remember to filter this!
$regex = '#\w+#(?<domain>\w+\-?\w+\.\w+)#';
preg_match($regex, $email, $matches);
$domain = $matches['domain'];
if ($domain !== 'example-test.com') {
// Unauthorised
}

Issue with Token in createAndSend function

I have been building some pdf forms and using docusign for signatures. The issue I am having with the current one is that docusign goes to "finished" after my 2nd signer signs. The logs show that it is not sending to my 3rd signer but I am unsure how to code the getToken to include the 3rd signer after the 2nd has signed.
Any advice would be very helpful.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//$GLOBALS['_oneSigner'] = isset($_POST["autorefill"]);
createAndSend();
if(!$GLOBALS['_oneSigner']){
$GLOBALS['_showTwoSignerMessage'] = true;
}
} else if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (isset($_GET["envelopeID"])) {
// Display a message that we are moving on to Signer Number 2
// - unless the message is suppressed (by signing from the GetStatusAndDocs page)
if(isset($_GET['from_gsad'])){
getToken(getStatus($_GET['envelopeID']),$_GET['clientID']);
}else{
$GLOBALS['_showTransitionMessage'] = true;
getToken(getStatus($_GET['envelopeID']),2);
}
} else {
$_SESSION["embedToken"] = "";
}
}
To prevent an email sending and using "Embedded Signing" you need to specify a clientUserId that would tell DocuSign not to send the email and instead expect a call to generate embedded signing url.
See complete code example here - https://developers.docusign.com/esign-rest-api/guides/features/embedding

PHPMailer Body gets truncated

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

Resources