I am developing my php website on my own computer using WAMP Server. I am using the Swiftmailer to Send mail. It sends the Mail, But it works very slowly. It sends a single mail in about 8-12 seconds.
I am using the following code
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',465, 'ssl');
$transport->setUsername('xyzmail#gmail.com');
$transport->setPassword('xyz12345');
$message = Swift_Message::newInstance();
$message->setTo("myownemail#gmail.com");
$message->setSubject("Test Mail from News Page");
$message->setFrom("xyzmail#gmail.com");
$message->setBody("This is the Message Body");
$mailer = Swift_Mailer::newInstance($transport);
if($mailer->send($message))
{
$output_msg = "Message Has been Sent Successfully!";
}
else
{
$output_msg = "Could Not Sent Message!";
}
One Thing I mention here that I am not sending so much text in the message body, I am sending the same single line text, as I have used in the above example.
Why it is working so slow, Is there any problem on my part? or do I need to use any other mail sender tool?
Not sure if SwiftMail is the problem here.
Maybe server configurations does not allow to send e-mails faster. I had a problem like this when i was in a shared server.
Because sending email fast (i.e. every 0,5 secs) can overload the server so the server administrator have configured to send an email every 10 seconds.
Another reason could be for spam security.
Related
Im using swiftmailer for sending mails via PHP. Most times it works fine. But sometimes, my mail Mails are landing in Spam-Folder.
Here my code, which sends the mails
function sendMail2($from,$to,$subject,$body,$attachment=NULL) {
require_once 'include_apth/swiftmailer/swift_required.php';
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($subject);
$message->setFrom($from);
$message->setTo($to);
$message->setBody($body, 'text/html');
if($attachment) {
$message->attach(Swift_Attachment::fromPath($attachment));
}
if(#$mailer->send($message)) {
return true;
}
else {
return false;
}
}
any ideas, why its landing sometimes in spam-folder?
I was having the same issues with deliver-ability of emails. Getting all of the correct DNS settings, headers and the like isn't enough.
Most, if not all cloud-hosting, and home-ISPs IP ranges are on various lists of IPs from where emails are not expected to be sent from - and so they are more likely to be marked as spam.
The easiest way to solve that is to use a dedicated service where emails are well known to come from, and that the company spends a great deal of effort to get email delivery properly configured.
There are a number of well known such companies, many of which offer significant free tiers, as long as you are well behaved and send appropriate emails that aren't being marked as spam, or bounce. If you are hosted on Amazon EC2 for example, you can get over 60,000 emails delivered per month via AWS/SES. My my own systems, I have an account, currently free, with Mailgun, and a 'limit' of 10,000 email sends per month.
For Swiftmailer, there are a number of plugins that can, for example, use a HTTP API to send email to the service, which then is sent over SMTP in the usual way - with greatly improved deliverabilty.
Add the below code and it will work perfectly
$headers =& $message->getHeaders();
$headers->addIdHeader('Message-ID', "b3eb7202-d2f1-11e4-b9d6-1681e6b88ec1#domain.com");
$headers->addTextHeader('MIME-Version', '1.0');
$headers->addTextHeader('X-Mailer', 'PHP v' . phpversion());
$headers->addParameterizedHeader('Content-type', 'text/html', ['charset' => 'utf-8']);
Solution get from the below question
Swiftmailer mails go into SPAM Folder
I need to send over 2000 mails and I am using Swift Mailer library for it.
We have own server and it has both SMTP and sendmail transports. I am using SMTP:
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
All mails are send fine to few people, but I'm afraid that we will be banned when we send mass mail.
I don't really know what means "banned" and how it looks like, but I'm afraid about the aftermath.
So, is it true, that such "ban" exists and how to implement mass mailing with Swift Mailer in a right way?
P.S.: My code looks like:
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance($message_theme)
->setFrom(array($sender => $name))
->setTo($emails)
->setBody($message_text,"text/html")
;
try {
// Send the message
$result = $mailer->send($message);
}
catch(Exception $e) {
echo "Error: ".$e->getMessage();
}
As I'm hoping you will not use this for spam!!!
Here are some things to do:
try to same different Emails (change name of the recipient in body)
send emails once every 3-4 seconds and not 100 emails/second - it should send 2000 emails in about 2-3 hours.
Blacklisting/graylisting does indeed exist and there are some best practices you can implement to try to avoid these issues. For 2,000 emails, as long as your headers are legitimate, there is nothing fishy going on in your body text, and your recipients are on different domains, you should not run into this issue. However, as khomyakoshka mentions, the above code is incorrect and you should use a loop to send each email. This avoids exposing your entire mail list to each user.
Some additional best practices:
1) Swiftmailer offers plugins (http://swiftmailer.org/docs/plugins.html) that will help you send bulk email. Of particular note are the Throttler and AntiFlood plugins.
2) If you intend on modifying the contents of the mail to tailor to the recipient, consider using the Decorator plugin (also mentioned on the plugins page) for this task.
Hope these tips help.
I'm using CakePHP to send automated emails to clients. It's been working great, but it seems some recipients aren't receiving our emails. So I decided to use the SMTP option for sending emails, and route emails through our email provider at Media Temple.
However, when trying to send email from a Media Temple account, I get the error "550- relay not permitted".
That sounds like the Media Temple server is just plain not allowing me to send mail through it.
That's odd because I've confirmed the username and password I'm using is correct and I can send mail via SMTP through it from my macmail client and iPhone mail client. I've also confirmed my cakephp email settings are correct, because I can send emails via SMTP with a gmail account with the exact same configuration in cakephp.
Any idea why I'm getting this error and how to resolve it?
Thanks
Here's the code that handles sending an email. I use this class just like the regular EmailComponent from within many different controllers.
class CanadafindsEmailerComponent extends EmailComponent
{
...
function send($content = null, $template = null, $layout = null) {
if(!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && is_array($this->bcc))
$this->bcc[]=TECHY_MONITOR_EMAIL;
else if (!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && !is_array($this->bcc))
$this->bcc=array(TECHY_MONITOR_EMAIL);
if(DEVSITE){//commented-out code are settings for smtp with gmail, which works fine
$this->delivery = 'smtp';
$this->smtpOptions = array(
'port'=>'465',//'465',
'timeout'=>'30',//'30',
'auth' => true,
'host' => 'ssl://mail.thenumber.biz',//'ssl://smtp.gmail.com',
'username'=>USERNAME,//'USERNAME#gmail.com',
'password'=>SMTP_PASSWORD//,
);
$this->to=$this->correctFormatOn($this->to);
$this->bcc=$this->correctFormatOn($this->bcc);
$this->cc=$this->correctFormatOn($this->cc);
$this->replyTo=$this->correctFormatOn($this->replyTo);
$this->from=$this->correctFormatOn($this->from);
}
return parent::send($content,$template,$layout);
}
function correctFormatOn(&$email){
if(is_array($email)){
$copiedEmail=array();
foreach($email as $singleEmail){
$copiedEmail[]=$this->correctFormatOnSingle($singleEmail);
}
$email=$copiedEmail;
}else{
$email=$this->correctFormatOnSingle($email);
}
return $email;
}
function correctFormatOnSingle(&$email){
$subEmails=explode(",",$email);
$fixedSubEmails=array();
foreach($subEmails as $subEmail){
$fixedSubEmails[]=preg_replace('/<?([^< ]+)#([^>,]+)[>,]?/i', '<$1#$2>', trim($subEmail));
}
$email=implode(",",$fixedSubEmails);
return $email;
}
}
The main problem I was having was that clients weren't receiving emails from our server, (and so I wanted to use an SMTP server to see if that would fix it, instead of the server's default email server).
But I managed to get those clients to receive emails from the server by making some other changes, thus removing the need to use SMTP and the Media Temple email server.
(As an FYI, I found that we were getting bouncebacks from client email servers stating Diagnostic-Code: smtp; 550 Access denied - Invalid HELO name (See RFC2821
4.1.1.1), but they were being sent directly back to the server, and going into the linux user account "www-data". (I read them in /var/mail/www-data, just using tail and vim). I found that postfix, which was handling the sending of emails, was marking the email sender's hostname (ie, "HELO name") as canadafinds3, the name I gave the server in Rackspace, not the domain name: canadafinds.com. So I changed that in /etc/postfix/main.cf, restarted postfix, et voila! No more bouncebacks from those particular clients, and everyone's happy again.)
I ended up writing my own PHP mail() script based on https://web.archive.org/web/20180401094709/http://www.dreamincode.net/forums/topic/36108-send-emails-using-php-smtp-direct/ in order to circumvent this error.
This question should have a simple, simple answer, but I just can't seem to get it working. Here's the scenario:
I created a php page -> this one: http://adianl.ca/pages/member_application.php. Once the form is completed, it proceeds to http://adianl.ca/pages/member_application_action.php, puts the data into a MySQL db, & thanks the user for their interest. Anyway, the form works perfectly, except for one little thing: whenever someone fills out that form, I want an email to be sent to sbeattie#adianl.ca, informing them that the form was filled out, & the email would include the form components. The problem is, I can NOT get an email to be sent to that address, or any address truth be told. Having a php page send an email should be a simple thing to do, but it's really baffling me.
Can anyone help me with this? This particular problem has been troubling me since yesterday, & if anyone can help me with this...man, thank you soooooo much.
JP
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.adianl.ca"; // SMTP server $mail->From = "webadmin#adianl.ca";
$mail->FromName = "Web Administration [ADIANL]";
$mail->AddAddress("sbeattie#adianl.ca");
$mail->AddCC("justinwparsons#gmail.com"); the #messageBody variable is just a string
If you want to have the email sent using the server's sendmail client, you can use mail.
If you want it to use another mail server, there are extensions to connect to an SMTP server. I use PHPMailer.
If mail doesn't work, it could be that the server is not set up to send email, or it could be that the mail server is rejecting emails sent from php, amongst other reasons.
this code can also be used to email in php so have a look, you can find many more examples of emailing in php look around
?php
$to = "recipient#example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
When sending mass mails with PHP, is it better to send each subscriber an e-mail (running a for loop through all the e-mail addresses) or is it better to just add all in BCC in a comma separated list and thus sending only one e-mail?
Thank you.
There's a good chance the number of addresses in the BCC field is limited on the SMTP server (to avoid spamming). I'd go with the safe route and send an e-mail to each individual subscriber. That will also allow you to customize the e-mail for each subscriber if needed.
Also note that mail() is probably not the best way to send bulk mail (due to the fact that it opens a new connection to the SMTP server each time it's invoked). You may want to look into PEAR::Mail.
Best practice is to send an email per recipient.
If it's a linux mail server, it can handle massive throughputs so volume should not be an issue unless it's a crap server!
If it's a shared webserver your host may not be happy - if this si the case I'd split it into chuncks and spread the send. If it's dedicated then do as you will :)
If the sending process for some reason failed (example cause might me unresolvable domain) for one of the BCC recipients, the whole operation would be canceled (which is in 99% of cases unwanted behavior).
I you send the emails in a PHP loop, even if one of the emails fails to send, other emails will be sent.
As the others says one mail per recipient is the better fit.
If you want a library to do the dirty job for you, give a try to SwiftMailer http://swiftmailer.org
Here is an example directly from the docs:
require_once 'lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
//Send the message
$numSent = $mailer->batchSend($message);
printf("Sent %d messages\n", $numSent);
/* Note that often that only the boolean equivalent of the
return value is of concern (zero indicates FALSE)
if ($mailer->batchSend($message))
{
echo "Sent\n";
}
else
{
echo "Failed\n";
}
*/
It also has a nice Antiflood plugin: http://swiftmailer.org/docs/antiflood-plugin-howto