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 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊