It is common to use an smtp library to send emails in php. One such popular library is Swift mailer. It also facilitates the usage of gmail smtp settings, and attachments. Just download the library and try this sample code.
<?php
require_once 'lib/swift_required.php';
// Using smtp
$transport = Swift_SmtpTransport::newInstance('my_smtp_host.com', 25)
// Using Gmail
// $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('my_smtp_username') // or your gmail username
->setPassword('my_smtp_password'); // or your gmail password
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('johndoe@gmail.com' => 'John Doe'))
->setTo(array('dskanth.99@gmail.com'))
->attach(Swift_Attachment::fromPath('doc.pdf'))
->setBody('Here is the message itself');
$result = $mailer->send($message);
?>
<?php
require_once 'lib/swift_required.php';
// Using smtp
$transport = Swift_SmtpTransport::newInstance('my_smtp_host.com', 25)
// Using Gmail
// $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('my_smtp_username') // or your gmail username
->setPassword('my_smtp_password'); // or your gmail password
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('johndoe@gmail.com' => 'John Doe'))
->setTo(array('dskanth.99@gmail.com'))
->attach(Swift_Attachment::fromPath('doc.pdf'))
->setBody('Here is the message itself');
$result = $mailer->send($message);
?>
Comments
Post a Comment