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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵