簡體   English   中英

PHP使用定義的SMTP郵件服務器發送郵件

[英]PHP send mail using defined SMTP mail server

我使用簡單的php處理使用localhost服務器發送表單輸入數組的過程。 現在,我需要切換到已定義的郵件服務器(例如Gmail SMTP)來處理發送過程。

<?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:\n\n"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

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

?>

使用mail(),您不能這樣做。

例如,使用:SwiftMailer。 例:

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

請參閱: http : //swiftmailer.org/docs/sending.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM