php - 提交表单后尝试创建一个 php 自动电子邮件,并使用 HTML 设置电子邮件样式

标签 php html css forms email

我正在构建一个允许某人注册交易的表单。我希望表单在提交后使用 php 发送 3 封电子邮件;一种是将表格的详细信息发送给我自己,一种是将详细信息发送给提供交易的人,一种是将实际的交易确认和凭证发送给注册的人。

我遇到了一些问题,在我测试时,某些电子邮件地址似乎无法接收或发送电子邮件(它适用于我的 hotmail 和 gmail 地址,但不适用于我自己的域电子邮件地址).我也不知道如何向发送的电子邮件添加某种 html 样式。

我如何设置这些电子邮件的样式并确保将它们发送到正确的电子邮件地址?

代码如下:

    <?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>\r\nReturn-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\nAirline: $airline\nPosition: $position\nCheckin: $checkin\nAttendance: $attendance\nEmail: $email\n" ); 
    echo "Email sent!"; 
    } 

    //email going to the customer//
    {         
    $from="From: me<myemail@test.com>\r\nReturn-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>\r\nReturn-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\nAirline: $airline\nPosition: $position\nCheckin: $checkin\nAttendance: $attendance\nEmail: $email\n
", $from );
    } 
}   
 ?> 

非常感谢。

最佳答案

你最好的选择是使用 PHPMailer .下面是一些通过 gmail 中继电子邮件的示例代码。您这样做的原因是为了避免来自服务器的电子邮件进入您的垃圾邮件箱。

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

关于php - 提交表单后尝试创建一个 php 自动电子邮件,并使用 HTML 设置电子邮件样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34884458/

相关文章:

php - 无法从 PHP 执行 Bash 脚本

javascript - 加载 Laravel Chart consoletvs/charts :7. *

javascript - 如何切换文字颜色?

javascript - 使图像闪烁并自动调整大小以适应浏览器

javascript - 我可以将 URL 重定向到不同页面中由 Ajax 填充的特定内容吗?

javascript - JSTree Proton 主题更改图标以打开和关闭文件夹

javascript - 谷歌翻译将英语设置为默认,以便翻译回来

php - echo html 和 php 给出语法错误

jquery - 如何使用 jquery 设置 css @media 打印样式

PHP 时间变量在 while 循环中不起作用