dsdsm2016 2015-07-30 18:39
浏览 52
已采纳

PHP使用定义的SMTP邮件服务器发送邮件

I use simple php to process sending form inputs arrays using localhost server. Now I need to switch to defined mail server like Gmail SMTP to handle sending process.

<?php

    $to = "contact@mail.com";
    $from = $_REQUEST['email'];
    $name = $_REQUEST['name'];
    $headers = "From: $from";
    $subject = "[Contact form] You have a message from $name.";

    $fields = array();
    $fields{"name"} = "Name";
    $fields{"email"} = "Email";
    $fields{"phone"} = "Phone";
    $fields{"department"} = "Department";
    $fields{"message"} = "Message";

    $body = "Here is what was sent:

"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s
",$b,$_REQUEST[$a]); }

    $send = mail($to, $subject, $body, $headers);

?>
  • 写回答

1条回答 默认 最新

  • douyuanliao8815 2015-07-30 18:43
    关注

    With mail(), you can't.

    Use for example: SwiftMailer. Example:

    require_once 'lib/swift_required.php';
    
    // Create the Transport
    $transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
    ->setUsername('your username')
    ->setPassword('your password');
    
    You could alternatively use a different transport such as Sendmail or Mail:
    
    $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
    
    $transport = Swift_MailTransport::newInstance();
    
    $mailer = Swift_Mailer::newInstance($transport);
    
    $message = Swift_Message::newInstance('Wonderful Subject')
    ->setFrom(array('john@doe.com' => 'John Doe'))
    ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
    ->setBody('Here is the message itself');
    
    $result = $mailer->send($message);
    

    See this: http://swiftmailer.org/docs/sending.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数电几道习题,写出作答过程
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错