Jump to content

How do you send emails with PHP?


diode

Recommended Posts

Hello,

What I am making is an account registration part of my website. The registration form works and it submits it to the next page and all that. But on the page it submits it to, I'm making it so that it inserts a row into a table using MySQL and adds them to the users table so that they can log in and use the site and stuff. The variables I'm using on this initial account creation are:

$firstname
$lastname
$acctname
$password
$email

I want to send them an email from the page that receives the form, to show them their account details, and provide a link for them to click on to activate the account. But I can't get the email sender thing to work. I'm sure it's probably something easy, but I couldn't find it in the PHP Manual that came with my php.

Thanks in advance :),
diode
Link to comment
Share on other sites

I have tried everything suggested here, and it's still not working. What are the requirements in order to email information from a php page to somebody? For example, do I need certain daemons running, other things installed, etc.

Thanks :),
diode
Link to comment
Share on other sites

What type of server are you running, something from your home or something you are paying for like shared hosting? Does that host have cPanel?

If your running from a shared host with PHP and apache you can do something like....

[code=php:0] $subject = 'MY SUBJECT';
$message = '<html><body>my html message</body></html>';

$headers  = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$headers .= 'Return-Path: YOURMAIL@DOMAIN.COM'."\r\n";
$headers .= 'From: YOUR NAME <YOURMAIL@DOMAIN.COM>';

mail('SENDTO@DOMAIN.COM', $subject, $message, $headers, "-f YOURMAIL@DOMAIN.COM");[/code]

In order for AOL users or even some other mail clients such as hotmail you might need to have Reverse DNS enabled on your server. You'll need to talk to your host about that one.
Link to comment
Share on other sites

simple code for sending email with HTML :
[code=php:0]<?php 
// Recipients. You can send it to more than 1 person! 
$to = "you1 <you@yoursite.com>" . ", " ; // note the comma 
$to .= "you2 <you2@yoursite.com>"; 

// This is the email subject 
$my_subject = "My first HTML message!"; 

// Message 
$my_message = ' 
<html> 
<head> 
<title>My first HTML message!</title> 
</head> 
<body> 
<table> 
<tr> 
<td>Hi! Just to say i can now send html mail :p</td> 
</tr> 
<tr> 
<td>Do you like it!? C ya</td> 
</tr> 
</table> 
</body> 
</html> 
'; 

// To send HTML mail, you can set the Content-type header. 
$my_headers = "MIME-Version: 1.0rn"; 
$my_headers .= "Content-type: text/html; charset=iso-8859-1rn"; 

// Setting additional headers 
$my_headers .= "From: HTML mail <example@example.com>rn"; 

$my_headers .= "Cc: example@example.comrn"; 
$my_headers .= "Bcc: example@example.comrn"; 

// And now mail it! 
mail($to, $my_subject, $my_message, $my_headers); 
?>[/code]
Or read more in here :
http://www.t4vn.net/example/showcode/Sending-HTML-email.html
Link to comment
Share on other sites

Sorry to everyone who I haven't responded to. I was out of town last night and had to work today.

It's my own personal server in my room. It is a 400 mhz Power Mac G4 running MAMP. Mac OS X 10.4.8, Apache, PHP, and MySQL.

I get no error message. It acts like it has sent the mail, and yet I receive nothing. In the "From" field, I don't know what to put. How would I check to see if I have an email daemon running? I can't remember the unix command to do it. This is, of course, if I really need an email daemon. Do I have to have a working email address to send the data? And does it have to be a daemon on the server sending it?

Thanks a lot for your time :),
diode
Link to comment
Share on other sites

Well here's my code, and I still can't send emails.

[code]

<?php

$email = $_POST['email'];
$fromaddress = "do-not-reply@myserveripaddress";
$eol="\r\n";
$mime_boundary=md5(time());  $headers .= 'From: Boards<'.$fromaddress.'>'.$eol;

  $headers .= 'Reply-To: Do Not Reply<'.$fromaddress.'>'.$eol;
  $headers .= 'Return-Path: Boards<'.$fromaddress.'>'.$eol;    // these two to set reply address
  $headers .= "X-Mailer: PHP v".phpversion().$eol;          // These two to help avoid spam-filters

  # Boundry for marking the split & Multitype Headers
  $headers .= 'MIME-Version: 1.0'.$eol;
  $headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;


$subject = "Account registration";
$msg = "Thank you for registering for the message boards.$eol $eol---------------------------------$eol Here is your account information:$eol First name:\t$firstname $eol Last name:\t$lastname $eol Account name:\t$acctname $eol Password:\t$password $eol Email address:\t$email\n--------------------------------------------$eol $eol To activate your account, click this link, or enter it in your browser's address bar and go to it.\t\t pagename.php $eol $eol Observe reasonable conduct and enjoy the message boards!";

ini_set(sendmail_from,$fromaddress);  // the INI lines are to force the From Address to be used !
mail($email, $subject, $msg, $headers);
ini_restore(sendmail_from);
echo " A registration email has been sent to the email address you specified. To activate your account, you must check your email and click on the link provided. Thank you and enjoy the message boards.";


?>

[/code]

It turns out I do have sendmail installed. SMTP is set to localhost. smtp_port is 25 in phpinfo().

Is it even possible for this to work? lol... Thanks :),
diode
Link to comment
Share on other sites

Now this is going to be ironic since I own a web hosting company and frown upon this but another possibility for you is to have your own php scripts and all of that on your server and then get a free hosting from a web hosting company supporting PHP/MySQL/cPanel.

Generally you can find one that offers little amounts of space but it won't take up much for sendmail. Then all you need to do is have your script on your own server send that command (or include it) to the server in cyberspace which sends the e-mail and then inturn redirects you back to your server.

You can see specifics for the cPanel configuration you'll need at http://xpsservers.com but don't try signing up for a free one, I'm already onto you ;)

Hope this helps,
-SMC
Link to comment
Share on other sites

ok, this is what ive had to do for my normal server.......

ive had to port forward port 81 ( any port you want will work becauase my isp blocks port 80 outgoing ( or shaterver) ( port 80 is defult http))  and then get people to type :81 after the url...........

now they also block port 25.... outgoing only for hosting becauase they use that port for their mail... so i am going to use mercury mailserver.... free and came with xampp, so i thinnk all i d ghave to do is port forward another prot and then set email to go to the mail server.... and redirect to outside world.... through another port....

i think that should help
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.