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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?