douhun8647 2016-01-19 18:36 采纳率: 0%
浏览 52
已采纳

尝试在提交表单后创建php自动发送电子邮件,并使用HTML为电子邮件设置样式

I'm building a form that allows someone to sign up for a deal. I want the form to send 3 emails using php once it has been submitted; one sending the details of the form to myself, one sending the details to the person offering the deal, and one sending the actual deal confirmation and voucher to the person who signed up.

I've run into problems in that certain email addresses don't seem to receive or be sent the email when i'm testing it (it works for both my hotmail and gmail addresses, but not for my own domain email address). I also can't figure out how to add some sort of html style to the emails being sent.

How can I style these emails and make sure that they are being sent to right email addresses?

here is the code:

    <?php 
}  
 else                /* send the submitted data */ 
{ 
$name=$_REQUEST['name']; 
 $airline=$_REQUEST['airline'];
  $position=$_REQUEST['position'];
   $checkin=$_REQUEST['checkin'];
    $attendance=$_REQUEST['attendance'];
    $email=$_REQUEST['email']; 
    $terms=$_REQUEST['terms'];

if (($name=="")||($position=="")||($checkin=="")||($attendance=="")||($email=="")) 
    { 
    echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
    } 

    //email going to me//
else{         
    $from="From: Deal 1 Form Submission<$email>
Return-path: $email"; 
    $subject="Message sent using your contact form"; 
    mail("myemail@test.com", 
    $subject, 
    $message="someones taken the deal: heres their info: <br> Name: $name
Airline: $airline
Position: $position
Checkin: $checkin
Attendance: $attendance
Email: $email
" ); 
    echo "Email sent!"; 
    } 

    //email going to the customer//
    {         
    $from="From: me<myemail@test.com>
Return-path: $email"; 
    $subject="Thank you for choosing this deal"; 

    mail("$email", $subject, $message="thanks for choosing this deal!, please present this voucher when attending the restaurant", $from );  
    } 

    //email going to the partner//
    {         
    $from="From: Me<myemail@test.com>
Return-path: $email"; 
    $subject=" Great news! Someone has chosen your deal"; 
    mail("partneremail@test.com", 
    $subject,
    $message="Fantastic news, a customer has taken your deal! here's their info: Name: $name
Airline: $airline
Position: $position
Checkin: $checkin
Attendance: $attendance
Email: $email

", $from );
    } 
}   
 ?> 

Many thanks.

  • 写回答

1条回答 默认 最新

  • dongzhong2018 2016-01-19 18:56
    关注

    Your best bet is to use PHPMailer. Here is some example code that relays the email through gmail. The reason why you would want to do this is to avoid emails from the server going into you spam box.

        $debug = true; // set it to false in production
        $Mail = new PHPMailer();
        if ($debug) {
            // this allows you to see the details of the connection to gmail
            $Mail->SMTPDebug = 4;
        }
        $Mail->isSMTP();
        $Mail->Host = 'smtp.gmail.com';
        $Mail->SMTPAuth = true;
        $Mail->Username = 'your@gmail.com';
        $Mail->Password = '[your-password]';
        $Mail->SMTPSecure = 'tls';
        $Mail->Port = 587;
    
        $Mail->setFrom('from@address.com', 'John Smith');
        $Mail->addAddress('to@address.com', 'Someone Else');
    
        $Mail->addReplyTo('from@address.com', 'John Smith');
        $Mail->addBCC('bcc@address.com', 'BCC Person');
        $Mail->addAttachment('path/to/img/or/pdf/or/whatever/you/want/to/attach.pdf');
    
        $Mail->Subject = 'Test Subject';
        $Mail->Body    = '<h1>Hi!  This is an HTML format body'
        $Mail->AltBody = 'Hi!  Im just a text email in case HTML is not supported!';
    
        if (!$Mail->send()) {
            print $Mail->ErrorInfo;
            return false;
        } else {
            return true;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档