用PHPMailer联系表格 [英] Contact Form with PHPMailer

查看:138
本文介绍了用PHPMailer联系表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的表单: >

  • Message



  • 我使用PHPMailer发送表单,因为我也希望它包含一个附件PDF格式),它位于服务器上。



    我已将表格发送到可以发送姓名,电子邮件和附件的地方名称,电子邮件和消息。当一个人工作时,它似乎禁用了其他人。



    请参阅下面我正在使用的PHP代码:

     <?php 

    $ field_fullname = $ _POST ['cf_mercury'];
    $ field_email = $ _POST ['cf_jupiter'];
    $ field_message = $ _POST ['cf_uranus'];

    //定义电子邮件的收件人
    $ to ='email@emailaddress.co.uk';
    //定义电子邮件的主题
    $ subject ='通过网站从'。$ field_fullname;
    //创建一个边界字符串。它必须是唯一的
    //所以我们使用MD5算法生成一个随机哈希
    $ random_hash = md5(date('r',time()));
    //定义我们想要传递的标头。请注意,它们是用\r\\\

    $ body_message ='From:'。$ field_fullname。\\\
    分隔的。
    $ body_message。='消息:'。$ field_message;
    //主要信息
    $ headers =From:$ field_email\r\\\
    Reply-To:$ field_email;
    //添加边界字符串和MIME类型说明
    $ headers。=\r\\\
    Content-Type:multipart / mixed; boundary = \PHP-mixed - 。$ random_hash。 \ ;
    //将atachment文件内容读入一个字符串,
    //用MIME base64,
    对它进行编码//并将其分割为更小的块
    $ attachment = chunk_split(base64_encode(的file_get_contents( '/网站/ 123reg / LinuxPackage22 / DA /吨/ EC / damtechdesigns.co.uk /的public_html /样张/ ETAP / etapbooklet.pdf')));
    //定义消息的正文。
    ob_start(); //打开输出缓冲区
    ?>
    --PHP-mixed-<?php echo $ random_hash; ?>
    Content-Type:multipart / alternative; boundary =PHP-alt-<?php echo $ random_hash;?>

    --PHP混合 - <?php echo $ random_hash; ?>
    Content-Type:application / pdf; name =ETAPBooklet.pdf
    Content-Transfer-Encoding:base64
    Content-Disposition:attachment

    <?php echo $ attachment; ?>
    --PHP-mixed-<?php echo $ random_hash; ?>

    <?php
    //将当前缓冲区内容复制到$ message变量并删除当前输出缓冲区
    $ body_message = ob_get_clean();
    //发送电子邮件
    $ mail_sent = @mail($ to,$ subject,$ body_message,$ headers);
    //如果邮件发送成功,打印邮件发送。否则打印邮件失败

    if($ mail_sent){?>
    < script language =javascripttype =text / javascript>
    alert('谢谢您与谁联系,我们会尽快与您联系。');
    window.location ='index.html';
    < / script>
    <?php
    }
    else {?>
    < script language =javascripttype =text / javascript>
    alert('消息失败,请将您的电子邮件发送至email@emailaddress.co.uk');
    window.location ='index.html';
    < / script>
    <?php
    }

    ?>

    代码当前状态是它发送附件但不显示消息。



    任何和所有帮助都将受到极大的赞赏,如果我可以做更多的事情来表达我的问题并提高其可理解性,请告诉我。



    解决方案:

     <?php 

    $ field_fullname = $ _POST ['cf_mercury'];
    $ field_email = $ _POST ['cf_jupiter'];
    $ field_message = $ _POST ['cf_uranus'];

    require_once('class.phpmailer.php');

    $ mail = new PHPMailer(); //默认使用phpmail()

    $ body ='From:'。$ field_fullname;
    $ body。='消息:'。$ field_message;

    $ mail-> SetFrom($ field_email,$ field_fullname);

    $ mail-> AddReplyTo($ field_email,$ field_fullname);

    $ address =email@address.co.uk;
    $ mail-> AddAddress($ address,Name);

    $ mail-> Subject ='通过网站从'。$ field_fullname;

    $ mail-> AltBody =要查看邮件,请使用兼容HTML的电子邮件查看器!; //可选,注释掉并测试

    $ mail-> MsgHTML($ body);

    $ mail-> AddAttachment(/ websites / 123reg / LinuxPackage22 / da / mt / ec / damtechdesigns.co.uk / public_html / proofs / etap / etapbooklet.pdf); //附件
    $ mail-> AddAttachment(); //附件

    if(!$ mail-> Send()){
    {?>
    < script language =javascripttype =text / javascript>
    alert('消息失败,请将您的电子邮件发送至alternatemail@address.co.uk');
    window.location ='index.html';
    < / script>
    <?php
    }
    } else {
    ?>
    < script language =javascripttype =text / javascript>
    警报('感谢您联系ETAP中心,我们会尽快与您联系。');
    window.location ='index.html';
    < / script>
    <?php
    }
    ?>


    解决方案

    您提供的代码仅显示尝试发送带有单个附件的电子邮件,以便在发送消息+附件时很难发现可能出现的问题。请注意,对于MIME,'message'本身就是一个MIME附件,并且具有处理内联的功能。

    只需使用swiftmailer或phpmailer就可以节省很多痛苦。 p>

    I have a very simple form consisting of:

    • Name
    • Email
    • Message

    I am using PHPMailer to send the form because I also want it to include an attachment (PDF) which sits on the server.

    I have gotten the form to a point where it can send either the name, email and attachment OR name, email and message. When one works it seems to disable the other.

    Please see below the PHP code I am using:

    <?php 
    
      $field_fullname = $_POST['cf_mercury'];
      $field_email = $_POST['cf_jupiter'];
      $field_message = $_POST['cf_uranus'];
    
    //define the receiver of the email 
    $to = 'email@emailaddress.co.uk'; 
    //define the subject of the email 
    $subject = 'Message via the website from '.$field_fullname;
    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash 
    $random_hash = md5(date('r', time())); 
    //define the headers we want passed. Note that they are separated with \r\n 
    $body_message = 'From: '.$field_fullname."\n";
    $body_message .= 'Message: '.$field_message;
    //Main message
    $headers = "From: $field_email\r\nReply-To: $field_email"; 
    //add boundary string and mime type specification 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    //read the atachment file contents into a string,
    //encode it with MIME base64,
    //and split it into smaller chunks
    $attachment = chunk_split(base64_encode(file_get_contents('/websites/123reg/LinuxPackage22/da/mt/ec/damtechdesigns.co.uk/public_html/proofs/etap/etapbooklet.pdf'))); 
    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
    
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: application/pdf; name="ETAPBooklet.pdf"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  
    
    <?php echo $attachment; ?> 
    --PHP-mixed-<?php echo $random_hash; ?>-- 
    
    <?php 
    //copy current buffer contents into $message variable and delete current output buffer 
    $body_message = ob_get_clean(); 
    //send the email 
    $mail_sent = @mail( $to, $subject, $body_message, $headers ); 
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    
     if ($mail_sent) { ?>
        <script language="javascript" type="text/javascript">
            alert('Thank you for contacting the whoever. We will contact you shortly.');
            window.location = 'index.html';
        </script>
    <?php
    }
      else { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send your email to email@emailaddress.co.uk');
            window.location = 'index.html';
        </script>
    <?php
    }
    
    ?>
    

    The codes current state is that it sends the attachment but does not display the message.

    Any and all help would be hugely appreciated and if I can do anything more to articulate my question and increase its understandability just let me know.

    Solution:

    <?php
    
      $field_fullname = $_POST['cf_mercury'];
      $field_email = $_POST['cf_jupiter'];
      $field_message = $_POST['cf_uranus'];
    
    require_once('class.phpmailer.php');
    
    $mail             = new PHPMailer(); // defaults to using php "mail()"
    
    $body = 'From: '.$field_fullname;
    $body .= 'Message: '.$field_message;
    
    $mail->SetFrom($field_email, $field_fullname);
    
    $mail->AddReplyTo($field_email,$field_fullname);
    
    $address = "email@address.co.uk";
    $mail->AddAddress($address, "Name");
    
    $mail->Subject    = 'Message via the website from '.$field_fullname;
    
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
    $mail->MsgHTML($body);
    
    $mail->AddAttachment("/websites/123reg/LinuxPackage22/da/mt/ec/damtechdesigns.co.uk/public_html/proofs/etap/etapbooklet.pdf");      // attachment
    $mail->AddAttachment(""); // attachment
    
    if(!$mail->Send()) {
      { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send your email to alternatemail@address.co.uk');
            window.location = 'index.html';
        </script>
    <?php
    }
    } else {
      ?>
        <script language="javascript" type="text/javascript">
            alert('Thank you for contacting the ETAP Centre. We will contact you shortly.');
            window.location = 'index.html';
        </script>
    <?php
    }
    ?>
    

    解决方案

    The code you've presented only shows the atempt to send an email with a single attachment so it's rather hard to see what might be going wrong when you send the message + attachment. Note that with MIME, the 'message' is itself a MIME attachment with disposition inline.

    You might save yourself a lot of pain by simply using swiftmailer or phpmailer.

    这篇关于用PHPMailer联系表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆