dongque1646 2015-06-16 11:53
浏览 76
已采纳

使用Smtp发送电子邮件的PHP代码

I am new to PHP and trying to send an email with PHP script.

I did change "php.ini" settings to SMTP = "in.mailjet.com" smtp_port= 25 auth_username="abc" auth_password="abc" sendmail_from = "abc@abc.com"

Simple HTML file to call php file is form name="contactform" method="post" action="PHPMailer/email.php"
style="padding-left:5%;"> <td colspan="2" style="text-align:center"> <br> <input class="btn btn-info" type="submit" value="Download">

email.php file

    require 'class.phpmailer.php';
    $mail = new PHPMailer(true);
    $mail->IsSMTP();
    $mail->Host="in.mailjet.com";
    $mail->SMTPAuth = true;
    $mail->Username = 'abc';
    $mail->Password = 'abc';
    $mail->SetFrom('abc','Your Name');
    $mail->AddAddress('jibran.ishtiaq@runyourfleet.com','Their Name');
    $mail->Subject = 'Your email from PHPMailer and Mailjet';
    $mail->MsgHTML('This is your email message.");
    $mail->Send();

I am trying to send email from IIS server(test server), but I am not been able to send please help me out or let me know what I am missing.

Thank You

  • 写回答

1条回答 默认 最新

  • dongtui2029 2015-06-16 11:58
    关注

    Try below code:

    require 'class.phpmailer.php';
    
    $mail = new PHPMailer();
    
    $mail->isSMTP(); // send via SMTP
    
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    //$mail->SMTPDebug = 2;
    
    $mail->Host = $host; //SMTP server
    $mail->Port = $port; //set the SMTP port; 587 for TLS
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
    
    if (strlen($username)) {
        $mail->SMTPAuth = true;
        $mail->Username = $username;
        $mail->Password = $password;
    } else {
        $mail->SMTPAuth = false;
    }
    
    $mail->From = $from; // FROM EMAIL
    $mail->FromName = $fromName; // FROM NAME
    $mail->addAddress($to);
    $mail->addReplyTo($from);
    
    $mail->IsHTML($html);
    
    $mail->Subject = $subject; // YOUR SUBJECT
    $mail->Body = $message; // YOUR BODY
    $mail->addAttachment($attachment); // YOUR ATTACHMENT FILE PATH
    
    if ($mail->send()) {
        echo "Message Sent";exit;
    } else {
        echo "Message Not Sent<br>";
        echo "Mailer Error: " . $mail->ErrorInfo;exit;
    }  
    

    Note: Replace all variables with yours appropriately

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效