邮件收到垃圾邮件

时间:2017-03-24 07:10:30

标签: php phpmailer

我尝试使用php发送邮件,但是邮件会发送到垃圾邮件 以下是我的代码

 //send mail
    $from = get_option('client_mail');
    $to = $_POST['email'];
    $subject = get_option('subject_');
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "From:".$from."\r\n";
    $message1= "<html>
    <body>
    <table width='100%' border='0'>
    <tr>
    <td>here is my is mail ending code
    </td>
    </tr>
    </table>
    </body>
    </html>";
    if(mail($to,$subject,$message1,$headers))
    {
        $yes = 'mail send';
    }
    else
    {
        $yes = 'mail not send';
    }

过程结束后,我收到了“邮件发送”。 我也尝试PHPMailerAutoload 这是代码

  if( file_exists("PHPMailer-master/PHPMailerAutoload.php") && is_readable("PHPMailer-master/PHPMailerAutoload.php") && include("PHPMailer-master/PHPMailerAutoload.php")) {

                //mail sending
                $email = new PHPMailer();
                $email->From      = 'from_email';
                $email->FromName  = 'fname';
                $email->Subject   = 'mail attarchment';
                $email->Body      = 'after remove atachment';
                $email->AddAddress( 'to_email' );


               if(!$email->send()) {
                   echo 'Message could not be sent.';
                   echo 'Mailer Error: ' . $mail->ErrorInfo;
               } else {
    echo 'Message has been sent';
               }
                }

这是在邮件处理“消息已发送”之后得到的

1 个答案:

答案 0 :(得分:0)

使用php邮件时你必须使用smtp模式。然后它不会发送垃圾邮件 他们在这里描述了如何使用带有smtp模式的php邮件程序

 require_once('class.phpmailer.php');
    function sendmail($to,$subject,$message,$name)
    {
                  $mail             = new PHPMailer();
                  $body             = $message;
                  $mail->IsSMTP();
                  $mail->SMTPAuth   = true;
                  $mail->Host       = "smtp.gmail.com";
                  $mail->Port       = 587;
                  $mail->Username   = "youraccount@gmail.com";
                  $mail->Password   = "your gmail password";
                  $mail->SMTPSecure = 'tls';
                  $mail->SetFrom('youraccount@gmail.com', 'Your name');
                  $mail->AddReplyTo("youraccount@gmail.com","Your name");
                  $mail->Subject    = $subject;
                  $mail->AltBody    = "Any message.";
                  $mail->MsgHTML($body);
                  $address = $to;
                  $mail->AddAddress($address, $name);
                  if(!$mail->Send()) {
                      return 0;
                  } else {
                        return 1;
                 }
    }
sendmail("mailto@example.com","example subject","dummy message","dummy name");

REFFERENCE: https://codeforgeek.com/2014/11/phpmailer-ultimate-tutorial/