简体   繁体   English

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

[英]PHP send mail using defined SMTP mail server

I use simple php to process sending form inputs arrays using localhost server. 我使用简单的php处理使用localhost服务器发送表单输入数组的过程。 Now I need to switch to defined mail server like Gmail SMTP to handle sending process. 现在,我需要切换到已定义的邮件服务器(例如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);

?>

With mail(), you can't. 使用mail(),您不能这样做。

Use for example: SwiftMailer. 例如,使用: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 请参阅: http : //swiftmailer.org/docs/sending.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能使用外部SMTP服务器在PHP中发送邮件 - can't send mail in php using external smtp server PHP-指定SMTP服务器以通过发送邮件 - PHP - Specifying the SMTP server to send mail through 尝试在php中使用Yahoo SMTP发送邮件 - Trying to send mail using yahoo smtp in php 使用php发送未经身份验证的smtp邮件 - Send smtp mail with no authentication using php 使用LOCALHOST smtp GMAIL上的XAMPP和PHP中的mail()函数发送邮件 - Send mail using mail() function in PHP , XAMPP on LOCALHOST smtp GMAIL SMTP邮件发送错误的PHP - Smtp mail Send error php Codeigniter:您的服务器可能未配置为使用此方法发送邮件。 无法使用 PHP SMTP 发送电子邮件 - Codeigniter: Your server might not be configured to send mail using this method. Unable to send email using PHP SMTP 无法使用 PHP SMTP 发送电子邮件。 您的服务器可能未配置为使用此方法发送邮件 - Unable to send email using PHP SMTP. Your server might not be configured to send mail using this methods 无法使用 PHP SMTP 发送 email。您的服务器可能未配置为使用此方法发送邮件 - Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method Codeigniter 3 无法使用 PHP ZC2239A92BDE29F0A00F9173193CC2FE 发送 email。 您的服务器可能未配置为使用此方法发送邮件 - Codeigniter 3 Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM