在 PHP 中使用 mail() 发送邮件


send mail using mail() in php

你好,我正在学习php,我开始了解mail((函数,我已经尝试过这段代码

function sendMail()
{
    $to = 'Sohil Desai<sohildesaixxxx@gmail.com>';
    $from = 'Sohil Desai<sohildesaixxxx@hotmail.com>';
    $subject = 'Test Mail';
    $headers  = 'MIME-Version: 1.0'. "'r'n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
    $headers .= 'From: '.$from . "'r'n";
    $headers .= 'X-Mailer: PHP/'.phpversion();
    $message = 'This mail is sent for testing.';
    $mail = mail($to, $subject, $message, $headers);
    if (!$mail) {
        return 'Error occured sending mail.';
    }
    return 'Mail successfully sent.';
}
echo sendmail();

我只测试了gmail,ymail和hotmail。

此功能在gmail和hotmail的垃圾邮件中发送邮件,并且不会向ymail发送邮件。

为什么会这样??

我使用的是 Ubuntu 12.04 和 php 版本 5.3.10。

谁能帮我??感谢提前的哈尔珀斯..

尝试添加"to"作为标题:

$headers  = 'MIME-Version: 1.0'. "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
$headers .= 'To: ' . $to . "'r'n";
$headers .= 'From: '.$from . "'r'n";
$headers .= 'X-Mailer: PHP/'.phpversion();
更多详细信息

可以在 http://www.php.net/manual/en/function.mail.php 中找到(示例 #4(。

Verry simple

$to = 'someone@example.com';
$subject = 'the subject';
$message = 'the message';
$headers = 'From: webmaster@example.com' . "'r'n" .
    'Reply-To: webmaster@example.com' . "'r'n" .
    'X-Mailer: PHP/' . phpversion();
 
mail($to, $subject, $message, $headers);

希望对您有所帮助!

来源: http://w3webtools.com/send-email-using-php/

与其使用蹩脚的 email(( 函数,不如使用 PHPMailer。它易于使用,并为您完成大部分艰苦的工作。

特征:

  • 可能是世界上最受欢迎的从PHP发送电子邮件的代码!
  • 被许多开源项目使用:WordPress,Drupal,1CRM,SugarCRM,
  • Yii,Joomla!等等 集成的SMTP支持 - 无需本地邮件服务器即可发送 使用多个收件人,抄送,密件抄送和
  • 回复 不读取 HTML 电子邮件的邮件客户端的多部分/备用电子邮件 支持 UTF-8 内容和 8 位、base64、二进制、和带引号的可打印编码 SMTP
  • 使用LOGIN,PLAIN,NTLM,CRAM-MD5和Google的身份验证通过 SSL 和 TLS 传输的XOAUTH2机制 中的错误消息47种语言!
  • DKIM 和 S/MIME 签名支持 与 PHP 兼容 5.0 及更高
  • 版本 更多!

您所要做的就是下载此类并将其包含在您的项目中,然后使用它,例如:

一个简单的例子

<?php
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->setFrom('from@example.com', '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';
}

您可以在 https://github.com/PHPMailer/PHPMailer 的示例文件夹中找到更多可以玩的内容。