How to send email from PHP without SMTP server installed?
Asked Answered
W

4

26

I have a classic LAMP platform (Debian, Apache2, PHP5 and MySQL) on a dedicated server.

I heard PHPMailer can send email without having installed SMTP. Is PHPMailer the best choice for this?

Wray answered 10/2, 2011 at 22:57 Comment(0)
T
23

Yes, PHPMailer is a very good choice.

For example, if you want, you can use the googles free SMTP server (it's like sending from your gmail account.), or you can just skip the smtp part and send it as a typical mail() call, but with all the correct headers etc. It offers multipart e-mails, attachments.

Pretty easy to setup too.

<?php

$mail = new PHPMailer(true);

//Send mail using gmail
if($send_using_gmail){
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth = true; // enable SMTP authentication
    $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $mail->Username = "[email protected]"; // GMAIL username
    $mail->Password = "your-gmail-password"; // GMAIL password
}

//Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";

try{
    $mail->Send();
    echo "Success!";
} catch(Exception $e){
    //Something went bad
    echo "Fail - " . $mail->ErrorInfo;
}

?>
Truculent answered 10/2, 2011 at 23:7 Comment(10)
i am using something like that atmWray
So... this uses SMTP and SSL?Assignat
Just clarifying, because the question does say "without having installed SMTP."Assignat
"something went wrong" is not quite helpful, use $mail->ErrorInfo :))Ellingston
@Kolob Canyon the answer does answer the question: "For example, if you want, you can use the googles free SMTP server (it's like sending from your gmail account.), or you can just skip the smtp part"...Mixie
@Proto doesn't sending from google's gmail count as SMTP?Rossy
@Kolob Canyon as I quoted in bold, you can just skip / erase the smtp part and it should still workMixie
Does this work with Microsoft Azure Web Apps? I need to send emails from a 'contact-us' form, and I am hoping PHPMailer could work :)Shy
@Shy Why not? Wherever you can run php code, you can use phpmailer.Swahili
require("phpmailer/class.phpmailer.php"); for the sake of completenessClot
G
3

You can use phpmailer to send using the default php mail() function as well.

I recommend not trying to do things manually using the mail() function, use phpmailer instead and configure it to use mail().

I'd like to point out that even though you're not using an SMTP connection to send the mails yourself, the mail() function will use either an SMTP connection or the server's sendmail program to send out the emails anyways, so that will have to be configured for it to work correctly.

Gentilis answered 10/2, 2011 at 23:5 Comment(0)
S
0

You have to find out the SMTP address of the recipient of the mail by yourself. (Querying the DNS/MX record with function like getmxrr)

Splatter answered 4/12, 2023 at 9:59 Comment(0)
D
-1

Without SMTP, you can use the PHP mail function: http://php.net/manual/en/function.mail.php

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Daron answered 10/2, 2011 at 23:1 Comment(2)
A tip: remember to validate any user-submitted information before putting it in headers, that way they can't insert their own info into the headers when using mail.Preterhuman
The answer is wrong, because you will have to have configured smtp anyway (which mail() uses anyway)Nolasco

© 2022 - 2024 — McMap. All rights reserved.