doume5227 2018-05-18 09:22
浏览 62

php mail()在ubuntu服务器上失败[重复]

This question already has an answer here:

I'm trying to write a code to send emails from a contact form, I'm doing tests on a ubuntu server and here is the php config on this server:

enter image description here

And here is my php code (I've tested this same code in other online production server and works perfectly):

$ToEmail = ''; 
$EmailSubject = 'Site contact form'; 
$mailheader = "From: ".$_POST["email"]."
$mailheader .= "Reply-To: ".$_POST["email"]."
$mailheader .= "Content-type: text/html; charset=iso-8859-1
$MESSAGE_BODY = "Name: ".$_POST["name"].""; 
$MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"]).""; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?>

This code isn't working on my Ubuntu server, what can I do? I don't want to use libraries. I read that in Linux server there is no need to install anything and no need to add a SMTP configuration on the code, even in the php manual give some basics examples to send emails and those don't have any SMTP code. What is the problem with this code?

  • 写回答

1条回答 默认 最新

  • douna4762 2018-05-18 09:54

    This isn't the answer you wanted but probably the best way to tackle given our discussion on the main question.

    A PHPMailer Example

    You'll just need to adapt your current code to your variables listed in your original code.

    Source: PHPMailer Example - Official Package GitHub

    // Import PHPMailer classes into the global namespace
    // These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    //Load Composer's autoloader
    require 'vendor/autoload.php';
    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        //Server settings
        $mail->SMTPDebug = 2;                                 // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = ';';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '';                 // 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('', 'Mailer');
        $mail->addAddress('', 'Joe User');     // Add a recipient
        $mail->addAddress('');               // Name is optional
        $mail->addReplyTo('', 'Information');
        $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';
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 QT 实现 RSTP 语音对讲功能
    • ¥50 AES魔改之后的安全性关于PRF(相关搜索:密码学)
    • ¥15 有没有谁能高分通过 reCaptcha v3验证,国外网站。有兴趣留言,有偿。
    • ¥15 用C语言写的一个程序遇到了两个问题第一是偏移正确但读取不到坐标,第二个问题是自己定义的函数实现不了获取指定进程模块。
    • ¥15 在安装Anaconda时总是闪退怎么办?
    • ¥15 对图中电路进行以下几个方面的分析
    • ¥15 对图中电路进行以下几个方面的分析
    • ¥15 对图中电路进行以下几个方面的分析
    • ¥15 对图中电路进行以下几个方面的分析
    • ¥500 抖音主页视频预存加载卡bug