When I change providers I always have to make sendmail work and it's a real drag.
Are there any free providers I can use within my PHP code to send the same variables like keycodes for registration verification and stuff?
I tried looking on Google but I only found stuff that had form generators, which is not what I need.
I use PHPMailer with great success.
Right now Swift Mailer is one of the best mail solutions around for PHP. It's highly modular and can easily be configured to suit your needs. You could define multiple transports (in a config file for example) and use the one you need when you change providers.
Here is an example from the docs:
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// 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
$result = $mailer->send($message);
What you need is an outgoing authenticated SMTP server so your not using the hosts one and don't have to change the details each time.
Take a look at AuthSMTP (there are lots of other websites that provide this) and then use something like PHPMailer or Swift Mailer to send the email with authentication.
The problem is that a lot of ISPs block connections on port 25 (default smtp) to servers other than their own. Has to do with spam blocking etc.
Related
I want to send mails from different accounts in my Laravel application.
Therefore I created a new swiftmailer transport
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.example.org', 25))
->setUsername('your username')
->setPassword('your password')
;
// Create the Mailer using your created Transport
$swift_mailer = new Swift_Mailer($transport);
When I send the message directly from the Swift_Mailer object
// Create a message
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['john#doe.com' => 'John Doe'])
->setTo(['receiver#domain.org', 'other#domain.org' => 'A name'])
->setBody('Here is the message itself')
;
// Send the message
$result = $swift_mailer->send($message);
I get a mail that is actually send from my mail server:
However, when I send a mail from a Laravel Mailer (as explained here) using the same Swift_Mailer object from above:
$view = app()->get('view');
$events = app()->get('events');
$mailer = new \Illuminate\Mail\Mailer($view, $swift_mailer, $events);
$mailer->alwaysFrom('john#doe.com', 'John Doe');
$mailer->alwaysReplyTo'john#doe.com', 'John Doe');
$mailer->to('my_email#test.com')->send(new \App\Mail\Test);
then it appears in Gmail, that the mail wasn't really send from my mail server:
When I click on details it tells me that
The sender domain is different from the domain in the "From:" address.
I actually own both domains. In my .env file the FROM address is ****.com and I wanted to create a new mail from a ****.net address.
Why does Gmail think I have send a mail via my ****.com address if use the Laravel Mailer class?
Note: This is not a duplicate of multiple mail configurations - this is in fact where I started looking, but the 5 year old answer was not quite working and I gave this answer, but I am stuck with the above described problem.
I solved it as follows:
I compared first the header of both mails, I realized that in the mail using the Mailer object I had a respond address with the ***.com domain.
spf=pass (google.com: domain of respond#****.com designates **** as
permitted sender) smtp.mailfrom=respond#****.com Return-Path: <respond#***.com>
Then I checked the Mailer class (with hindsight, I wonder why I didn't look here first..) and I found the following snippet that I added some month ago:
$this->withSwiftMessage(function ($message) {
$message->getHeaders()
->addTextHeader('Return-Path', 'respond#****.net');
});
So I just had to remove it, now everything works.
In summary, I was convinded the problem was in the Mailer class, but it was simply inside the build method of the Mailable that I was using.
I've searched high and low and I can't seem to find a working example of a swiftmailer script that uses a hotmail smtp, I have tried using the google one's in examples and upating the port, user and password but the page just hangs until it times out.
My code:
require_once 'lib/swift_required.php';
// Create the SMTP configuration
$transport = Swift_SmtpTransport::newInstance("smtp.live.com", 465, "TLS");
$transport->setUsername("xxxx#hotmail.com");
$transport->setPassword("XXXXXX");
// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
"XXXX#company.com" => "xxx",
));
$message->setCc(array("another#fake.com" => "Aurelio De Rosa"));
$message->setBcc(array("boss#bank.com" => "Bank Boss"));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
$message->setFrom("XXXXX", "XX XXXXXXX");
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);
// Show failed recipients
print_r($failedRecipients);
I expect you need to set your encryption param correctly. For port 465 use ssl; for 587, tls. hotmail isn't any different to anywhere else - your code would not work anywhere.
I am teaching myself php and how to use Swiftmailer. I have followed the doc and added the code below to my php file, but it returns array( ). I can't figure out what could be wrong and how to check. I don't seem to have access to the remote server's ini file from the hosting provider. Would appreciate any help in determining what may be wrong and how to correct. thanks!
require_once 'Swift/lib/swift_required.php';
//Use simple mail transport
$transport = Swift_MailTransport::newInstance();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo("xxx#yyy.com");
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
//$message->setFrom($email);
$message->setFrom("xxx#zzz.net");
$message->setReturnPath('xxx#zzz.net');
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);
// Show failed recipients
print_r($failedRecipients);
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
I was trying to find an easier way to send e-mails to all my clients using our database (MySQL). I wanted to see if there is a way that it can select all the e-mails of my clients and I can add message, Subject and send it to all of my clients from my website rather than copying each of the mail.
Is there a way to integrate SMTP to do this? either using PHP or javascript.
Thanks.
Yes, there are about 5,247 ways. See these:
PEAR Mail
SwiftMailer
PHPMailer
Zend_Mail
Those are all good (and not the only ones). It is up to you to pick the one that best suits your purpose, there is no "single best" library.
I use SwiftMailer .. it works wonders for me.
* Send emails using SMTP, sendmail, postfix or a custom Transport implementation of your own
* Support servers that require username & password and/or encryption
* Protect from header injection attacks without stripping request data content
* Send MIME compliant HTML/multipart emails
* Use event-driven plugins to customize the library
* Handle large attachments and inline/embedded images with low memory use
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";
}
read more here ..
http://swiftmailer.org/docs/batchsend-method