Search code examples
phpemailpear

Add $_POST['url'] to body of email Pear package?


I need my php script to email me some variables which is parsed to it.

I receive the email properly when any text is passed:

mypage.php?url=hello

But I do not receive the email if an url is passed:

mypage.php?url=http://www.google.com

I searched for hours but didn't found any way to do it.

This is the php script:

<?php
// Pear Mail Library
require_once "Mail.php";


$name = $_POST['name'];
$url = $_POST['url'];



$from = '<[email protected]>';
$to = '<[email protected]>';
$subject = 'Hi!';
$body = '


Name: '.$name.'
The url is: '.$url.'


';

            
$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);



$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => '[email protected]',
        'password' => 'password'
    ));


$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<h1>Message successfully sent!</h1>');
}

?>

When an actual url is parsed, I don't recive the email but also no error is shown.


Solution

  • As @Xorifelse said, I used addslashes function and it worked. I got the email properly with the url in it.

    Below is the working script:

    <?php
    // Pear Mail Library
    require_once "Mail.php";
    
    
    $name = $_POST['name'];
    $url = $_POST['url'];
    
    
    
    $from = '<[email protected]>';
    $to = '<[email protected]>';
    $subject = 'Hi!';
    $body = '
    
    
    Name: '.addslashes($name).'
    The url is: '.addslashes($url).'
    
    
    ';
    
                
    $headers = array(
        'From' => $from,
        'To' => $to,
        'Subject' => $subject
    );
    
    
    
    $smtp = Mail::factory('smtp', array(
            'host' => 'ssl://smtp.gmail.com',
            'port' => '465',
            'auth' => true,
            'username' => '[email protected]',
            'password' => 'password'
        ));
    
    
    $mail = $smtp->send($to, $headers, $body);
    
    if (PEAR::isError($mail)) {
        echo('<p>' . $mail->getMessage() . '</p>');
    } else {
        echo('<h1>Message successfully sent!</h1>');
    }
    
    ?>