I have a system that sends e-mails through PHPMailer, and saves relevant messages data in a local DB (message-id, body, recipients, CCs, headers, etc..).
In this DB we save also incoming messages data, through a different process.
Now when I send an email that is supposed to be a reply to a previous message, I would like to include the whole history of thread quoted messages, right in the body.
With a PHP function, I'm able to get from DB all messages history in the correct in/out order, with their bodies, dates, sender/recipients, message-IDs, and so on.
For instance, here I'm sending a message with body "Sounds Good!":
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "ssl://smtp.example.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'john#doe.com';
$mail->Password = 'password123';
$mail->setFrom('john#doe.com', 'John Doe');
$mail->IsHTML(true);
$mail->Subject = 'Meeting';
$mail->Body = "Sounds good!";
$mail->Send();
Looking at messages raw data in my E-Mail client, seems that quoted old messages are included within the body of message.
If this is the right thing to do, and assuming that in the DB I have all the necessary data for this, is there a way to "pack" the $mail->Body property of PHPMailer to include old messages properly? Or has PHPMailer a method for this purpose?
Something like:
Sounds good!
On Mon 6 February 2023 at 11:06 Jane Appleseed <jane#appleseed.com> wrote:
> Sure, shall we at 3?
> On Mon 6 February 2023 at 11:04 John Doe <john#doe.com> wrote:
> > Can we met tomorrow?
Or using $mail->Body isn't the proper way?
Related
So i just received this error when trying to send an mail using PHPmailer from my site.
SMTP Error: The following recipients failed: XXXX
I tried to set $mail->SMTPAuth = true; to false but no result. And i tried to change the password for the mail account and update that in the sendmailfile.php but still the same.
It worked as intended two days ago, now i don't know why this is happening. Since there ain't any error code either i don't really know where to begin and since it did work..
Anyone who might know?
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->ContentType = 'text/html';
$mail->IsSMTP();
$mail->Host = "HOST.COM";
$mail->SMTPAuth = true;
$mail->Username = "MAIL_TO_SEND_FROM";
$mail->Password = "PASSWORD";
$mail->From = "MAIL_TO_SEND_FROM";
$mail->FromName = "NAME";
$mail->AddAddress($safeMail);
$mail->AddReplyTo("no-reply#example.COM", "No-reply");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$sub = "SUBJECT";
mail->Subject = ($sub);
I've encountered the same problem. Managed too fix it when i commented the next row:
$mail->isSMTP();
Noticed you already found an answer, however maybe this will fix the problem for other people.
This does prevent using your external SMTP server as RozzA stated in the comments.
Maybe your class.phpmailer.php file is corrupt. Download the latest version from :
https://github.com/PHPMailer/PHPMailer
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
It is a restriction from your SMTP server.
Sending e-mail messages is a vital part of the ever-growing Internet business. Sometimes, a large number of e-mails are required to be sent daily, even hourly. With this comes also the ever-increasing problem with the e-mail spam, and the countless number of junk messages users receive constantly.
The most common restrictions are:
150 e-mails per hour;
1500 e-mails per 24 hours;
50 recipients per message, where each recipient is counted as a separately sent e-mail message (e.g. if you have 50 recipients in a single message, this willcount as 50 sent messages);
One solution is to use a mailing list, then the restriction is 1500 e-mails for 24 hours. There's no restriction for the amount of emails sent per hour, i.e. you can send an email to a mailing list with up to 1500 recipients without a problem.
If you reach the hourly/daily limit you will get this error when trying to send further e-mails:
550 - Stop, you are sending too fast!
You will be able to send e-mails again, once the hour/day has passed.
Things you should know in order to avoid exceeding your limit:
The above e-mail restrictions are valid for the entire hosting account, and not for a single mailbox. This means, that if one of your mailboxes exceeds the allowed limit, you will not be able to send messages from any of your other e-mail accounts.
If, at any point you receive the afore-mentioned error message, it is highly recommended to stop all attempts to send messages from your mailboxes. If you continue trying, your messages will be left in a mail queue, which will have to clear first, before the server timer can reset and allow you to send e-mails again.
try inlcuding this
$mail->SMTPDebug = 1;
Just try to set SMTPAuth to false.
there is a slightly less probable problem.maybe this condition is caused by protection placed by your ISP.and you said it worked well two days ago.maybe that is the problem.try contacting your ISP.
or maybe its a problem with the recipients/senders email adresses
Here is some additional info about SMTP Auth
PLAIN (Uses Base64 encoding.)
LOGIN (Uses Base64 encoding.)
e.t.c - you can watch here http://en.wikipedia.org/wiki/SMTP_Authentication
For me solution was to set SMTPAuth to true for PHPMailer class
Please note in your lines i.e....
$mail->Username = "MAIL_TO_SEND_FROM";
$mail->Password = "PASSWORD";
$mail->From = "MAIL_TO_SEND_FROM";
Here at Line 1 and 3 you have to use same email address (You can't use different email address), this will work sure, I hope u r using different email address, (Email address must be same as username/password matching).
for Skip sending emails to invalid adresses; use try ... catch
$mail=new PHPMailer(true);
try {
$mail->CharSet = 'utf-8';
$mail->isSMTP();
$mail->isHTML(true);
$mail->Host = 'smtp.yourhost.com';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->Username = 'xxxx';
$mail->Password = 'xxxx';
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 0;
$mail->MailerDebug = false;
$mail->setFrom($absender, $name);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $message_other_player;
}
$mail->send();
// echo 'Message has been sent';
} catch (Exception $e) {
// echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
PHPMailer - Skip sending emails to invalid adresses
This is a little bit a newbie question I know. But however I couldn't find an answer to this question.
I have made some websites that leverage the functionality of automatic emailling. I have made this websites using PHP. Every website I do, in the mailling part, I come accross some "redundancies". Let me give an example, from the examples of PHPMailer library:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domail.com';
$mail->SMTPAuth = true;
$mail->Username = 'someuser#domain.com'; // SMTP username
$mail->Password = 'secret';
$mail->Port = 587;
$mail->setFrom('someuser#domain.com', 'Mailer');
$mail->addAddress('to#gmail.com', 'Joe User'); // Add a recipient
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
In these two statements, is where I thought there are redundancies: $mail->Username = "someuser#domain.com; $mail->Password = 'secret';" and $mail->setFrom('someuser#domain.com'). Here is my question? Why do I need to provide a "from" address if I already given a username and password. Shoudln't it simply log in to my email account and sent it? If I provide a user name, why do I also provide a "from" address? And vice versa.
Could someone explain the reason why mailling systems work like this? I have alson seen similar structure in python's standard mailling library.
Because "from" does not have to be the same as your log in username. You can try to send yourself a message with a different from value and check your inbox or spam folder to see the result.
In a Microsoft Exchange server (just an example), you are able to Send As many users mailboxes under your mailbox login when the correct permissions are applied. So in this case, if you were using PHPMailer to send mail to a Microsoft Exchange server and authenticate, you would use the same credentials to access your mailbox, but then specify the mailbox you would like to send as in the From variable.
As well as the above, From is part of the current SMTP standard (RFC 821) and MUST be used when communicating with an SMTP server. So this means all mail-servers using SMTP should ask for the From field to meet this standard despite if they understand who is sending the email.
I'm using PHPMailer to send emails to support when our server is pinged with updates (usually related to payments).
I'm trying to get related emails to display as a Gmail conversation to make it easier to support staff to follow previous updates/replies. I had originally assumed that it was based on subject, but that doesn't seem to make a difference.
My mailer code:
$mail = new PHPMailer; // create a new instance
$mail->isSMTP(); // set that we're using stmp
$mail->CharSet = 'UTF-8'; // make sure it's utf-8 encoded
$mail->Host = 'smtp.gmail.com'; // the hostname of the mail server
$mail->Port = 587; // set the smtp port number (587 for authenticated TLS)
$mail->SMTPSecure = 'tls'; // set the encryption to use, ssl (deprecated) or tls
$mail->SMTPAuth = true; // should we use smtp authentication?
$mail->Username = MY_EMAIL_LOGIN; // the user name for the smtp authentication
$mail->Password = MY_EMAIL_PASSWORD; // the password for smtp authentication
$mail->wordWrap = 70; // make sure we've no lines longer than 70 chars
$mail->Subject = "[Payment] - Player {$payment->user->name} ({$payment->user->id}) - Payment ID {$payment->id}";
$mail->Body = $htmlBody; // our html body
$mail->AltBody = $plainBody; // our fallback, plain-text body
$mail->setFrom( SUPPORT_EMAIL, 'Support' ); // who this is from
$mail->addReplyTo( SUPPORT_EMAIL, 'Support' ); // who we can reply to
$mail->addAddress( SUPPORT_EMAIL ); // who we're sending it to
$mail->isHTML( true ); // is this a html formatted email?
if( !$mail->send() )
error_log( "Can't send an email to support about payment {$payment->id} for user {$payment->user->id}" );
If I get 2 emails from the same user relating to the same payment (so the same subject), what I want it to come in as:
+-----------------------------------------------------------------------------+
| Support (2) | [Payment] Player foo (123456789) - Payment ID 123456789 |
+-----------------------------------------------------------------------------+
What it's actually coming in as:
+-----------------------------------------------------------------------------+
| Support | [Payment] Player foo (123456789) - Payment ID 123456789 |
+-----------------------------------------------------------------------------+
| Support | [Payment] Player foo (123456789) - Payment ID 123456789 |
+-----------------------------------------------------------------------------+
Am I missing something simple?
For anybody looking for what worked: following the link left by #ErikNedwidek will lead you to this blog post: http://www.sensefulsolutions.com/2010/08/how-does-email-threading-work-in-gmail.html
Two rules are specified:
The subject must be similar.
The sender must be a part of the thread OR in-reply-to must be used.
The first one is covered, as the subjects are identical, and the first part of the second should be covered as the sender is the same as the receiver.
There was also this part:
One interesting thing to note is that if you send email messages from Gmail they will also be threaded. The rules are exactly the same as when you receive them, except for one minor detail. If you send the same exact message twice with no subject prefix (e.g. subject is test not re: test) it does get threaded on the receiving end, but not on sending end. Conversely, if it does contain a prefix (e.g. re: test) it will be threaded in both cases.
I figured it wasn't getting threaded because the sender and receiver addresses are the same. Changing the receiver address to another test address meant that messages were threaded properly when received. Keeping the sender and receiver address the same, but adding another receiver address also meant that they got threaded properly. Just having one receiver address that matches the sender address wouldn't work though.
I tried adding a 're:' to the start of the subject, but that didn't make any difference. What did work however, was adding the 'In-Reply-To' header using:
$mail->addCustomHeader( 'In-Reply-To', '<' . SUPPORT_EMAIL . '>' );
Note that the < and > are important, as without it seemed to be ignored.
So to summarise:
foo#domain.com sending to foo#domain.com = no threading
foo#domain.com sending to bar#domain.com = threading
foo#domain.com sending to foo#domain.com and bar#domain.com = threading on both
foo#domain.com sending to foo#domain.com with re: pre-pended to the subject = no threading
foo#domain.com sending to foo#domain.com with header In-Reply-To set to foo#domain.com = no threading
foo#domain.com sending to foo#domain.com with header In-Reply-To set to <foo#domain.com> = threading
Full PHPMailer code:
$mail = new PHPMailer; // create a new instance
$mail->isSMTP(); // set that we're using stmp
$mail->CharSet = 'UTF-8'; // make sure it's utf-8 encoded
$mail->Host = 'smtp.gmail.com'; // the hostname of the mail server
$mail->Port = 587; // set the smtp port number (587 for authenticated TLS)
$mail->SMTPSecure = 'tls'; // set the encryption to use, ssl (deprecated) or tls
$mail->SMTPAuth = true; // should we use smtp authentication?
$mail->Username = MY_EMAIL_LOGIN; // the user name for the smtp authentication
$mail->Password = MY_EMAIL_PASSWORD; // the password for smtp authentication
$mail->wordWrap = 70; // make sure we've no lines longer than 70 chars
$mail->Subject = "[Payment] Player {$payment->user->name} ({$payment->user->id}) - Payment ID {$payment->id}";
$mail->Body = $htmlBody; // our html body
$mail->AltBody = $plainBody; // our fallback, plain-text body
$mail->setFrom( SUPPORT_EMAIL, 'Support' ); // who this is from
$mail->addReplyTo( SUPPORT_EMAIL, 'Support' ); // who we can reply to
$mail->addAddress( SUPPORT_EMAIL ); // who we're sending it to
$mail->addCustomHeader( 'In-Reply-To', '<' . SUPPORT_EMAIL . '>' ); // so we get threading on gmail (needed as to and from are the same address)
$mail->isHTML( true ); // is this a html formatted email?
if( !$mail->send() )
error_log( "[paymentsRealtimeUpdates] Can't send an email to support about payment {$payment->id} for user {$payment->user->id}" );
Unrelated small point - whatever address you set for the setFrom seemed to be ignored - Gmail would take whatever address is behind the MY_EMAIL_LOGIN login.
Check out the accepted answer here:
https://webapps.stackexchange.com/questions/965/how-does-gmail-decide-to-thread-email-messages
The author also has more information in a blog post. It's 3 years old, but hopefully the information still holds.
Well, this doesn't really have anything to do with PHP. This has to do with the way Gmail nests emails together in a "conversation"
You might want to have a look at questions like https://webapps.stackexchange.com/questions/16651/gmail-not-grouping-messages-with-the-same-subject-from-google-groups
So i just received this error when trying to send an mail using PHPmailer from my site.
SMTP Error: The following recipients failed: XXXX
I tried to set $mail->SMTPAuth = true; to false but no result. And i tried to change the password for the mail account and update that in the sendmailfile.php but still the same.
It worked as intended two days ago, now i don't know why this is happening. Since there ain't any error code either i don't really know where to begin and since it did work..
Anyone who might know?
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->ContentType = 'text/html';
$mail->IsSMTP();
$mail->Host = "HOST.COM";
$mail->SMTPAuth = true;
$mail->Username = "MAIL_TO_SEND_FROM";
$mail->Password = "PASSWORD";
$mail->From = "MAIL_TO_SEND_FROM";
$mail->FromName = "NAME";
$mail->AddAddress($safeMail);
$mail->AddReplyTo("no-reply#example.COM", "No-reply");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$sub = "SUBJECT";
mail->Subject = ($sub);
I've encountered the same problem. Managed too fix it when i commented the next row:
$mail->isSMTP();
Noticed you already found an answer, however maybe this will fix the problem for other people.
This does prevent using your external SMTP server as RozzA stated in the comments.
Maybe your class.phpmailer.php file is corrupt. Download the latest version from :
https://github.com/PHPMailer/PHPMailer
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
It is a restriction from your SMTP server.
Sending e-mail messages is a vital part of the ever-growing Internet business. Sometimes, a large number of e-mails are required to be sent daily, even hourly. With this comes also the ever-increasing problem with the e-mail spam, and the countless number of junk messages users receive constantly.
The most common restrictions are:
150 e-mails per hour;
1500 e-mails per 24 hours;
50 recipients per message, where each recipient is counted as a separately sent e-mail message (e.g. if you have 50 recipients in a single message, this willcount as 50 sent messages);
One solution is to use a mailing list, then the restriction is 1500 e-mails for 24 hours. There's no restriction for the amount of emails sent per hour, i.e. you can send an email to a mailing list with up to 1500 recipients without a problem.
If you reach the hourly/daily limit you will get this error when trying to send further e-mails:
550 - Stop, you are sending too fast!
You will be able to send e-mails again, once the hour/day has passed.
Things you should know in order to avoid exceeding your limit:
The above e-mail restrictions are valid for the entire hosting account, and not for a single mailbox. This means, that if one of your mailboxes exceeds the allowed limit, you will not be able to send messages from any of your other e-mail accounts.
If, at any point you receive the afore-mentioned error message, it is highly recommended to stop all attempts to send messages from your mailboxes. If you continue trying, your messages will be left in a mail queue, which will have to clear first, before the server timer can reset and allow you to send e-mails again.
try inlcuding this
$mail->SMTPDebug = 1;
Just try to set SMTPAuth to false.
there is a slightly less probable problem.maybe this condition is caused by protection placed by your ISP.and you said it worked well two days ago.maybe that is the problem.try contacting your ISP.
or maybe its a problem with the recipients/senders email adresses
Here is some additional info about SMTP Auth
PLAIN (Uses Base64 encoding.)
LOGIN (Uses Base64 encoding.)
e.t.c - you can watch here http://en.wikipedia.org/wiki/SMTP_Authentication
For me solution was to set SMTPAuth to true for PHPMailer class
Please note in your lines i.e....
$mail->Username = "MAIL_TO_SEND_FROM";
$mail->Password = "PASSWORD";
$mail->From = "MAIL_TO_SEND_FROM";
Here at Line 1 and 3 you have to use same email address (You can't use different email address), this will work sure, I hope u r using different email address, (Email address must be same as username/password matching).
for Skip sending emails to invalid adresses; use try ... catch
$mail=new PHPMailer(true);
try {
$mail->CharSet = 'utf-8';
$mail->isSMTP();
$mail->isHTML(true);
$mail->Host = 'smtp.yourhost.com';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->Username = 'xxxx';
$mail->Password = 'xxxx';
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 0;
$mail->MailerDebug = false;
$mail->setFrom($absender, $name);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $message_other_player;
}
$mail->send();
// echo 'Message has been sent';
} catch (Exception $e) {
// echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
PHPMailer - Skip sending emails to invalid adresses
I've used PHP to send emails before but never to send a full HTML page from another source and so I'm wondering where to start and a few other things.
I did a bit of research but my confusion isn't clearing up any.
Do I directly get the web-page contents and send that or can I use a setting to just use a URL?
What is the simplest method I could use and could someone show me an example?
Are there risks with sending an email like this to say... 5000 people and how do I change the header data with a return link to URL source?
The following line get the contents of a HTML page.
$mail->MsgHTML(file_get_contents('contents.html'));
Go here for full details:
http://phpmailer.worxware.com/index.php?pg=exampleagmail
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Disclaimer: I can't yet comment, so please forgive this being an "answer".
I think you're probably going to have to clarify your objectives a little bit here.
It sounds like what you want to do is first build a basic scraper unless you have access to the raw html file.
Basically you can use fopen("Url", "r"), fsockopen("url", 80), or use a curl handler to submit the page request.
From here, depending on your method, you would read the response and generate an HTML or multi-part e-mail.
As far as adding a link to the e-mail header, you can do that, but I have a feeling it's not going to do what you want it to. The way to do it will depend on how you decide to send the e-mail.
Ives' answer is nice.
There is one gotcha you really want to consider with emailing an html page.
Html emails and Html pages are two totally different school.
Html emails take you back 10 years (hello tables!) in what you can do to support as many email clients as possible.
It's very likely a straight email-a-webpage thing will look total crap on the recipient email..
and then you've got to consider embedding stylesheets, etc..