I am new to use phpmailer for sending bulk mails.
The execution stops and shows error if send() fails.
Is there any way to skip the error and continue with next email id.
I am using email id's from database.
You Can Catch Exception Mechanism, to continue with next Mail with appropriate logic
Posted by Phil
PHPMAiler uses Exceptions. Try to adopt the following
require_once '../class.phpmailer.php';
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
$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!
}
use can use phplist for bulk mailing with PHP mailer or without
click Here for more
In PHP, you can suppress error display by adding # to the function. This will usually prevent the error from being displayed.
For your situation, you can put the code in try..catch. In the catch section, check if the all the emails have been sent; if not, just continue with the next one. This also means that you need a way to track the total emails and the number of emails completed.
Hope this helps.
Related
I am trying to send emails through PHP and, while the messages get delivered, I'm having a small issue. In Gmail, and probably other email clients, the last line of the message (the signature) is getting clipped. By "clipped" I mean that the last line is hidden, with a sort of button that can be clicked to unhide the last line.
Is there some way to stop this from happening? This is my first time trying to send HTML emails through PHP, so I thought maybe there's some kind of syntax I don't know about.
I'm basically just using the phpmailer example code:
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<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Appreciate your advice.
No, there isn't really. The "clipping" as you refer to it is done by GMail (or whatever client) based on its own analysis of the content of the email - if something looks like a signature/quoted reply, it'll collapse it.
It has nothing to do with how you're sending the email.
Solution often repeated on SO is to eliminate any sort of "repetition" so people add unique timestamps in the footer, invisible images etc.
In my case Gmail exhibited a bug and would display this message because the first name was appearing inside the subject line and inside the body of the email.
And it wasn't even trimming the message, it was displaying the full message, but saying that it's trimmed.
Solution is to try to manually cut out chunks of the email and keep sending yourself tests until you stop seeing the notice and then work your way out from there.
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..
I'm using PHPmailer to send an email from a website to the website owner. It works fine to some addresses (e.g. my Gmail account), and it used to work to the owner's address, but he's recently changed from POP to IMAP and now he doesn't receive emails from the website. He does receive emails from other sources. This is the code:
$mail = new PHPMailer(true);
try {
$mail->AddAddress($to, 'Example To');
$mail->SetFrom('example#example.com', 'Example');
$mail->AddReplyTo('example#example.com', 'Example');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($message);
$mail->Send();
//echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Any advice much appreciated.
Thanks!
G
That has nothing to do with the PHP code. The IMAP protocol is just responsible for fetching the mails from the server as a user (with IMAP the mails stay on server ... you have an open stream for a long time ...).
So: did he switch to another email provider? Maybe it is in spam. Check the maillog! ;-)
I'm going to give this another try because my last question might have been confusing.
I have a simple web form consisting of the following some inputs (for now, pretend i have two inputs, name and file input). I want the user to upload a document (if possible restrict to .doc, .docx, .pdf, if this is not possible to accomplish, let's just restrict to .doc), and i want to restrict the size to under 2MB.
Let me rephrase this. The file to be attached is NOT on the webserver. It will dynamically be uploaded to a temporary folder, send via the mail script, and then deleted.
If this is possible to accomplish, please, I need all the help that I can get.
I've tried Swiftmailer, PHPMailer, PEAR, I can't seem to get them to work. All I need is a simple script to send an attached file, nothing more. No validation necessary, nothing.
Any help would be greatly appreciated.
Thanks a lot,
Amit
It is possible to do with all 3 libraries you listed (PHPMAiler, PEAR and Swiftmailer).
For PHPMailer you can see a tutorial here:
require_once '../class.phpmailer.php';
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
$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<P></P>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
AddAttachment will take a file from your server.
How to upload a file form an HTML form can be found here. Once your email is sent you can delete (unlink) the file from the server.
The PHP manual can help you to better undersand file uploads.
All you want to do is easy to achieve, but it's longer to explain than do it :) But with all the links I gave you you have everything you need. If you have specific questions let me know.
I've used two PHP email scripts and routing it through my SMTP server, when I do this though it sends two of the same email.
When I use mail() this doesn't happen, but I'd much rather use SMTP.
Any ideas why this may be occuring?
If you're setting the 'To' and/or 'Recipient' header multiple times, the SMTP server could interpret that as separate e-mail address, thus you'll receive the multiple e-mails.
I'd recommend using the PEAR Mail class. Very simple to use, and handles much of the work for you. It supports multiple backends including SMTP. Likewise, if you want to expand your class to send HTML emails, the Mail_Mime class handles this very nicely, providing methods to set the plain-text body and the HTML body (in case the recipient doesn't support HTML).
So if you're only using PHPMailer without editing it's code, it's not your script's fault. Maybe check your SMTP server's configuration?
function send_email($from, $fromname, $to, $subject, $body, $alt = '')
{
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 = 'localhost'; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
//$mail->AddReplyTo($from, $fromname);
$mail->AddAddress($to);
$mail->SetFrom($from, $fromname);
$mail->Subject = $subject;
//$mail->AltBody = $alt; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->Send();
echo 'Message Sent OK';
}
catch (phpmailerException $e)
{
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e)
{
echo $e->getMessage(); //Boring error messages from anything else!
}
}
That's the current function so far
Based on your code, if it's the class which is at fault, you'd expect to get 'Message Sent OK' twice (I can't see why that would happen though). If you don't, then I'd be looking at your SMTP server (maybe via a call to support).
I'm assuming you've disabled Reply-to to rule it out as a cause in this case? Note: I'm not suggesting that would affect anything (other than you likely being classified as spam).
Incidentally, I moved from PHPMailer to Swift Mailer some time ago & have never looked back. If you don't get any joy from support then I'd try at least testing with Swift Mailer.
I agree with what da5id said, why dont you take the second error message out. Further have you checked the receiver whether they REALLY get 2 messages?