Asked  7 Months ago    Answers:  2   Viewed   80 times

I am using PHP on a website and I want to add emailing functionality.

I have WAMPSERVER installed.

How do I send an email using PHP?

 Answers

73

Using PHP's mail() function it's possible. Remember mail function will not work on a Local server.

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "rn" .
    'Reply-To: webmaster@example.com' . "rn" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?> 

Reference:

  • http://php.net/manual/en/function.mail.php
Wednesday, March 31, 2021
 
Sanguine
answered 7 Months ago
52

The PHP mail() function is for sending mail via Sendmail. If you want to use some SMTP server, you can use Zend_Mail which makes this thing very easy: http://framework.zend.com/manual/en/zend.mail.html

Using Zend_Mail you only need to write something like this:

$config = array('auth' => 'login',
            'username' => 'myusername',
            'password' => 'password');

$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender@test.com', 'Some Sender');
$mail->addTo('recipient@test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);

The above handles authentication for you and sends a mail.

Wednesday, March 31, 2021
 
hohner
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :