tags:

views:

408

answers:

3

Hello,

After a user adds 5 emails and their name to form below, and then hits the "send" button, I would like to send a message such as "Hello, your friend 'sendername' recommends that you use thissite.com. Please visit the site." I would also like to specify the from address that this email has on it.

What PHP script could do this?

Thanks in advance,

JOhn

<div class="email1">
<form method="post" action="friends.php">
email address of friend 1:<br>
<input name="email1" type="text" size="55"><br>
<br>
email address of friend 2:<br>
<input name="email2" type="text" size="55"><br>
<br>
email address of friend 3:<br>
<input name="email3" type="text" size="55"><br>
<br>
email address of friend 4:<br>
<input name="email4" type="text" size="55"><br>
<br>
email address of friend 5:<br>
<input name="email5" type="text" size="55"><br>
<br>
your name:<br>
<input name="sendername" type="text" size="55"><br>
<br>
<input type="submit" value="send" name="Send" id="Send"/>
</form>
A: 

Just get those POST variable and use the mail() function from PHP. It's not hard

More reference about mail(): http://php.net/manual/en/function.mail.php

Fu4ny
A: 
$to      = $_POST['email1'];
$subject = 'your subject';
$message = 'Hello, your friend '.$_POST['sendername'].' recommends that you use thissite.com. Please visit the site.';
$headers = 'From: [email protected]' . "\r\n";

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

Repeat this process the times you need.

yoda
Cool... quick question: how would I make thissite.com a hyperlink in the body of the email message?
John
Also, how could I add an image to the bottom of the email message I send out?
John
A: 

You can use the mail function of php.

The code would be something like this

$msg = "a message";
$subject = "a subject";
mail($_POST['email1'], $subject,$msg,'From: ' . $_POST['sendername'] . "\n\r" );
mail($_POST['email2'], $subject,$msg,'From: ' . $_POST['sendername'] . "\n\r" );
mail($_POST['email3'], $subject,$msg,'From: ' . $_POST['sendername'] . "\n\r" );
mail($_POST['email4'], $subject,$msg,'From: ' . $_POST['sendername'] . "\n\r" );
mail($_POST['email5'], $subject,$msg,'From: ' . $_POST['sendername'] . "\n\r" );

Also changing your form to that would make the code more easy

<div class="email1">
<form method="post" action="friends.php">
email address of friend 1:<br>
<input name="email[]" type="text" size="55"><br>
<br>
email address of friend 2:<br>
<input name="email[]" type="text" size="55"><br>
<br>
email address of friend 3:<br>
<input name="email[]" type="text" size="55"><br>
<br>
email address of friend 4:<br>
<input name="email[]" type="text" size="55"><br>
<br>
email address of friend 5:<br>
<input name="email[]" type="text" size="55"><br>
<br>
your name:<br>
<input name="sendername" type="text" size="55"><br>
<br>
<input type="submit" value="send" name="Send" id="Send"/>
</form>

It would recude the code to

Hope this helps.

$msg = "a message";
$subject = "a subject";
foreach($_POST['email'] as $email){        
mail($email, $subject,$msg,'From: ' . $_POST['sendername'] . "\n\r" );
}

your comment code

$msg = "<html><body><h1><a href="thissite.com">thissite</a></h1><img src='http://youserver.com/img.jpg'&gt;&lt;/body&gt;&lt;/html&gt;";
$subject = "a subject";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $_POST['sendername'] . "\r\n";
foreach($_POST['email'] as $email){
mail($email, $subject,$msg,$headers);
}
RageZ
cool... I'll give it a try. Quick question: how would I make thisite.com a hyperlink in the body of the email message?
John
Also, how could I add an image to the bottom of the email message I send out?
John
the most easiest way would be to make the mail being an html mail. you would put that image on your webserver and link it the email
RageZ
I have edited the code.
RageZ
@RageZ You could add hyperlink for thissite.com also. little change in your $msg, <a href='http://thissite.com'>thissite.com</a> instead of <h1>test</h1>
paulrajj
@RageZ the code you provide works great, but when i try to change $headers .= 'From: ' . $_POST['sendername'] . "\r\n"; to $headers = 'From: [email protected]' . "\r\n";, the HTML and images don't work anymore. Any idea why?
John
@john use the $header variable by concatenating for example, $headers .= 'From: [email protected]' . "\r\n";
paulrajj
voila changed the link ^^ don't why you asked that!
RageZ
anyway would great if he approve my answer...
RageZ