尝试在提交表单后创建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.

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;
    }