简体   繁体   English

如何在没有php.ini文件的情况下在webhost上设置php

[英]How to setup php on webhost without php.ini file

I'm close to finishing my first real website, this website uses php mail function to send mail from my website. 我即将完成我的第一个真实网站,该网站使用php mail功能从我的网站发送邮件。 My script works because I'm running mercury which routes the mail through gmail smtp. 我的脚本有效,因为我正在运行水银,该水银通过gmail smtp路由邮件。 Now my question is if someone can tell me how to connect to this smtp server from php because my webhost doesn't allow me acces to the php.ini file. 现在我的问题是,是否有人可以告诉我如何从php连接到此smtp服务器,因为我的虚拟主机不允许我访问php.ini文件。

Thanks in advance 提前致谢

This PHP class will do it for you. 这个PHP类将为您完成。 Most real webapps don't rely on PHP's mail() , they use this class instead (or swiftmailer, but his one's much easier). 大多数真正的Web应用程序都不依赖PHP的mail() ,而是使用此类(或swiftmailer,但他的类要容易得多)。

PHPMailer PHPMailer

Sample code (from the linked page) 示例代码(来自链接页面)

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM