dozc1071 2016-01-30 07:08 采纳率: 100%
浏览 16
已采纳

使用php通过联系表单发送邮件[重复]

This question already has an answer here:

I have this function and after doing some research i'm still not being able to find out why this isn't working I am new to PHP and would like some help. thank you.

sendemail.php

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Mail Sent. Thank you , we will contact you shortly. '
    );

    $name = @trim(stripslashes($_POST['name'])); 
    $email = @trim(stripslashes($_POST['email'])); 
    $subject = @trim(stripslashes($_POST['subject'])); 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from = $email;
    $email_to = 'help@addonevents.in';//replace with your email

    $body = 'Name: ' . $name . "

" . 'Email: ' . $email . "

" . 'Subject: ' . $subject . "

" . 'Message: ' . $message;

    mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
    echo json_encode($status);
    die;

My form

<form name="contact-form" method="POST" action="sendemail.php">
                <label>Name *</label>
               <input type="text" name="name" required="required">
                <label>Email *</label>
                <input type="email" name="email" required="required">
                <label>Phone</label>
                <input type="number">
                 <label>Subject *</label>
                <input type="text" name="subject" required="required">
                 <label>Message *</label>
                 <textarea name="message"  required="required" rows="8"></textarea>
                 <button type="submit" name="submit" required="required">Submit Message</button>
</form> 

can anyone help? I am getting the success message but no mail to my inbox Thank you.

</div>
  • 写回答

1条回答 默认 最新

  • douping7975 2016-01-30 09:10
    关注

    As far as I know, almost nobody uses PHP's mail function to send emails professionally. Even official documentation on that function says:

    It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

    I used PHPMailer, which is quite easy to setup and use. I think it is the most popular mailing library in PHP. You can get it here.

    The documentation from PHPMailer describes well what is wrong with mail function.

    Many PHP developers utilize email in their code. The only PHP function that supports this is the mail() function. However, it does not provide any assistance for making use of popular features such as HTML-based emails and attachments.

    Formatting email correctly is surprisingly difficult. There are myriad overlapping RFCs, requiring tight adherence to horribly complicated formatting and encoding rules - the vast majority of code that you'll find online that uses the mail() function directly is just plain wrong! Please don't be tempted to do it yourself - if you don't use PHPMailer, there are many other excellent libraries that you should look at before rolling your own - try SwiftMailer, Zend_Mail, eZcomponents etc.

    Here is a small (official) example on how to use PHPMailer:

    <?php
    require 'PHPMailerAutoload.php';
    
    $mail = new PHPMailer;
    
    //$mail->SMTPDebug = 3;                               // Enable verbose debug output
    
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');
    
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    $mail->isHTML(true);                                  // Set email format to HTML
    
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
    

    So don't reinvent the wheel, and use proven and reliable library to do it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度