duanmin0941 2019-01-28 22:11
浏览 115

通过SMTP发送PHP表单

I've found some similar questions to my query, but none that fully match my situation.

I had a basic mail() PHP setup for use on the contact form on my website but, turns out this is blocked on my hosting server to avoid spam/abuse.

The web host recommends using SMTP as a workaround, but the tutorial they link to doesn't seem to provide a working solution for me!

This is my HTML:

<form action="php/mail.php" method="POST">
<p class="form-field-title">Name</p>
<input class="contact-field focus-hide" type="text" id="name" name="name" required autocomplete="off">
<p class="form-field-title">Email Address</p>
<input class="contact-field focus-hide" type="email" id="email" name="email" required autocomplete="off">
<p class="form-field-title">Phone</p>
<input class="contact-field focus-hide" type="text" id="number" name="number" autocomplete="off">
<p class="form-field-title">Message</p>
<textarea class="contact-field focus-hide" id="message" name="message" data-gramm_editor="false" required autocomplete="off"></textarea>
<input class="contact-button" type="submit" value="Send Message">
</form>

And this is my original (non-SMTP) mail.php code:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];

$formcontent="Name: $name 
Email: $email 
Phone Number: $number     
Message: $message";
$recipient = "email@email.com";
$subject = "Website Contact Form Enquiry";
$mailheader = "From: $email 
";

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

header('Location: ../thanks.html');
exit();
?>

I have the email server details for my web hosting, but none of the SMTP solutions I've found are working for me!

Is anyone able to help me? I've seen a few things about using PEAR, but their website doesn't seem to be working...?

Thanks!

  • 写回答

1条回答 默认 最新

  • doudandui1592 2019-01-29 11:14
    关注

    If your form is in an email, I think you can't post data (maybe I'm wrong). But you can send it to your script with method get.

    <form action="php/mail.php" method="GET">
    </form>
    

    Then update your php :

    <?php
    $name = $_GET['name'];
    $email = $_GET['email'];
    $number = $_GET['number'];
    $message = $_GET['message'];
    
    $formcontent="Name: $name 
    Email: $email 
    Phone Number: $number     
    Message: $message";
    $recipient = "email@email.com";
    $subject = "Website Contact Form Enquiry";
    $mailheader = "From: $email 
    ";
    
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    
    header('Location: ../thanks.html');
    exit();
    ?>
    

    It should do the job.

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)