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.

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

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算