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 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 C++行情软件的tick数据如何高效的合成K线
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。
  • ¥20 在easyX库下编写C语言扑克游戏跑的快,能实现简单的人机对战
  • ¥15 svpwm波形异常求解答
  • ¥15 STM32——硬件IIC从机通信代码实现
  • ¥15 微生物组数据分析--微生物代谢物