Sending email from PHP using SMTP and email goes to spam.What will be easiest way to send email to inbox? [duplicate]

▼魔方 西西 提交于 2019-12-25 08:07:40

问题


I am trying to send email from PHP using SMTP but every time I am getting emails in my spam. I searched on google and got some solution but still I am getting email in spam. Would you help me in this?

//$mail->isSMTP();                                      // Set mailer to use SMTP

$mail->Host = 'smtp.gmail.com';                       // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'abc@gmail.com';                   // SMTP username
$mail->Password = '***';               // SMTP password
$mail->SMTPSecure = 'tls';         // Enable encryption,'ssl' also accepted
$mail->Port = 587;                                    //Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom('abc@gmail.com', 'admin');     //Set who the message is to be sent from
$mail->addReplyTo('abc@gmail.co', 'First Last');  //Set an alternative reply-to address
$mail->addAddress($to, 'user');  // Add a recipient
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Hello';
$mail->Body    = "<html>
<head>
<title>HTML email</title>
</head>
<body>
<a href='/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
</body>
</html>";
if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

回答1:


// Use phpmailer library from github install and use
require_once('PHPMailer/PHPMailerAutoload.php');
if(isset($_REQUEST['submit']))
{
    $mail = new PHPMailer(); // defaults to using php "mail()"

    $body = "Name : ".$_REQUEST['name']."<br> Email Id ".$_REQUEST['email']."<br> message ".$_REQUEST['message'];

    $mail->IsSMTP();                                      // set mailer to use SMTP
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "mail.xx.co";
    $mail->Port = 25; 
    $mail->Username = "support@xx.co";  // SMTP username
    $mail->Password = "xxxxxxxxxx"; // SMTP password
    $mail->SetFrom('support@xx.co',$_REQUEST['subject']);
    $mail->AddAddress('support@xx.co', $_REQUEST['name']);
    $mail->IsHTML(true);                                  // set email format to HTML
    $mail->Subject = $_REQUEST['subject'];
    $mail->Body    = $body;

    if(!$mail->Send()) {
      echo '<strong>Email</strong> sent failed.';
    } else {
      echo '
        <strong>Email</strong> s`enter code here`ent successfully.';
    }
}

// get smtp host detail from the cpanel



回答2:


Your problem is not caused by your code.

You need to make sure that the email is coming from a server that is associated with the domain you are sending from.

Every email you send needs to be signed with an SPF record as per the Sender Policy Framework in order to not end up in spam boxes.

You can usually add an SPF record to your DNS yourself.

Another thing to check is that the SMTP server you are using is not blacklisted in any way.



来源:https://stackoverflow.com/questions/41361169/sending-email-from-php-using-smtp-and-email-goes-to-spam-what-will-be-easiest-wa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!