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

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)