Do not recreate the wheel to send mails ! Use Swift Mailer library.
Swift Mailer integrates into any web app written in PHP 5, offering a flexible and elegant object-oriented approach to sending emails with a multitude of features easy to implement.
Learn in this article how to install and use this library.
Installation
The preferred way to install Swiftmailer is via Composer:
$ composer require swiftmailer/swiftmailer
or add to your composer.json file in the require block
"swiftmailer/swiftmailer": "v5.4.0",
or if you use Symfony 2
"symfony/swiftmailer-bundle": "~2.3",
If you don't use Composer download the zip file in https://github.com/swiftmailer/swiftmailer
Sending mails
The algorithm to send a message is very easy to understan. You create a Transport, use it to create the Mailer, then you use the Mailer to send the message.
The mails can contain plain/text or html/text according to the configuration.
If the SwiftMailer class doesn't exist, don't forget to require it with php if the composer autoloader didn't work
require_once 'lib/swift_required.php';
Sending mail from outlook (hotmail)
Sending mails from outlook is a little bit different, as you can see outlook uses TLS encryption instead SSL and uses the port 587.
$transport = \Swift_SmtpTransport::newInstance()
->setUsername('[email protected]')->setPassword('mypassword')
->setHost('smtp-mail.outlook.com')
->setPort(587)->setEncryption('tls');
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance()
->setSubject($param['title'])
->setFrom(array('[email protected]' => 'I am someone'))
->setTo(array('[email protected]' => "[email protected]"))
->addPart("<h1>Welcome</h1>",'text/html')
;
$result = $mailer->send($message);
Sending mail from google (gmail)
Google uses SSL encryption and the port 465. Is important to know that sometimes , the gmail accounts doesn't allow to send mails with swiftmailer and you need to uncheck the property "Allows the use on unsafe devices" in your gmail account.
$transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 465,'ssl')->setUsername('[email protected]')->setPassword('mypassword');
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance('Our Code World Newsletter')
->setFrom(array('[email protected]' => 'Our Code World'))
->setTo(array("[email protected]" => "[email protected]"))
->setBody("<h1>Welcome</h1>", 'text/html');
$result = $mailer->send($message);
Sending mail from zoho(zoho mail)
Zoho uses SSL encryption and the port 465.
$transport = \Swift_SmtpTransport::newInstance('smtp.zoho.com', 465,'ssl')->setUsername('[email protected]')->setPassword('mypassword');
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance('Test')
->setFrom(array('[email protected]' => 'Our Code World'))
->setTo(array("[email protected]" => "[email protected]"))
->setBody("<h1>Welcome</h1>", 'text/html');
$mailer->send($message);
A frequent question for the swiftmailer user is what is the smtp for hotmail, zoho or gmail (or what is the smtp for my email account) ?
Well, the following list contains the SMTP configuration lot of mail clients in the market :
PROVIDER | URL | SMTP SETTINGS |
---|---|---|
1&1 | 1and1.com | smtp.1and1.com |
Airmail | Airmail.net | mail.airmail.net |
AOL | Aol.com | Smtp.aol.com |
AT&T | Att.net | Outbound.att.net |
Bluewin | Bluewin.ch | smtpauths.bluewin.ch |
BT Connect | Btconnect.com | mail.btconnect.tom |
Comcast | Comcast.net | smtp.comcast.net |
Earthlink | Earthlink.net | smtpauth.earthlink.net |
Gmail | Gmail.com | smtp.gmail.com |
Gmx | Gmx.net | mail.gmx.net |
HotPop | Hotpop.com | mail.hotpop.com |
Libero | Libero.it | mail.libero.it |
Lycos | Lycos.com | smtp.lycos.com |
O2 | o2.com | smtp.o2.com |
Orange | Orange.net | smtp.orange.net |
Outlook.com (former Hotmail) | Outlook.com | smtp.live.com |
Tin | Tin.it | Mail.tin.it |
Tiscali | Tiscali.co.uk | smtp.tiscali.co.uk |
Verizon | Verizon.net | outgoing.verizon.net |
Virgin | Virgin.net | smtp.virgin.net |
Wanadoo | Wanadoo.fr | smtp.wanadoo.fr |
Yahoo | Yahoo.com | smtp.mail.yahoo.com |
Zoho | zoho.com/mail/ | smtp.zoho.com |
Main ource : http://www.serversmtp.com/en/what-is-my-smtp
a piece of cake isn't ? Please share this article if it was useful for you.