php,从任何电子邮件地址发送电子邮件 [英] php, send email from any email address

查看:103
本文介绍了php,从任何电子邮件地址发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

电子邮件将被发送给人们-这是很平常的事情。

Email will be delivered to people - a veryusual thing.

但是发送电子邮件的地址有时会有所不同。

But the address from which the email will be sent will vary from time to time. And the address from which the mail will be sent will be taken as an input from the site admin.

问题是,从gmail帐户发送电子邮件需要一定的条件。类型的编码,要使用yahoo进行编码,还需要另一种编码,依此类推。

The thing is, to send email from a gmail account needs a certain type of coding, to do it with yahoo needs another kind of coding and so on.

php中从任何电子邮件地址发送电子邮件的方式是什么?

What is the way in php to send emails from any email address?

有免费的此类脚本吗?

推荐答案

发送没有身份验证的电子邮件

Sending email without authentication

<?php
$to      = 'nobody@whateverdomain.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@whateverdomain.com' . "\r\n" .
    'Reply-To: webmaster@whateverdomain.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

发送电子邮件的正确方法是通过SMTP连接。
假设已安装 PEAR Mail软件包

The correct way of sending email is through a SMTP connection. Assuming the PEAR Mail package is installed.

<?php
 require_once "Mail.php";

 $from = "Name Surname <sender@yourdomain.com>";
 $to = "Name Whatever <recipient@example.com>";
 $subject = "Subject!";
 $body = "Hi,\n\nHow are you?";

 $host = "mail.yourdomain.com";
 $username = "smtp_username";
 $password = "smtp_password";

 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));

 $mail = $smtp->send($to, $headers, $body);

 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
 ?>

假设您拥有Zend Framework,则可以通过 Zend_Mail 。下面的示例使用Google SMTP上的信息

Assuming you have Zend Framework, you can do the same by sending through Zend_Mail over SMTP. The example below uses information on Google SMTP

require_once 'Zend/Loader/Autoloader.php'; //Should be in the include_path
$autoloader = Zend_Loader_Autoloader::getInstance();

$config = array('ssl' => 'tls', 'port' => 587, 'auth' => 'login', 'username' => 'username@gmail.com', 'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);

$mail = new Zend_Mail();
if (strtolower($this->getType()) == 'html')
$mail->setBodyHtml($this->getBody());
}
else {
$mail->setBodyText($this->getBody());
}

$mail
->setFrom($this->getFromEmail(), $this->getFromName())
->addTo($this->getToEmail(), $this->getToName())
->setSubject($this->getSubject());

$mail->send($transport);

这篇关于php,从任何电子邮件地址发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆