简体   繁体   中英

how to send email via SMTP using PHP

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

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>

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM