How to use pear mail mime

How can you use the MIME mail pear with Google. I found this that allows you to use pear mail from Google, but not MIME mail: http://globalconstant.scnay.com/2009/11/06/sending-email-through-gmail-using-php/

require_once "Mail.php";
require_once "Mail/mime.php";

$from = "Sender <*******@googlemail.com>";
$to = "Receiver <*******@googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";

$headers = array('From' => $from,
                 'To' => $to,
                 'Subject' => $subject);


$host = "smtp.gmail.com";
$port = 465;
$username = "********@googlemail.com";
$password = "********";

$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html);

$body = $mime->get();
$headers = $mime->headers($headers);

$smtp = Mail::factory("smtp",array("host" => $host,
                      "port" => $port,
                      "auth" => true,
                      "username" => $username,
                      "password" => $password));

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

if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";

I keep getting

Failed to add recipient: @localhost [SMTP: Invalid response code received from server (code: 555, response: 5.5.2 Syntax error. F52sm5542930wes.35)]

Edit:

The letter is received, but it turns out like this:

This is a message I sent from <a href=3D"http://www.php.net/">PHP</a> using=
 the PEAR Mail package and SMTP through Gmail. Enjoy!
+4
source share
6 answers

, . pear mail (http://pear.php.net/manual/en/package.mail.mail-mime.example.php):

require_once "Mail.php";
require_once "Mail/mime.php";

$from = "Sender <*******@googlemail.com>";
$to = "Receiver <*******@googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";

$headers = array('From' => $from,
                 'To' => $to,
                 'Subject' => $subject);


//$host = "smtp.gmail.com";
$host = "ssl://smtp.gmail.com"; // try this one to use ssl
$port = 465;
$username = "********@googlemail.com";
$password = "********";

//$mime = new Mail_mime($crlf);
$mime =  new Mail_mime(array('eol' => $crlf)); //based on pear doc     
$mime->setHTMLBody($html);

//$body = $mime->get();
$body = $mime->getMessageBody(); //based on pear doc above
$headers = $mime->headers($headers);

$smtp = Mail::factory("smtp",array("host" => $host,
                      "port" => $port,
                      "auth" => true,
                      "username" => $username,
                      "password" => $password));

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

if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";

, , ! ,

+4

@john: , :

<?php
require_once('Mail.php');
require_once('Mail/mime.php');

$from = 'Sender <sender@gmail.com>';
$to = 'Receiver <receiver@something.com>';
$subject = 'Sent from PHP on my machine';

$text = 'This is a message I sent from <a href="http://www.php.net/">PHP</a> '
      . 'using the PEAR Mail package and SMTP through Gmail. Enjoy!';

$message = new Mail_mime();
$message->setTXTBody(strip_tags($text)); // for plain-text
$message->setHTMLBody($text);
$body = $message->get();

$host = 'smtp.gmail.com';
$port = 587; //According to Google you need to use 465 or 587
$username = 'sender';
$password = 'your_password';

$headers = array('From' => $from,
    'To' => $to,
    'Subject' => $subject);

$smtp = Mail::factory('smtp',
    array(
        'host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

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

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

echo "\n";

?>

Update:

Edit:

, :

This is a message I sent from <a href=3D"http://www.php.net/">PHP</a> using=
the PEAR Mail package and SMTP through Gmail. Enjoy!

@john:

$body = $mime->get();

$body = $mime->get(array('text_charset' => 'utf-8'));

.

+2
$body = $mime->get(array('text_charset' => 'utf-8'));

html_charset html-.

$crlf = "\n";

$body = $mime->get(array('html_charset' => 'utf-8', 'text_charset' => 'utf-8', 'eol' => $crlf));

 .

+1

StealthyNinja, , .

, , , .

HTML , , . $headers:

$headers = $message->headers($headers);

.

0

3D after =.

$hdrs = array( 'From'    => $from,
       'To'      => $to,
       'Subject' => $subject  );

$mime =& new Mail_mime();
$mime->setTXTBody($message);

if($htmlMessage==""){
    $htmlMessage=$message;
}

$mime->setHTMLBody($htmlMessage);
if($attachmentIsFile){
    if($attachment!=null)
        $mime->addAttachment($attachment,'application/octet-stream',$attachmentName.extractExtension($attachment));
}else{
    if($attachment!="")
        $mime->addAttachment($attachment,'application/octet-stream',$attachmentName,false);
}
$body = $mime->get(array('text_encoding' => '8bit','html_encoding' => '8bit'));
$hdrs = $mime->headers($hdrs);
0

.

$ body = $mime-> get (array ('text_encoding' => '8bit', 'html_encoding' => '8bit'));

. ,

0

Source: https://habr.com/ru/post/1782194/


All Articles