开发者

How to create a script that users can specify an email and send a user a pre-defined email?

So, basically, I'm creating a page where a user can visit, and enter in the following information:

1. Their First Name

2. Their Email

3. Recipient's Email

They then can send a pre-defined email, saying something like the following...

"Hello, {Recipient's email}. {First name} ({Email}) has just sent you a slurp! You can ignore this message, or slurp them back.

[Slurp Them Back] [Slurp Someone Else] [What's A Slurp?]"

The whole part about the Slurp is something that doesn't really matter, it's just the text from the pre-defined email. The text in {...} is taken from the fields on the page, and the text in [...] are just links.

Does anyone have any idea on how to do this? Even if you can't customize the e开发者_如何学Gomail, and it would just be without the information from the site, help would be appreciated. Thanks.!

Here's an example of what I'm going for... Example Layout


It's possible to do using the php mail function. You can take input for everything you specified, then use the example here. They show it in a basic and more advanced form.


Interestingly, the first chapter of Head First PHP describes almost exactly this scenario (except that the recipient is fixed). If you want to learn more about PHP you can look into the book; otherwise, their code is online at http://www.headfirstlabs.com/books/hfphp/ (actual code link: http://www.headfirstlabs.com/books/hfphp/code/HeadFirstPHPMySQL_code_ch01.zip)


Sending an email by itself can be done by using the mail() command

mail('to.address@host.com', 'subject', 'message', 'From: from.address@host.com');

The whole code would look something like this:

HTML:

<form action="slurping.php" method="post">
    Your name: <input type="text" name="name" /><br />
    Your email: <input type="text" name="email" /><br />
    Recipient: <input type="text" name="recipient" /><br />
    <input type="submit" />
</form>

PHP (slurp.php):

// order of parameters is: to, subject, message body, additional headers
mail(
    $_POST['recipient'], 
    'You just got slurped', 
    'You\'ve been slurped by '.$_POST['name'].'. Slurp him/her back by visiting http://slurp.com/',
    "From: {$_POST['email']}\r\n"
);

This will send out an email like it's coming from the senders email address to the recipient.

There's a lot missing there, though. What you normally want to include is:

  • Validation of input on the client side (javascript)
  • Validation of input on the serverside
  • Clear out and handle right encodings etc

If you want to do it properly however, so that a greater percentage of your users actually receive the email, you should use a 3rd party library to send out emails. I recommend PHP Mailer or Swift Mailer.

Those libraries provide an easy mechanism for you to include HTML in your emails, attachments, allow for easily connecting to SMTP servers and handle all escaping and encoding issues for you.

On top of that they encapsulate everything within an Object oriented approach, so if that's your cup of tea, you'll be happy. Se their websites for more info.

It's pretty funny, I wrote a blog post about the very issue of email deliverability (getting through spam filters) etc: http://arnorhs.com/2011/02/21/delivering-email-with-php/ - It might be helpful to you.

Cheers


On submit you can send a predefine email in php

following is the code sample

assumed that you will have a html page and following is the sample html code

Send Slurp

Enter Your Name:
Enter Your Email:
Enter Recipient's Email:

following is the php code in "="send_email.php"

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $your_name = $_POST['your_name'];
    $your_email = $_POST['your_email']; 
    $recipients_email= $_POST['recipient_email']; 

    $email_subject = 'My Slurp'; 
    $email_body = "Hello, $recipients_email. $your_name ($your_email) has just sent you a slurp! You can ignore this message, or slurp them back.
    [Slurp Them Back] [Slurp Someone Else] [What's A Slurp?]";

    echo $email_body;
    // Send Email
    mail($recipients_email, $email_subject, $email_body);

}
?> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜