Php mail with pear

How to use pear mail package

It’s a PHP email class that makes sending emails with HTML and attachments easy That’s why using SMTP for sending emails is more preferable than using the web server.

How to use pear mail package

I want to send some emails through my scripts.

I want to send html emails maybe big with huge chunks of html or maybe small. However I know about the mail() function but I have heard that something like pear package is good for Sending Emails.

I visited pear.php.net but couldn’t understand though.I don’t know what is this. Can somebody help how to use these packages with some examples. Please answer with guide.

You might find that PHPMailer will do what you’re looking for. It’s a PHP email class that makes sending emails with HTML and attachments easy

Here’s an example from their website as to how to use PHP Mailer:

IsSMTP(); // telling the class to use SMTP $mail->Host = "smtp.example.com"; // SMTP server $mail->From = "from@example.com"; $mail->AddAddress("myfriend@example.net"); $mail->Subject = "First PHPMailer Message"; $mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.You Can Use HTML!"; // You can put HTML tags in this string $mail->WordWrap = 50; $mail->IsHTML(true); // This allows you to use HTML in the body if(!$mail->Send()) < echo 'Message was not sent.'; echo 'Mailer error: ' . $mail->ErrorInfo; > else < echo 'Message has been sent.'; >?> 
 mixed send ( mixed $recipients , array $headers , string $body ) send($recipients, $headers, $body); ?> 

this is the code sample given by them its enough for explanations, you can set the $body as the bulk html or messages to be sent as the content

Читайте также:  Программа по проверке html

Email — PHP Pear : SMTP mail script with Attachment, i am creating script where the script would be generating a mail such that there becomes two option available in form of «text/plain» or «text/html». …

Best way to send 10,000+ emails with PEAR/Mail_Queue

I have a cron which generates the whole mail info and puts in a database table using $mail_queue->put(. ) with an option to delete emails after they’re sent.

Here’s where I need a little help: What is the best way to send the emails after I have the above info? Running the $mail_queue->sendMailsInQueue() right away, using other cron job(s) or something else?

The server limit by the way is 100 emails / minute. Currently the last csv diff for Mail_Queue is not applied (currently working with the support on that), so I can’t use the «delay» option.

I had an idea to use the $seconds_to_send option, but it’s calculated on the base of create_time field, which isn’t a big help, but it’s also an option.

Any ideas and suggestions would be really appreciated.

Personally, I would go the cron way because it gives less opportunity for failure. Say your mail server stops responding or for some other reason becomes unavailable. Or what if your entire network goes offline for a few hours, but the servers are still generating emails. I say use the queue.

As for the delay thing, just have a service/cronjob pick up the queue every sixty seconds, pop 100 emails and send them, then quit. You might get a queue of emails to be sent but that’s going to happen no matter what system you choose. The queue will empty during off-peak hours, anyways.

use two scripts. one for populating your mail_queue table with the emails you need to send and the second script to send those emails in chunks of 90 mails at a go. set the second script to be activated about every 2 minutes or so.

you could also just upgrade your hosting plan 😉

why you dont loop through 100 emails and sleep for 60seconds. this costs you no servertime, the only downside your script runs a little longer.

How to send email using PHP PEAR mail and Exchange, How shall I configure PEAR mail and/or Exchange so that this can work? Anonymous SMTP email sending is not an option in this environment. …

How to use pear mail mime

How can you use PEAR Mail mime with google. I found this which lets you use pear mail with google, but not mail mime: 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 = " 

This is HTML

"; $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";

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

The email is now received, however it turns out like this:

This is a message I sent from PHP using= the PEAR Mail package and SMTP through Gmail. Enjoy! 

It looks like you have an issue with the email header. I updated your code based on the pear mail doc (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 = " 

This is HTML

"; $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";

It works for me so I hope it will work for you! Cheers, Erez

@john: Using the code from the link you posted, modify it like so —

'; $to = 'Receiver '; $subject = 'Sent from PHP on my machine'; $text = 'This is a message I sent from PHP ' . '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"; ?> 

Edit:

The email is now received, however it turns out like this:

This is a message I sent from PHP using= the PEAR Mail package and SMTP through Gmail. Enjoy! 
$body = $mime->get(array('text_charset' => 'utf-8')); 
$body = $mime->get(array('text_charset' => 'utf-8')); 

In addition to the above you need html_charset for html emails.

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

This will fix the abberations like  in the e-mails.

Couldn’t comment on StealthyNinja’s answer so I posted my own, sorry about that.

The question is also a bit old but I maybe this could be useful to others.

To get rid of all that HTML tags and weird characters you have to prepare your header so the e-mail client can read the e-mail right. Try this AFTER setting your $headers array:

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

It should work ok after that.

Best way to send 10,000+ emails with PEAR/Mail_Queue, Yes, a queue is good, but if your entire network comes offline for a few hours, and you’re still pushing e-mails to the queue, your mailserver will be in …

How to Send a PHP Email via SMTP with the PEAR

While using the PHP mail function, the email is sent directly from the web server. However, that can cause some problems, if you haven’t set the FROM address properly or the email is not hosted by the DreamHost.

That’s why using SMTP for sending emails is more preferable than using the web server.

In this article, we will show you how to use the PEAR option to send emails via SMTP accurately.

Configure PEAR for Sending Emails

In this section, you will see the steps that will walk you through how to configure PEAR.

Install PEAR

The first step is installing PEAR. All you need to do is visiting the PEAR article and Installing PEAR on your web server.

Installing PEAR Mail Packages

The second step is installing the necessary PEAR Mail packages. To get the instructions, you can visit this page.

Generally, you may need the packages of Net_SMTP and Mail.

Generate a PHP File

The final step includes generating a PHP file, which will use PEAR for sending messages with SMTP.

Configure PHP for Sending Emails with SMTP

In this section, we will provide the code that will help you to send emails via SMTP. You need to generate a mail.php file with the code below:

error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);require_once "Mail.php";$host = "ssl://smtp.dreamhost.com"; $username = "[email protected]"; $password = "your email password"; $port = "465"; $to = "[email protected]"; $email_from = "[email protected]"; $email_subject = "Subject Line Here:" ; $email_body = "whatever you like" ; $email_address = "[email protected]";$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address); $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $email_body);if (PEAR::isError($mail)) < echo("

" . $mail->getMessage() . "

"
); > else < echo("

Message successfully sent!

"
); > ?>

All you need is updating the highlighted fields.

Activate Gmail

If you use Gmail for sending emails via SMTP, then you are required to allow the application to access the Gmail address. Otherwise, the email will not be sent.

For any detailed information, you can check out this tutorial.

When to Use PEAR?

This option is used only while generating a custom built mail form. In other cases, especially when you use WordPress, you are required to use plugins or built-in tools for sending emails via SMTP.

PHP Pear Mail Can’t Send Name With Unicode Characters, On my server i’m using php pear package and when i send email, and i type in «name» field unicode characters, it doesn’t send.. when i type latin …

Источник

Оцените статью