1

Hi am new in PHP i need to send email via SMTP using PHP. Actually i did in normal code, but mail land in spam. (Following mycode)

$to = 'xxx@gmail.com';
$subject = 'Test mail';
$from = 'zzz@domain.com';
$header = "From: $from \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '//some HTML Code...';
if (mail($to, $subject , $message, $headers)) {
echo 'Success';
} else {
echo 'Error';
}

So i refer some websites they said using SMTP (Simple Mail Transfer Protocol) mail will land in inbox. Kindly please help me for sending email via SMTP.

Thanks in advance

Ramesh S
  • 37
  • 7
  • 1
    Possible duplicate of [Send email using the GMail SMTP server from a PHP page](http://stackoverflow.com/questions/712392/send-email-using-the-gmail-smtp-server-from-a-php-page) – IeuanG Feb 22 '16 at 08:07

3 Answers3

2

Thank You for helping me this is my sample code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test 3 mail</title>
</head>

<body>
<?php
if(isset($_POST['send']))
{
    require_once'Swift-5.1.0\lib\swift_required.php';

    $name = filter_var($_POST['name'],  FILTER_SANITIZE_STRING);
    $email = filter_var($_POST['email'],  FILTER_SANITIZE_STRING);
    $message = filter_var($_POST['message'],  FILTER_SANITIZE_STRING);
    $body = "Name:".$name."<br>Email:".$email."<br>Message:".$message."";
    //tarnport
    $transport = Swift_SmtpTransport::newInstance('d9.privatewebsolution.com', 465, "ssl")
      ->setUsername('test@dimain.com')
      ->setPassword('XXXXX');
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance('Uni-Tech')
        ->setFrom(array('test@dimain.com' => "Name"))
        ->setTo($email)
        ->setBody($body, 'text/html');

    $result = $mailer->send($message);
}
?>
<form action="test3.php" method="post">
<label>Name</label>
<input type="text" name="name" /><br />
<label>Email</label>
<input type="email" name="email" /><br />
<label>Message</label>
<textarea name="message"></textarea><br />
<input type="submit" name="send" value="Send" />
</form>
</body>
</html>
Ramesh S
  • 37
  • 7
0

PHP is not using a well configured SMTP server that's why it immediately goes to spam. You can try using PhpMailer a well-known mailer for PHP.

kimbebot
  • 9
  • 1
  • 3
0

I think SwiftMailer is good for sending emails in PHP using SMTP Protocol

http://swiftmailer.org/docs/introduction.html

Please ensure that https://pear.php.net/package/Net_SMTP Package is enabled in PHP .

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
  ->setUsername('***@gmail.com')
  ->setPassword('***');   */
 $mailer = Swift_Mailer::newInstance($transport);
$subject="<subject of email";
$body="<body goes Here";
$message = Swift_Message::newInstance($subject)
  ->setFrom(array('from@example.com' => "Name"))
  ->setCc(array("cc@example.com" => "ccName"))
  ->setTo($to_email)
  ->setBody($body, 'text/html')
// Add alternative parts with addPart()
  ->addPart($body, 'text/plain');
$result = $mailer->send($message);