带有变量的 PHP MAIL 函数>>>如何?



我正在创建一个联系人.php。我的计划是将用户之前输入的内容包含在 e-maail 中。(变量)在这种情况下,包括发件人电子邮件、主题和消息。我不想使用梨。我想使用我的SSMTP,它安装在Debian上的Raspberry Pi上。

表单 + 输入和文本区域的代码:

 <form method="POST" action="contact.php?page=log">
<input type="text" name="from_email" placeholder="Your E-Mail"/>
<input type="text" name="subject_email" placeholder="Subject"/>
<textarea rows="5" cols="50" name="message_email" style="width: 100%" placeholder="Message"></textarea>
<input type="submit" name="submite_email" value="Send E-Mail" />
</form>

PHP 电子邮件代码:

<?php
if(isset($_POST['from_email']) and isset($_POST['subject_email']) and isset($_POST['message_email'])){
$to      = 'fawfafafaf@gmail.com';
$subject = $_GET['subject_email'];
$message = $_GET['message_email'];
$headers = 'From: '$_GET['from_email'] . "rn" .
    'Reply-To: webmaster@example.com' . "rn" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
} ?>

设置了 SSMTP 和 php.ini。如果我为此使用默认表单,它会向我发送电子邮件。

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "rn" .
    'Reply-To: webmaster@example.com' . "rn" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?> 

你可以使用 phpmailer 类 https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php

    require_once('../class.phpmailer.php');
    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
    $mail             = new PHPMailer();
    $body             = file_get_contents('contents.html');
    $body             = eregi_replace("[]",'',$body);
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "mail.yourdomain.com"; // SMTP server
    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                               // 1 = errors and messages
                                               // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "yourname@yourdomain"; // SMTP account username
    $mail->Password   = "yourpassword";        // SMTP account password
    $mail->SetFrom('name@yourdomain.com', 'First Last');
    $mail->AddReplyTo("name@yourdomain.com","First Last");
    $mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($body);
    $address = "whoto@otherdomain.com";
    $mail->AddAddress($address, "John Doe");
    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }

相关内容

  • 没有找到相关文章