doushuo1080 2018-07-03 17:10
浏览 86
已采纳

使用PHP在localhost上发送电子邮件

I want to send an email on localhost but don't really know how to do it... I tried some different ways but it doesn't work. Can you please help me? I used PHPMailer https://github.com/PHPMailer/PHPMailer/tree/5.2-stable as the Mailserver but I thinks thats maybe wrong implemented or so... Don't know if it's important but I use MAMP. Sorry if the mistake is obviously - i'm a kind of newbie. :-)

This is what I currently have:

<?php
if (isset($_POST['submit'])) {
  require("PHPMailer/PHPMailerAutoload.php");
  ini_set("SMTP","ssl://smtp.gmail.com");
  ini_set("smtp_port","465");
  $mail = new PHPMailer();
  $mail->SMTPAuth = true;
  $mail->Host = "smtp.gmail.com";
  $mail->SMTPSecure = "ssl";
  $mail->SMTPAuth = true;
  $mail->Username = "mail account";
  $mail->Password = "password for account";
  $mail->Port = "465";

  $mail->setFrom('receiver mail', 'TEST');
  $mail->addReplyTo('receiver mail', 'TEST');
  $mail->addAddress('recipient mail');
  $mail->Port = "465";

  $mail->isHTML(true);
  $mail->Subject = "test";

  // get text from input fields
  $email = $_POST['email'];
  $name = $_POST['name'];
  $address = $_POST['address'];
  $city = $_POST['city'];
  $number = $_POST['number'];
  $textarea = $_POST['textarea'];

  $bodyContent =
    "<p>Name: " . $name . "</p>
    <p>E-Mail: " . $email . "</p>
    <p>Telefonnummer: " . $number . "</p>
    <p>Adresse: " . $address . $city . "</p>
    <p>Anliegen: " . $textarea . "</p>";

  $mail->Body = $bodyContent;

  if (!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error' . $mail->ErrorInfo;
  } else {
    echo 'Message has been sent.';
  }
}
?>
  • 写回答

2条回答 默认 最新

  • dsfhe34889 2018-07-04 15:47
    关注

    Okay, i finally found the solution. I updated my code to this

    <?php
    if (isset($_POST['sendButton'])) {
      require("PHPMailer/PHPMailerAutoload.php");
      require 'PHPMailer/class.phpmailer.php';
    
      $mail = new PHPMailer();
      $mail->isSMTP();
      $mail->SMTPAuth = true;
      $mail->Host = "smtp.gmail.com";
      $mail->SMTPSecure = "tls";
      $mail->SMTPAuth = true;
      $mail->Username = "secret";
      $mail->Password = "secret";
    
      $subject = utf8_decode('test');
      $mail->setFrom('secret', $subject);
      $mail->addReplyTo('secret', $subject);
      $mail->addAddress('secret');
      $mail->Subject = utf8_decode('test');
      $mail->Port = "587";
      $mail->isHTML(true);
      $email = $_POST['email'];
      $name = $_POST['name'];
      $address = $_POST['address'];
      $city = $_POST['city'];
      $number = $_POST['number'];
      $sendText = $_POST['sendText'];
    
      $bodyContent =
        "<p>Name: " . $name . "</p>
        <p>E-Mail: " . $email . "</p>
        <p>Telefonnummer: " . $number . "</p>
        <p>Adresse: " . $address . ' ' . $city . "</p>
        <p>Anliegen: " . $sendText . "</p>";
    
      $mail->Body = $bodyContent;
    }
    ?>
    

    Besides I had to go to myaccount.google.com -> "Sign-in & security" -> "Apps with account access", and turn "Allow less secure apps" to "ON" Now everything is fine. Thank you for your help guys

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据