简体   繁体   中英

Sending emails using PHP, having troubles configuring the SMTP server

Trying to send an activation email for newly registered users, here's the code so far:

$to = $email;
$subject = "Activate your account!";
$headers = "From: noreply@test.com";    
$body = "..."

$mail($to, $subject, $body, $headers);

In the tutorial by phpacademy, the instructor used

ini_set("SMTP", $server);

I cannot figure out what to do. I think that my SMTP server should be my Yahoo account. How can I send the emails?

No, your account is not the same as a server . Your $server variable should be set to a suitable hostname or IP address of an SMTP server, like $server = "mail.myisp.net"; .

Check your email client to find out your own mail server, or ask your ISP.

$server should be the name (or IP address) of a machine that's running an SMTP server daemon and that you have access to. This should work with Yahoo's service (probably something like smtp.yahoo.com) but you'll need to provide a user and password if you're going to email an address that's not @yahoo.com. More common is to use a local server, like something your office/school would have configured for you. Or, if you're running a unix-ish OS, you can probably just skip the line completely and let it default to localhost.

If you are going to sending email with PHP mail function you have to setup a SMTP server first in your PC. Other way, you can use a class to do this with out setting any thing in your PC. PHP mailer known to do this with few lines: here is an example from the site of the project:

<?php

require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->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.";
$mail->WordWrap = 50;

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?> 

Official web site: http://phpmailer.worxware.com

When I had to send activation emails to the users after the users registered I had considered the same SMTP route but even if you manage to get it going there are certain disadvantages using SMTP

  1. Delay in sending out emails.
  2. Emails considered spam
  3. Config etc.

Try this out, it has been very cheap and reliable service for me so far. www.postmarkapp.com They have APIs that you can hook into using PHP. My sample PHP code that is in Production is as below.

    // Create a "server" in your "rack", then copy it's API key
define('POSTMARKAPP_API_KEY', 'xyz-blah-blah-blah');

// Create a "Sender signature", then use the "From Email" here.
// POSTMARKAPP_MAIL_FROM_NAME is optional, and can be overridden
// with Mail_Postmark::fromName()
define('POSTMARKAPP_MAIL_FROM_ADDRESS', 'email address that you are sending from-google apps email in my case');
define('POSTMARKAPP_MAIL_FROM_NAME', 'Description of the Sender name');

// Create a message and send it
Mail_Postmark::compose()
    ->addTo($to, $to)
    ->subject('whatever subject you want to put here for your email')
    ->messagePlain($pin_message)
    ->send();

Yes its that easy once you have created a virtual server with postmarkapp. Guess what first 1000 emails are free so you can try it out and if not use SMTP your way. After the first 1000 i think 5000 costs $7 or something. Really nice and cheap and I have had no downtime at all.

Try it out. You will like it. By the way I am not associated with postmark in any way its just that I like using their services. Check out my question when I was working on this before.

PHP mail() function sends the email but it takes more than 10 mins to show up

if you're on a linux server you should check that the server as a smtp server installed, if not you'd better configure one in the php.ini http://fr.php.net/manual/en/mail.configuration.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM