php,从任何电子邮件地址发送电子邮件

Ist*_*med 0 php email

电子邮件将被发送给人们——这是一件非常平常的事情。

但发送电子邮件的地址会不时发生变化。发送邮件的地址将作为站点管理员的输入。

问题是,从 Gmail 帐户发送电子邮件需要某种类型的编码,使用 yahoo 发送电子邮件则需要另一种编码,等等。

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

有免费的这样的脚本吗?

Al-*_*unk 6

发送电子邮件而不进行身份验证

<?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);
?>
Run Code Online (Sandbox Code Playgroud)

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

<?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>");
  }
 ?>
Run Code Online (Sandbox Code Playgroud)

假设您有 Zend Framework,您可以通过Zend_Mail通过 SMTP 发送来执行相同的操作。下面的示例使用 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);
Run Code Online (Sandbox Code Playgroud)