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 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上